#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<queue> using namespace std; char map[105][105]; int mapx[105][105]; int mapy[105][105]; int visit[105][105]; int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; int n,m; struct node { int x,y; int time; friend bool operator < (const node &a,const node &b) { return a.time>b.time; } }; int check(int x,int y) { if(x>=0&&y>=0&&y<m&&x<n&&map[x][y]!='X') return 1; return 0; } void dfs(int x,int y,int num) { int i; if(mapx[x][y]+mapy[x][y]) { if(map[x][y]=='.') dfs(mapx[x][y],mapy[x][y],num-1); else dfs(mapx[x][y],mapy[x][y],num-1-(map[x][y]-'0')); } if(map[x][y]=='.') printf("%ds:(%d,%d)->(%d,%d)\n",num,mapx[x][y],mapy[x][y],x,y); else { printf("%ds:(%d,%d)->(%d,%d)\n",num-map[x][y]+'0',mapx[x][y],mapy[x][y],x,y); for(i=map[x][y]-'0';i>=1;i--) printf("%ds:FIGHT AT (%d,%d)\n",num-i+1,x,y); } } int bfs() { int i; node st,ed; priority_queue<node>que; memset(visit,0,sizeof(visit)); st.x=0; st.y=0; st.time=0; visit[0][0]=1; que.push(st); while(!que.empty()) { st=que.top(); que.pop(); if(st.x==n-1&&st.y==m-1) return st.time; for(i=0;i<4;i++) { ed.x=st.x+dir[i][0]; ed.y=st.y+dir[i][1]; if(check(ed.x,ed.y)&&!visit[ed.x][ed.y]) { mapx[ed.x][ed.y]=st.x; mapy[ed.x][ed.y]=st.y; visit[ed.x][ed.y]=1; if(map[ed.x][ed.y]=='.') ed.time=st.time+1; else ed.time=st.time+map[ed.x][ed.y]-'0'+1; que.push(ed); } } } return -1; } int main() { int i,j,x,y,ans; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++) scanf("%s",map[i]); memset(mapx,0,sizeof(mapx)); memset(mapy,0,sizeof(mapy)); 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); dfs(n-1,m-1,ans); } printf("FINISH\n"); } return 0; }