hdu 1072 Nightmare (广搜)
2013-06-11 16:27
351 查看
// Time 0ms, Memory 356K
#include<iostream> #include<cstdio> #include<queue> using namespace std; int n,m,p[9][9],sx,sy,dx[]={0,1,0,-1},dy[]={1,0,-1,0}; struct point { int x,y,time,step; point(int x=0,int y=0,int time=0,int step=0):x(x),y(y),time(time),step(step){} }; int bfs() { point s(sx,sy,6,0),t; queue<point>q; int nx,ny,i; q.push(s); while(!q.empty()) { s=q.front();q.pop(); for(i=0;i<4;i++) { nx=s.x+dx[i];ny=s.y+dy[i]; t=point(nx,ny,s.time-1,s.step+1); if(t.x<0 || t.x>=n || t.y<0 || t.y>=m || !p[nx][ny] || t.time==0) continue; if(p[nx][ny]==3) return t.step; if(p[nx][ny]==4) { t.time=6;p[nx][ny]=0; } q.push(t); } } return -1; } int main() { int i,j,c; cin>>c; while(c--) { scanf("%d%d",&n,&m); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&p[i][j]); if(p[i][j]==2) { sx=i;sy=j;p[i][j]=0; } } } printf("%d\n",bfs()); } return 0; }
相关文章推荐
- HDU 1072 Nightmare
- Hdu 1072 Nightmare[Bfs]
- HDU_1072_Nightmare
- BFS:HDU-1072-Nightmare
- HDU 1072 Nightmare( 身上带有定时炸弹的他能否在炸弹爆炸之前离开—— BFS+DP思想)
- hdu 1072 Nightmare
- 搜索专题(DFS)HDU 1072-Nightmare
- HDU 1072 Nightmare
- hdu 1072 Nightmare (BFS)
- HDU-1072 Nightmare (bfs+贪心)
- HDU 1072:Nightmare(BFS)
- hdu 1072 Nightmare(BFS法和DFS法)
- HDU 1072 Nightmare(搜索-DFS)
- HDU 1072 Nightmare
- hdu 1072 Nightmare
- HDU1072 Nightmare(广搜BFS+优先队列)
- HDU 1072 Nightmare
- hdu 1072 Nightmare BFS搜索(可重走)
- HDU-1072-Nightmare
- HDU 1072 NYOJ 483 Nightmare -- BFS+允许回头