搜索入门题合集
poj1321 棋盘问题
Input
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
实现代码:
#include<iostream> #include<cstring> using namespace std; #define ll long long const int M = 10; int vis[M],n,k,cnt; string s[M]; void dfs(int r,int num){ if(num == k){ cnt ++; return ; } if(r == n) return ; for(int i = 0;i < n;i ++){ if(s[r][i]=='#'&&!vis[i]){ vis[i] = 1; dfs(r+1,num+1); vis[i] = 0; } } dfs(r+1,num); } int main() { while(cin>>n>>k){ if(n==-1&&k==-1) break; for(int i = 0;i < n;i ++){ cin>>s[i]; } cnt = 0; memset(vis,0,sizeof(vis)); dfs(0,0); cout<<cnt<<endl; } return 0; }
POJ - 2251 Dungeon Master
Is an escape possible? If yes, how long will it take?
Input
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.
Output
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!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
基础bfs,之前用dfs写了一发超时。
实现代码:
#include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; const int inf = 1e9+7; int minn,c,l,r,x1,y1,z1,x2,z2,y2; bool vis[40][40][40]; char mp[40][40][40]; int d[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; struct node{ int x,y,z,step; }; int check(int x,int y,int z){ if(x<0||x>=c||y<0||y>=l||z<0||z>=r||vis[x][y][z]||mp[x][y][z]=='#') return 0; return 1; } int bfs(){ queue<node>q; node now,next; now.x = x1;now.y=y1;now.z=z1;now.step = 0; vis[x1][y1][z1] = 1; q.push(now); while(!q.empty()){ now = q.front(); q.pop(); if(now.x == x2&&now.y == y2&&now.z == z2) return now.step; for(int i = 0;i < 6;i++){ next.x = now.x + d[i][0]; next.y = now.y + d[i][1]; next.z = now.z + d[i][2]; if(check(next.x,next.y,next.z)){ vis[next.x][next.y][next.z] = 1; next.step=now.step+1; q.push(next); } } } return 0; } int main() { while(scanf("%d%d%d",&c,&l,&r)){ //init(); if(c==0&&l==0&&r==0) break; getchar(); for(int i = 0;i < c;i ++){ for(int j = 0;j < l;j ++){ scanf("%s",&mp[i][j]); for(int k = 0;k < r;k ++){ vis[i][j][k] = 0; if(mp[i][j][k] == 'S'){ x1 = i; y1 = j; z1 = k; } if(mp[i][j][k]=='E'){ x2 = i; y2 = j;z2 = k; } } } } int ans = bfs(); if(ans==0) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",ans); } }
POJ - 3278 Catch That Cow
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
* Teleporting: FJ can move from any point X to the point 2 × X 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?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
#include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; int n,k; int step[100010]; bool vis[100010]; queue<int>q; int bfs(){ int now = n; step[now] = 0; q.push(now); while(!q.empty()){ now = q.front(); q.pop(); int next; for(int i = 0;i < 3;i ++){ if(i == 0) next = now + 1; else if(i == 1) next = now - 1; else next = now * 2; if(next < 0||next >= 100010) continue; if(!vis[next]){ vis[next] = 1; step[next] = step[now] + 1; q.push(next); } if(next == k) return step[next]; } } return 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>k; if(n>=k) cout<<n-k<<endl; else { int ans = bfs(); cout<<ans<<endl; } return 0; }
poj 1426 Find The Multiple
Input
Output
Sample Input
2 6 19 0
Sample Output
10 100100100100100100 111111111111111111
基础bfs,
实现代码
#include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; #define ll unsigned long long ll n; ll bfs(){ queue<ll>q; q.push(1); while(!q.empty()){ ll k = q.front(); q.pop(); if(k/n*n==k){ return k; } q.push(k*10); q.push(k*10+1); } return 0; } int main() { ll ans; while(cin>>n){ if(n==0) break; ans = bfs(); cout<<ans<<endl; } }
POJ - 3126 Prime Path
题目链接:http://poj.org/problem?id=3126
思路:
bfs暴力搜出四个位数的所有可能组合,逐个判断是否是素数,是的话加入队列并标记当前数字。之前想把数字转换成字符串去操作,结果转了半天把自己绕昏了。。直接暴力搜就好了
实现代码:
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<cmath> using namespace std; struct node{ int num,step; }; node a; int b; int vis[10000]; bool isprim( int num ) { if(num ==2|| num==3 ) return 1 ; if(num %6!= 1&&num %6!= 5) return 0 ; int tmp =sqrt( num); for(int i= 5;i <=tmp; i+=6 ) if(num %i== 0||num %(i+ 2)==0 ) return 0 ; return 1 ; } int bfs(){ node now,next; queue<node>q; a.step = 0; q.push(a); while(!q.empty()){ now = q.front(); q.pop(); //cout<<now.num<<endl; int w0 = now.num%10; int w1 = (now.num/10)%10; int w2 = (now.num/100)%10; int w3 = (now.num/1000)%10; //cout<<w0<<" "<<w1<<" "<<w2<<" "<<w3<<endl; for(int i = 0;i < 10;i ++){ if(w0==i) continue; next.num = i+w1*10+w2*100+w3*1000; next.step = now.step + 1; //cout<<next.num<<endl; if(next.num == b) return next.step; if(isprim(next.num)&&!vis[next.num]){ vis[next.num] = 1; q.push(next); } } for(int i = 0;i < 10;i ++){ if(w1==i) continue; next.num = w0+i*10+w2*100+w3*1000; next.step = now.step + 1; //cout<<next.num<<endl; if(next.num == b) return next.step; if(isprim(next.num)&&!vis[next.num]){ vis[next.num] = 1; q.push(next); } } for(int i = 0;i < 10;i ++){ if(w2==i) continue; next.num = w0+w1*10+i*100+w3*1000; next.step = now.step + 1; //cout<<next.num<<endl; if(next.num == b) return next.step; if(isprim(next.num)&&!vis[next.num]){ vis[next.num] = 1; q.push(next); } } for(int i = 1;i < 10;i ++){ if(w3==i) continue; next.num = w0+w1*10+w2*100+i*1000; next.step = now.step + 1; //cout<<next.num<<endl; if(next.num == b) return next.step; if(isprim(next.num)&&!vis[next.num]){ vis[next.num] = 1; q.push(next); } } } return -1; } int main() { int t; cin>>t; while(t--){ memset(vis,0,sizeof(vis)); cin>>a.num>>b; if(a.num == b) cout<<0<<endl; else{ int ans = bfs(); if(ans == -1) cout<<"Impossible"<<endl; else cout<<ans<<endl; } } }