深度搜索:棋盘问题,详见http://poj.org/problem?id=1321
//#include<bits/stdc++.h>
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<vector> #include<set> #include<map> #include<stack> #include<queue> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; char a[10][10]; int vis[10]; int n,k; int ans,now; void DFS(int m) { if(now==k){ ans++; return ; } if(m==n) return ; for(int i=0;i<n;i++){ if(!vis[i]&&a[m][i]=='#') { vis[i]=1; now++; DFS(m+1); vis[i]=0; now--; } } DFS(m+1); } int main() { while(scanf("%d%d",&n,&k)&&n!=-1&&k!=-1) { memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) scanf("%s",a[i]); ans=now=0; DFS(0); printf("%d ",ans); } }
BFS题目:Dungeon Master (POJ2251: http://poj.org/problem?id=2251)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<vector> #include<set> #include<map> #include<stack> #include<queue> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; int L,R,C; char Map[30][30][30]; int vis[30][30][30]; int to[6][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0}; int sx,sy,sz,ex,ey,ez; int ans; struct node{ int x; int y; int z; int step; }; int check(int x,int y,int z) { if(x<0||y<0||z<0||x>=L||y>=R||z>=C) return 1; else if(Map[x][y][z]=='#') return 1; else if(vis[x][y][z]==1) return 1; return 0; } int bfs() { queue<node> q; node temp,next; while(!q.empty()) { q.pop(); } temp.x=sx; temp.y=sy; temp.z=sz; temp.step=0; q.push(temp); while(!q.empty()) { temp=q.front(); q.pop(); if(temp.x==ex&&temp.y==ey&&temp.z==ez) { return temp.step; } for(int i=0;i<6;i++) { next=temp; next.x = temp.x+to[i][0]; next.y = temp.y+to[i][1]; next.z = temp.z+to[i][2]; if(check(next.x,next.y,next.z)) continue; vis[next.x][next.y][next.z] = 1; next.step = temp.step+1; q.push(next); } } return 0; } int main() { while(scanf("%d%d%d",&L,&R,&C)&&L!=0&&R!=0&&C!=0) { for(int i=0;i<L;i++) for(int j=0;j<R;j++) { scanf("%s",Map[i][j]); for(int k=0;k<C;k++) { if(Map[i][j][k]=='S') { sx=i; sy=j; sz=k; } else if(Map[i][j][k]=='E') { ex=i; ey=j; ez=k; } } } memset(vis,0,sizeof(vis)); ans=0; ans=bfs(); if(ans) printf("Escaped in %d minute(s). ",ans); else printf("Trapped! "); } }
Catch That Cow :http://poj.org/problem?id=3278
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<vector> #include<set> #include<map> #include<stack> #include<queue> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; int N,K; int ans; int vis[200000]; struct node { int value; int step; }; int bfs() { node temp,next; queue<node> q; temp.value=N; temp.step=0; vis[temp.value]=1; q.push(temp); while(!q.empty()) { temp=q.front(); // cout<<temp.value<<endl; q.pop(); if(temp.value==K) return temp.step; next=temp; for(int i=0;i<3;i++) { if(i==0) { next.value=temp.value-1; } if(i==1) { next.value=temp.value+1; } if(i==2) { next.value=temp.value*2; } if(next.value<0||next.value>100000) continue; if(!vis[next.value]) { // cout<<1<<endl; vis[next.value]=1; next.step=temp.step+1; q.push(next); } } } return 0; } int main() { while(scanf("%d%d",&N,&K)) { memset(vis,0,sizeof(vis)); ans=0; ans=bfs(); if(N>=K) printf("%d ",N-K); else printf("%d ",ans); } return 0; }