标签:hdoj1072
3 3 3 2 1 1 1 1 0 1 1 3 4 8 2 1 1 0 1 1 1 0 1 0 4 1 1 0 4 1 1 0 0 0 0 0 0 1 1 1 1 4 1 1 1 3 5 8 1 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1
4 -1 13分析:0时墙 2是小明,1的地方可以走,3是出口。迷宫中有炸弹,将在6分钟后爆炸,小明需要在爆炸前逃出迷宫,其中每走一步,时间减1,遇到4可以重置炸弹时间为6,求出小明逃出迷宫的最小步数,逃不出来输出“-1”。wrong answer 代码:#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; int map[10][10]; int vis[10][10]; int n,m; int dir[4][2]={1,0,-1,0,0,1,0,-1}; int ex,ey,sx,sy; int sb; int ans; struct node{ int x; int y; int step; int minute; friend bool operator<(node a,node b) { a.step>b.step; } }; int judge(int x,int y) { if(x<0||x>=n) return 0; if(y<0||y>=m) return 0; if(map[x][y]==0) return 0; return 1; } void bfs(int c,int d) { node a,b; priority_queue<node>q; a.x=c; a.y=d; a.minute=6; a.step=0; q.push(a); while(!q.empty()) { a=q.top(); q.pop(); if(a.minute==1) continue; if(a.x==sx&&a.y==sy) { printf("%d\n",a.step); return; } // vis[a.x][a.y]=1; for(int i=0;i<4;i++) { b.x=a.x+dir[i][0]; b.y=a.y+dir[i][1]; if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&map[b.x][b.y]!=0) { if(map[b.x][b.y]==4) { b.minute=6; map[b.x][b.y]=1; } else b.minute=a.minute-1; b.step=a.step+1; q.push(b); } } } printf("-1\n"); } int main() { int t; scanf("%d",&t); while(t--) { // while(scanf("%d%d",&n,&m)!=EOF) int i,j; ans=0; scanf("%d%d",&n,&m); memset(vis,0,sizeof(vis)); sb=0; for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&map[i][j]); if(map[i][j]==2) ex=i,ey=j; if(map[i][j]==3) sx=i,sy=j; } } bfs(ex,ey); // if(!sb) // printf("%d\n",ans); // else // printf("-1\n"); } return 0; }正确代码:#include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; int map[15][15]; int dir[4][2]={1,0,-1,0,0,-1,0,1}; int n,m; int ex,ey,sx,sy; struct node{ int x; int y; int step; int time; friend bool operator<(node a,node b) { return a.step>b.step; } }; void bfs() { node a,b; priority_queue<node>q; a.x=ex; a.y=ey; a.time=6; a.step=0; q.push(a); while(!q.empty()) { a=q.top(); q.pop(); if(a.x==sx&&a.y==sy) { printf("%d\n",a.step); return; } if(a.time==1) continue; for(int i=0;i<4;i++) { b.x=a.x+dir[i][0]; b.y=a.y+dir[i][1]; if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&map[b.x][b.y]!=0) { if(map[b.x][b.y]==4) { b.time=6; map[b.x][b.y]=1; } else b.time=a.time-1; b.step=a.step+1; q.push(b); } } } printf("-1\n"); } int main() { int t; scanf("%d",&t); while(t--) { int i,j; scanf("%d%d",&n,&m); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%d",&map[i][j]); if(map[i][j]==2) ex=i,ey=j; if(map[i][j]==3) sx=i,sy=j; } } bfs(); } return 0; }
版权声明:博主情人,外人误碰!!!
标签:hdoj1072
原文地址:http://blog.csdn.net/qq_21654717/article/details/47401781