一道打印路径的题目 :学习了 第一次做这种题型
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<queue>
using namespace std;
char map[101][101];
int minTime[101][101];
int roat[4][2]= {0,1,1,0,0,-1,-1,0};
int n,m;
int head,last;
struct Node
{
int x;
int y;
int time;
int pre;
} no;
Node node[1001000];
void BFS(int idx,int idy)
{
Node tem,root;
tem.x=idx;
tem.y=idy;
tem.pre=-1;
minTime[idx][idy]=tem.time=0;
if(map[idx][idy]!='.'&&map[idx][idy]!='X')
minTime[idx][idy]=tem.time=map[idx][idy]-'0';
head=0;//.XX.1.Xx
last=1;
node[0]=tem;
while(head!=last)
{
root=node[head++];
for(int i=0; i<4; i++)
{
idx=root.x+roat[i][0];
idy=root.y+roat[i][1];
if(idx>=0&&idx<n&&idy>=0&&idy<m&&map[idx][idy]!='X')
{
tem.time=root.time+1;
if(map[idx][idy]!='.')
tem.time+=map[idx][idy]-'0';
if(tem.time<minTime[idx][idy])
{
minTime[idx][idy]=tem.time;
tem.x=idx;
tem.y=idy;
tem.pre=head;
node[last++]=tem;
}
}
}
}
}
int main()
{
Node tem1,tem2;
int t;
while(scanf("%d%d",&n,&m)!=EOF)
{
t=0;
memset(node,0,sizeof(node));
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
cin>>map[i][j];
minTime[i][j]=99999;
}
if(map[n-1][m-1]=='X')
{
printf("God please help our poor hero.\n");
printf("FINISH\n");
continue;
}
BFS(n-1,m-1);
if(minTime[0][0]==99999)
{
printf("God please help our poor hero.\n");
printf("FINISH\n");
continue;
}
/* for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
printf("%d ",minTime[i][j]);
printf("\n");
}
for(int i=0; i<30; i++)
printf("x=%d y=%d pre=%d\n",node[i].x,node[i].y,node[i].pre);
*/
tem1=node[head-1];
while(tem1.x!=0||tem1.y!=0)
while(tem1.x!=0||tem1.y!=0)
{
tem1=node[--head];
}
printf("It takes %d seconds to reach the target position, let me show you the way.\n",minTime[0][0]);
while(tem1.pre>=0)
{
tem2=node[tem1.pre-1];
printf("%ds:(%d,%d)->(%d,%d)\n",++t,tem1.x,tem1.y,tem2.x,tem2.y);
//printf("dfgd\n");
if(map[tem2.x][tem2.y]!='.'&&map[tem2.x][tem2.y]!='X')
for(int i=0; i<map[tem2.x][tem2.y]-'0'; i++)
printf("%ds:FIGHT AT (%d,%d)\n",++t,tem2.x,tem2.y);
tem1=tem2;
}
printf("FINISH\n");
}
return 0;
}