Some annoying questions, ideas, and how do you say this? It seems like it's not difficult, that is, the record path is annoying. I have done it before, but it should be quite different.
This code is messy, and I don't want to analyze it myself. In fact, I should write another structure to record each step and separate the initial node to avoid any changes in the process.
The initial situation, which should not be changed.
One thing worth noting is that the <overload is actually not familiar with the priority queue, so the first overload is wrong, such as the reference of the const and the form parameter, write a comparison letter first.
When I read STL, let's get started again.
#include<iostream>#include<algorithm>#include<queue>#define N 101#define M 101using namespace std;struct node{int m_t;int x,y;int pos_x;int pos_y;int cost;bool operator<( node t) const {return m_t > t.m_t;}};char map[N][M];node m_time[N][M];int n,m;int d[4][2] = {{1,0},{0,1},{0,-1},{-1,0}};bool in(int& x,int& y){return x >= 0 && x < n && y >= 0 && y < m;}bool X(int& x,int& y){return map[x][y] == 'X';}void input(){int i,j;for(i = 0;i < n;i++){for(j = 0;j < m;j++){ cin >>map[i][j];m_time[i][j].m_t = 0;if(map[i][j] == '.')m_time[i][j].cost = 1;else if(map[i][j] == 'X')m_time[i][j].cost = 0;elsem_time[i][j].cost = map[i][j]-'0'+1;}}}bool bfs(){priority_queue<node> q;node now;now.m_t = 0; now.x = 0; now.y = 0; now.pos_x = -1; now.pos_y = -1; now.cost = 1;map[0][0] = 'X';m_time[0][0] = now;q.push(now);while(!q.empty()){node t = q.top();//printf("(%d,%d,(%d))\n",t.x,t.y,t.m_t);//cout <<m_time[0][0].cost <<endl;q.pop();for(int i = 0; i < 4;i++){node next;next.pos_x = t.x;next.pos_y = t.y;next.x = t.x+d[i][0];next.y = t.y+d[i][1];next.cost = m_time[next.x][next.y].cost;if(in(next.x,next.y) && !X(next.x,next.y)){next.m_t = t.m_t + m_time[next.x][next.y].cost;m_time[next.x][next.y] = next;//cout << t.m_t << m_time[next.x][next.y].cost << m_time[next.x][next.y].m_t <<endl;q.push(next);if(next.x == n-1 && next.y == m-1){//m_time[n-1][m-1] = next;//cout << next.m_t <<endl;return true;}map[next.x][next.y] = 'X';}}}return false;}void print1(){cout <<"It takes " <<m_time[n-1][m-1].m_t <<" seconds to reach the target position, let me show you the way." <<endl;int x,y;x = n-1; y = m-1;while(x != 0 || y != 0){int now_x = m_time[x][y].pos_x;int now_y = m_time[x][y].pos_y;int _x = x;int _y = y;m_time[now_x][now_y].x = _x;m_time[now_x][now_y].y = _y;x = now_x; y = now_y;}int step = 0;while(true){//cout <<m_time[n-1][m-1].cost<<endl;if(m_time[x][y].cost == 1){cout <<++step <<"s:" <<"(" <<x <<"," <<y <<")->(" <<m_time[x][y].x <<"," <<m_time[x][y].y <<")" <<endl;int temp = x;x = m_time[x][y].x;y = m_time[temp][y].y;}while(m_time[x][y].cost > 1){cout <<++step <<"s:" <<"FIGHT AT (" <<x <<"," <<y <<")" <<endl;m_time[x][y].cost--;}if(x == n-1 && y == m-1){ cout <<"FINISH" <<endl;return ;}}}void print2(){cout <<"God please help our poor hero." <<endl <<"FINISH" <<endl;}int main(){while(cin >>n >>m){input();if(bfs())print1();else print2();}return 0;}