#include <iostream> #include"stdio.h" #include"string.h" #include"queue" using namespace std; const int maxn = 100 + 5; struct node { int x,y; int step; friend bool operator<(node n1,node n2) { return n1.step>n2.step; } }; int dir[4][2]={0,1,1,0,0,-1,-1,0}; int map[maxn][maxn]; int flag[maxn][maxn]; int blood[maxn][maxn]; int n,m; int ptime; int BFS() { priority_queue<node> Q; node s; s.x = 0; s.y = 0; s.step = 0; map[0][0] = -1; Q.push(s); while(!Q.empty()) { if(Q.top().x==n-1 && Q.top().y==m-1) return Q.top().step; node next; for(int i=0;i<4;i++) { next.x = Q.top().x + dir[i][0]; next.y = Q.top().y + dir[i][1]; if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && map[next.x][next.y]!=-1) { next.step = Q.top().step+1+map[next.x][next.y]; flag[next.x][next.y] = i+1; map[next.x][next.y] = -1; Q.push(next); } } Q.pop(); } return -1; } void Path(int x,int y) { int nx_x; int nx_y; if(flag[x][y] == 0) return ; nx_x = x - dir[flag[x][y]-1][0]; nx_y = y - dir[flag[x][y]-1][1]; Path(nx_x,nx_y); printf("%ds:(%d,%d)->(%d,%d)\n",ptime++,nx_x,nx_y,x,y); while(blood[x][y]--) printf("%ds:FIGHT AT (%d,%d)\n",ptime++,x,y); } int main() { char str[maxn]; int ans; while(scanf("%d%d",&n,&m)!=EOF) { memset(map,0,sizeof(map)); memset(flag,0,sizeof(flag)); memset(blood,0,sizeof(blood)); for(int i=0;i<n;i++) { scanf("%s",str); for(int j=0;str[j];j++) { if(str[j]=='.') map[i][j]=0; else if(str[j]=='X') map[i][j]=-1; else map[i][j]=blood[i][j]=str[j]-'0'; } } ans=BFS(); //printf("AA\n"); 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); ptime=1; Path(n-1,m-1); } printf("FINISH\n"); } return 0; }
*Hdu 1026-Ignatius and the Princess I,布布扣,bubuko.com
*Hdu 1026-Ignatius and the Princess I
原文:http://www.cnblogs.com/hrhguanli/p/3793435.html