标签:second for lse none print while event The ret
迷宫题
优先队列,然后输出路径
wa的代码,还没找到错哪里
先存起来把,等哪天心情好再改
#include <bits/stdc++.h> using namespace std; const int MAXN=110; char g[MAXN][MAXN]; int b[MAXN][MAXN]; int n,m; int dx[4]={0,0,-1,1}; int dy[4]={1,-1,0,0}; bool vis[MAXN][MAXN]; struct node{ int x,y,c; friend bool operator <(node a, node b){ return a.c > b.c; } }; int a[MAXN][MAXN]; priority_queue<node>que; void init(){ for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ vis[i][j]=false; a[i][j]=-1; } } while(!que.empty())que.pop(); } int bfs(){ int sx=0,sy=0,gx=n-1,gy=m-1; node aa,tt; aa.x=sx;aa.y=sy;aa.c=0; init(); que.push(aa); while(!que.empty()){ aa=que.top();que.pop(); if(aa.x==gx&&aa.y==gy){ return aa.c; } if(vis[aa.x][aa.y])continue; vis[aa.x][aa.y]=true; for(int i=0;i<4;i++){ int nx=aa.x+dx[i]; int ny=aa.y+dy[i]; if(nx<0||ny<0||nx>=n||ny>=m||vis[nx][ny]||g[nx][ny]==‘X‘)continue; tt.x=nx;tt.y=ny; if(g[nx][ny]==‘.‘)tt.c=aa.c+1; else tt.c=aa.c+g[nx][ny]-‘0‘+1; a[nx][ny]=i;//记录方向 que.push(tt); } } return -1; } int tot; void find_road(int x,int y){ if(x==0&&y==0)return ; find_road(x-dx[a[x][y]],y-dy[a[x][y]]); printf("%ds:(%d,%d)->(%d,%d)\n",++tot,x-dx[a[x][y]],y-dy[a[x][y]],x,y); for(int i=0;i<b[x][y];i++){ printf("%ds:FIGHT AT (%d,%d)\n",++tot,x,y); } } int main(){ while(~scanf("%d%d",&n,&m)){ for(int i=0;i<n;i++){ scanf("%s",g[i]); for(int j=0;j<m;j++){ if((g[i][j]>=‘0‘)&&(g[i][j]<=‘9‘)){ b[i][j]=g[i][j]-‘0‘; } else b[i][j]=0; } } int ans=bfs(); if(ans==-1) printf("God please help our poor hero.\n"); else{ printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans); tot=0; find_road(n-1,m-1); } printf("FINISH\n"); } return 0; }
标签:second for lse none print while event The ret
原文地址:https://www.cnblogs.com/lin1874/p/12203505.html