一般有个结论,问(最少)多少秒到达用BFS,问能不能(在恰当时候)到达用DFS
HDU 1241 http://acm.hdu.edu.cn/showproblem.php?pid=1241
View Code
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; int n,m; char a[150][150]; int sum; int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}}; void dfs(int i,int j) { if(i>n||j>m||i<=0||j<=0) return; for(int k=0;k<8;k++) { if(a[i+dir[k][0]][j+dir[k][1]]=='@') { a[i+dir[k][0]][j+dir[k][1]]='*'; dfs(i+dir[k][0],j+dir[k][1]); } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(a,0,sizeof(a)); getchar(); sum=0; if(n==0&&m==0) break; int i,j; for(i=1;i<=n;i++) for(j=1;j<=m;j++) cin>>a[i][j]; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(a[i][j]=='@') { sum++; a[i][j]='*'; dfs(i,j); } } printf("%d\n",sum); } }
HDU 1242 http://acm.hdu.edu.cn/showproblem.php?pid=1242
View Code
#include<stdio.h> int N,M; char map[200][200]; char c; int ai,aj,ri,rj; int mintime[200][200]; int move[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int inarea(int x,int y) { return x>0 && y>0 && x<=N && y<=M && map[x][y]!='#'; } void dfs(int x,int y,int time) { mintime[x][y]=time; for(int i=0; i<4; i++) { if(inarea(x+move[i][0],y+move[i][1]) && time+1+(map[x+move[i][0]][y+move[i][1]]=='x'?1:0)<mintime[x+move[i][0]][y+move[i][1]]) { dfs(x+move[i][0],y+move[i][1],time+1+(map[x+move[i][0]][y+move[i][1]]=='x'? 1:0)); } } } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(scanf("%d%d",&N,&M)!=EOF) { for (int i=1; i<=N; i++) { for (int j=1; j<=M; j++) { mintime[i][j]=100000000; } } for (int i=1; i<=N; i++) { for (int j=1; j<=M; j++) { c=getchar(); if (c=='a') { ai=i; aj=j; map[i][j]='.'; } else if (c=='r') { ri=i; rj=j; map[i][j]='.'; } else { map[i][j]=c; } } getchar(); } dfs(ri,rj,0); if(mintime[ai][aj]==100000000) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",mintime[ai][aj]); } //fclose(stdin); //fclose(stdout); return 0; }
HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312
View Code
#include<stdio.h> #include<iostream> #include<string.h> #include<queue> using namespace std; int n,m; char a[50][50]; int si,sj; int sum; int move[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; struct node { int x; int y; }w,ww; void bfs() { queue<node>q; w.x=si; w.y=sj; q.push(w); while(!q.empty()) { ww=q.front(); q.pop(); for(int k=0;k<4;k++) { w=ww; w.x+=move[k][0]; w.y+=move[k][1]; if(a[w.x][w.y]=='.'&&w.x>0&&w.y>0&&w.x<=m&&w.y<=n) { sum++; a[w.x][w.y]='#'; q.push(w); } } } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; int i,j; for(i=1;i<=m;i++) for(j=1;j<=n;j++) { cin>>a[i][j]; if(a[i][j]=='@') { si=i; sj=j; } } sum=0; bfs(); printf("%d\n",sum+1); } //fclose(stdin); //fclose(stdout); return 0; }
HDU 2717 http://acm.hdu.edu.cn/showproblem.php?pid=2717
View Code
#include<stdio.h> #include<string.h> #include<queue> using namespace std; int n,m; int vis[2500000]; int ans; struct node { int x;int step; }w,p,q; queue<node>Q; void bfs() { Q.push(w); while(!Q.empty()) { p=Q.front(); Q.pop(); if(p.x==m) { ans=p.step; return; } p.step++; if(!vis[p.x]&&p.x>0) { q=p; q.x--; Q.push(q); } if(!vis[p.x]&&p.x<m) { q=p; q.x*=2; Q.push(q); } if(!vis[p.x]) { q=p; q.x++; vis[p.x]=1; Q.push(q); } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(vis,0,sizeof(vis)); ans=0; w.x=n; w.step=0; bfs(); printf("%d\n",ans); while(!Q.empty()) Q.pop(); } return 0; }
HDU 1372 http://acm.hdu.edu.cn/showproblem.php?pid=1372
View Code
#include<stdio.h> #include<string.h> #include<iostream> #include<queue> using namespace std; int dir[8][2]={{2,1},{2,-1},{-2,-1},{-2,1},{1,2},{1,-2},{-1,2},{-1,-2}}; int map[10][10]; char n[3],m[3]; int ai,aj,bi,bj; int ans; struct node { int x; int y; int step; }w,p,q; queue<node>Q; void bfs() { while(!Q.empty()) { p=Q.front(); Q.pop(); if(p.x<=0||p.y<=0||p.x>8||p.y>8||map[p.y][p.x]) continue; if(p.x==bj&&p.y==bi) { ans=p.step; return; } p.step++; map[p.y][p.x]++; for(int k=0;k<8;k++) { q=p; q.x+=dir[k][0]; q.y+=dir[k][1]; Q.push(q); } } } int main() { while(scanf("%s%s",&n,&m)!=EOF) { aj=n[0]-'a'+1; ai=n[1]-'0'; bj=m[0]-'a'+1; bi=m[1]-'0'; ans=0; w.x=aj; w.y=ai; w.step=0; memset(map,0,sizeof(map)); Q.push(w); bfs(); printf("To get from %s to %s takes %d knight moves.\n",n,m,ans); while(!Q.empty()) Q.pop(); } return 0; }