hdu 1072 Nightmare
2015-05-18 18:04
190 查看
还是bfs,主要考虑剪枝,数组标记走过时***剩余的时间,以及***延时后将4变成1
[code]#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; struct node { int x,y,s,w; }t,t0; int n,m,g[10][10],vis[10][10]; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; queue <node> q; int bfs(int sx,int sy) { int i,flag=-1; while(!q.empty()) q.pop(); t.x=sx; t.y=sy; t.s=6; t.w=0; vis[t.x][t.y]=6; q.push(t); while(!q.empty()) { t0=q.front(); q.pop(); //if(g[t0.x][t0.y]==4) t0.s=6; if(g[t0.x][t0.y]==3) { flag=t0.w; break; } for(i=0;i<4;i++) { t.x=t0.x+dx[i]; t.y=t0.y+dy[i]; t.w=t0.w+1; t.s=t0.s-1; if(g[t.x][t.y]==0||t.x<0||t.x>n||t.y<0||t.y>m||t.s==0) continue; if(g[t.x][t.y]==4) { g[t.x][t.y]=0; t.s=6; } if(vis[t.x][t.y]<t.s) { vis[t.x][t.y]=t.s; q.push(t); } } } return flag; } int main() { int i,j,sx,sy,ex,ey,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=0;i<9;i++) for(j=0;j<9;j++) vis[i][j]=0; for(i=0;i<n;i++) for(j=0;j<m;j++) { scanf("%d",&g[i][j]); if(g[i][j]==2) { sx=i; sy=j; } } printf("%d\n",bfs(sx,sy)); } return 0; }
相关文章推荐
- hdu 1072 Nightmare(BFS+回头路)
- HDU 1072 Nightmare
- hdu 1072 Nightmare (BFS)
- HDU-1072 Nightmare (bfs+贪心)
- HDU 1072 Nightmare
- HDU 1072 Nightmare
- hdu 1072 Nightmare bfs()
- hdu1072(Nightmare)bfs
- HDU_1072_Nightmare
- hdu1072 Nightmare (BFS)
- hdu 1072 Nightmare
- HDU1072-Nightmare
- HDU 1072 Nightmare
- hdu 1072 Nightmare BFS搜索(可重走)
- HDU 1072 Nightmare
- HDU-1072-Nightmare
- HDU 1072 NYOJ 483 Nightmare -- BFS+允许回头
- Hdu 1072 Nightmare[Bfs]
- hdu 1072 Nightmare(BFS法和DFS法)
- BFS:HDU-1072-Nightmare