[kuangbin带你飞]专题一 简单搜索
这个章节的内容就是利用bfs,dfs进行搜索。简单搜索,内容不是很难。 A - 棋盘问题??POJ - 1321?
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
Input
输入含有多组测试数据。?
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8,k <= n? 当为-1 -1时表示输入结束。? 随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。? Output
对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。
Sample Input 2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output 2 1 #include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h>
using namespace std; typedef long long LL; const int maxn=15; int n,k; char s[maxn][maxn]; bool vis[maxn]; int ans; void dfs(int x,int cnt) { if(cnt>=k) { ans++; return; } for(int i=x;i<n;i++) { for(int j=0;j<=n;j++) { if(s[i][j]==‘#‘&&vis[j]==false) { vis[j]=true; dfs(i+1,cnt+1); vis[j]=false;//每次选择了,后面要记得消除标记 } } } } int main() { while(scanf("%d %d",&n,&k)!=EOF) { if(n==-1&&k==-1) break; memset(vis,false,sizeof(vis)); ans=0; for(int i=0;i<n;i++) scanf("%s",s[i]); dfs(0,0); printf("%dn",ans); } return 0; }
? B - Dungeon Master?POJ - 2251?
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north,south,east,west,up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.?
Is an escape possible? If yes,how long will it take?? Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L,R and C (all limited to 30 in size).?
L is the number of levels making up the dungeon.? R and C are the number of rows and columns making up the plan of each level.? Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#‘ and empty cells are represented by a ‘.‘. Your starting position is indicated by ‘S‘ and the exit by the letter ‘E‘. There‘s a single blank line after each level. Input is terminated by three zeroes for L,R and C. Each maze generates one line of output. If it is possible to reach the exit,print a line of the form?
Escaped in x minute(s). where x is replaced by the shortest time it takes to escape.? If it is not possible to escape,print the line? Trapped! 3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0 Escaped in 11 minute(s).
Trapped! #include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h> #include <queue>
using namespace std; typedef long long LL; const int maxn=35; int l,r,c; int ans; bool flag; int sx,sy,sz; char s[maxn][maxn][maxn]; bool vis[maxn][maxn][maxn]; int dx[]={1,-1,0,0}; int dy[]={0,1,0}; int dz[]={0,-1}; struct Node { int x,y,z; int step; }; Node now,temp,nextt; void bfs() { int x,z; queue<Node>q; vis[sz][sx][sy]=1; now.x=sx; now.y=sy; now.z=sz; now.step=0; q.push(now); while(!q.empty()) { temp=q.front(); q.pop(); for(int i=0;i<6;i++) { x=nextt.x=temp.x+dx[i]; y=nextt.y=temp.y+dy[i]; z=nextt.z=temp.z+dz[i]; nextt.step=temp.step+1; if(x<0||x>=r||y<0||y>=c||z<0||z>=l)//判断边界 continue; if(s[z][x][y]==‘#‘) continue; if(s[z][x][y]==‘E‘)//到终点跳出。 { ans=nextt.step; flag=true; return; } if(s[z][x][y]==‘.‘&&vis[z][x][y]==false) { vis[z][x][y]=true; q.push(nextt); } } } } int main() { int i,j,k; while(scanf("%d %d %d",&l,&r,&c)!=EOF) { if(l==0&&r==0&&c==0) break; memset(vis,sizeof(vis)); flag=false; for(i=0;i<l;i++) { for(j=0;j<r;j++) { scanf("%s",s[i][j]); for(k=0;k<c;k++) { if(s[i][j][k]==‘S‘) { sx=j;sy=k;sz=i; } } } } bfs(); if(flag) printf("Escaped in %d minute(s).n",ans); else printf("Trapped!n"); } return 0; }
C - Catch That Cow?POJ - 3278?Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point?N?(0 ≤?N?≤ 100,000) on a number line and the cow is at a point?K?(0 ≤?K?≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting. * Walking: FJ can move from any point?X?to the points?X?- 1 or?X?+ 1 in a single minute If the cow,unaware of its pursuit,does not move at all,how long does it take for Farmer John to retrieve it? Line 1: Two space-separated integers:?N?and?K Line 1: The least amount of time,in minutes,it takes for Farmer John to catch the fugitive cow. 5 17 4 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue>
using namespace std; const int maxn=100005; struct Node { int pos; int step; }; queue<Node>q; bool vis[maxn]; int n,k; void bfs() { Node now,nextt; now.pos=n;now.step=0; q.push(now); vis[now.pos]=true; while(!q.empty()) { temp=q.front(); q.pop(); if(temp.pos==k) { printf("%dn",temp.step); return; } else { nextt.pos=temp.pos-1; if(nextt.pos>=0&&vis[nextt.pos]==false)//向后退,但是注意一定要大于0 { vis[nextt.pos]=true; nextt.step=temp.step+1; q.push(nextt); } nextt.pos=temp.pos+1; if(nextt.pos<=maxn&&vis[nextt.pos]==false)//向前进,但注意要小于maxn { vis[nextt.pos]=true; nextt.step=temp.step+1; q.push(nextt); } nextt.pos=temp.pos*2; if(nextt.pos<=maxn&&vis[nextt.pos]==false)//向前跳,但是要小于maxn { vis[nextt.pos]=true; nextt.step=temp.step+1; q.push(nextt); } } } } int main() { memset(vis,sizeof(vis)); scanf("%d %d",&k); if(n>=k)//如果在牛的后面,那么一直向后退就是最优的走法。 printf("%dn",n-k); else bfs(); return 0; }
E - Find The Multiple??POJ - 1426?
Given a positive integer n,write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input. Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n,any one of them is acceptable.
Sample Input 2
6
19
0
10
100100100100100100
222222222222222111 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue>
using namespace std; typedef long long LL; const int maxn=100005; int n; void bfs() { queue<LL>q;//注意开long long q.push(1);//不可能以0开头 LL temp; while(!q.empty()) { temp=q.front(); q.pop(); if(temp%n==0) { printf("%lldn",temp); return; } q.push(temp*10); q.push(temp*10+1); } } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) break; bfs(); } return 0; }
F - Prime Path??POJ - 3126?
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.?
— It is a matter of security to change such things every now and then,to keep the enemy in the dark.? — But look,I have chosen my number 1033 for good reasons. I am the Prime minister,you know!? — I know,so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.? — No,it’s not that simple. Suppose that I change the first digit to an 8,then the number will read 8033 which is not a prime!? — I see,being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.? — Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.? Now,the minister of finance,who had been eavesdropping,intervened.? — No unnecessary expenditure,please! I happen to know that the price of a digit is one pound.? — Hmm,in that case I need a computer program to minimize the cost. You don‘t know some very cheap software gurus,do you?? — In fact,I do. You see,there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero,of course. Here is a solution in the case above.? 1033?The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased. One line with a positive number: the number of test cases (at most 100). Then for each test case,one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros). One line for each case,either with a number stating the minimal cost or containing the word Impossible. 3 1033 8179 1373 8017 1033 1033 6
7
0 并且变幻时只有一个数字不同,并且是素数,那么就是对于每一位上的数字进行变化,如果是素数才入队列,
#include <stdio.h> #include <string.h> #include <iostream> #include <queue> #include <math.h>
using namespace std; const int maxn=100005; int T,n,m; bool vis[maxn]; struct Node { int x; int step; }; bool judge_prime(int n)//判断是否为素数,这是之前写的代码,其实建议先用线性筛筛出素数打表,复杂度会低一些。 { if(n==1) return 0; for(int i=2;i<=sqrt(n);i++) { if(n%i==0) return false; } return true; } void bfs() { memset(vis,0,sizeof(vis)); Node now,nextt; now.x=n; now.step=0; vis[now.x]=1; queue<Node>q; q.push(now); while(!q.empty()) { temp=q.front(); q.pop(); if(temp.x==m) { printf("%dn",temp.step); return ; } for(int d=0;d<4;d++) { if(d==0)//个位
{ for(int i=1;i<=9;i+=2)//个位上偶数就不可能是素数,所以是+2 { nextt=temp; nextt.x=(temp.x)/10*10+i; if(nextt.x!=temp.x&&!vis[nextt.x]&&judge_prime(nextt.x)) { vis[nextt.x]=1; nextt.step=temp.step+1; q.push(nextt); } } } else if(d==1)//十位
{ for(int i=0;i<=9;i++) { nextt=temp; nextt.x=temp.x/100*100+i*10+temp.x%10; if(nextt.x!=temp.x &&!vis[nextt.x]&&judge_prime(nextt.x)) { vis[nextt.x]=1; nextt.step=temp.step+1; q.push(nextt); } } } else if(d==2)//百位
{ for(int i=0;i<=9;i++) { nextt=temp; nextt.x=temp.x/1000*1000+i*100+temp.x%100; if(nextt.x!=temp.x &&!vis[nextt.x]&&judge_prime(nextt.x)) { vis[nextt.x]=1; nextt.step=temp.step+1; q.push(nextt); } } } else if(d==3) { for(int i=1;i<=9;i++) { nextt.x=i*1000+temp.x%1000; if(nextt.x!=temp.x&&!vis[nextt.x]&&judge_prime(nextt.x)) { vis[nextt.x]=1; nextt.step=temp.step+1; q.push(nextt); } } } } } printf("Impossiblen"); return ; } int main() { scanf("%d",&T); while(T--) { scanf("%d %d",&m); bfs(); } return 0; }
G - Shuffle‘m Up?POJ - 3087? A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips,?S1?and?S2,each stack containing?C?chips. Each stack may contain chips of several different colors. The actual shuffle operation is performed by interleaving a chip from?S1?with a chip from?S2?as shown below for?C?= 5: The single resultant stack,?S12,contains 2 *?C?chips. The bottommost chip of?S12?is the bottommost chip from?S2. On top of that chip,is the bottommost chip from?S1. The interleaving process continues taking the 2nd?chip from the bottom of?S2?and placing that on?S12,followed by the 2nd?chip from the bottom of?S1?and so on until the topmost chip from?S1?is placed on top of?S12. After the shuffle operation,?S12?is split into 2 new stacks by taking the bottommost?C?chips from?S12?to form a new?S1?and the topmost?C?chips from?S12?to form a new?S2. The shuffle operation may then be repeated to form a new?S12. For this problem,you will write a program to determine if a particular resultant stack?S12?can be formed by shuffling two stacks some number of times. Input The first line of input contains a single integer?N,(1 ≤?N?≤ 1000) which is the number of datasets that follow. Each dataset consists of four lines of input. The first line of a dataset specifies an integer?C,(1 ≤?C?≤ 100) which is the number of chips in each initial stack (S1and?S2). The second line of each dataset specifies the colors of each of the?C?chips in stack?S1,starting with the bottommost chip. The third line of each dataset specifies the colors of each of the?C?chips in stack?S2?starting with the bottommost chip. Colors are expressed as a single uppercase letter (A?through?H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 *?C?uppercase letters (A?through?H),representing the colors of the desired result of the shuffling of?S1?and?S2?zero or more times. The bottommost chip’s color is specified first. Output Output for each dataset consists of a single line that displays the dataset number (1 though?N),a space,and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset,display the value negative 1 (?1) for the number of shuffle operations. 2 4 AHAH HAHA HHAAAAHH 3 CDE CDE EEDDCC 1 2
2 -1 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <stack> #include <map>
using namespace std; typedef long long LL; const int maxn=205; const int INF=0x3f3f3f3f; int T,m; char s1[maxn],s2[maxn]; char s[maxn*2]; map<string,bool>vis; int main() { scanf("%d",&T); int casee=1; while(T--) { scanf("%d",&n); scanf("%s",s1);//输入两个字符串 scanf("%s",s2); scanf("%s",s); vis[s]=true; int step=0; while(1) { char temp[maxn*2]; int cnt=0; for(int i=0;i<n;i++)//两个字符串合并 { temp[cnt++]=s2[i]; temp[cnt++]=s1[i]; } temp[cnt]=‘ |