5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX. 5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX1 5 6 .XX... ..XX1. 2...X. ...XX. XXXXX.
It takes 13 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) FINISH It takes 14 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) 14s:FIGHT AT (4,5) FINISH God please help our poor hero.FINISH
//Must so #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<cstring> #include<ctype.h> #include<queue> #include<vector> #include<set> #include<cstdio> #include<cmath> #define mem(a,x) memset(a,x,sizeof(a)) #define sqrt(n) sqrt((double)n) #define pow(a,b) pow((double)a,(int)b) #define inf 1<<29 #define NN 1000006 using namespace std; const double PI = acos(-1.0); typedef long long LL; int row,cow; char mp[104][104]; char note[104][104]; void getmap() { for (int i = 0; i < row; ++i) { scanf("%s",mp[i]); strcpy(note[i],mp[i]); } } int dx[4] = {0,0,-1,1}; int dy[4] = {-1,1,0,0}; struct Node { int x, y, t; inline bool operator < (const Node &a) const { return t > a.t;//时间小的优先出队 } }; struct Path { int x,y; } p[104][104]; //p[i][j]的前一个坐标是(p[i][j].x,p[i][j].y) int ans; bool bfs() { priority_queue<Node>q; Node s ; s.x = 0,s.y = 0,s.t = 0;//起点不会有数字 q.push(s); p[0][0].x = -1,p[0][0].y = -1; while (!q.empty()) { Node h = q.top(); q.pop(); if (h.x == row-1&&h.y == cow-1) { ans = h.t; return 1; } for (int i = 0; i < 4; ++i) { Node nx; nx.x = h.x + dx[i]; nx.y = h.y + dy[i]; if (nx.x>=0&&nx.y>=0&&nx.x<row&&nx.y<cow) { if (mp[nx.x][nx.y] == '.') { mp[nx.x][nx.y] = 'X'; nx.t = h.t+1; q.push(nx); p[nx.x][nx.y].x = h.x; p[nx.x][nx.y].y = h.y; } if (isdigit(mp[nx.x][nx.y])) { nx.t = h.t+1+mp[nx.x][nx.y]-48; q.push(nx); mp[nx.x][nx.y] = 'X'; p[nx.x][nx.y].x = h.x; p[nx.x][nx.y].y = h.y; } } } } return 0; } int kas; void print(int x,int y) { if (x == 0&& y == 0) return ; print(p[x][y].x,p[x][y].y); printf("%ds:(%d,%d)->(%d,%d)\n",kas++,p[x][y].x,p[x][y].y,x,y); if (isdigit(note[x][y])) { int w = note[x][y] - 48; for (int i = 0;i < w;++i) printf("%ds:FIGHT AT (%d,%d)\n",kas++,x,y); } } void test() { int i = row-1,j = cow-1; while (1) { if (i==0&&j==0) break; cout<<i<<"---"<<j<<endl; int xx = p[i][j].x, yy = p[i][j].y; cout<<xx<<"???"<<yy<<endl; i = xx,j = yy; } } int main() { while (cin>>row>>cow) { getmap(); if (bfs()) { //test(); printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans); kas = 1; print(row-1,cow-1); } else { puts("God please help our poor hero."); } puts("FINISH"); } return 0; }