64,650
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
char map[102][102];
bool visited[102][102];
int Min;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
struct node
{
int x,y;
int fx,fy;
int step;
bool operator<(const node t)const{
return step>t.step;
}
};
node path[102][102];
node a,b,c;
int N,M;
void getmap()
{
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
cin>>map[i][j];
}
void print(int n,int m)
{
if(n==0&&m==0)
return;
print(n-path[n][m].fx,m-path[n][m].fy);
if(map[n][m]>='1'&&map[n][m]<='9')
{
int t=map[n][m]-'0';
cout<<path[n][m].step-t<<"s:"<<"("<<n-path[n][m].fx<<","<<m-path[n][m].fy<<")->("<<n<<","<<m<<")"<<endl;
while(t)
{
cout<<path[n][m].step-t+1<<"s:"<<"FIGHT AT ("<<n<<","<<m<<")"<<endl;
t--;
}
}
else
cout<<path[n][m].step<<"s:"<<"("<<n-path[n][m].fx<<","<<m-path[n][m].fy<<")->("<<n<<","<<m<<")"<<endl;
}
int main()
{
while(cin>>N>>M)
{
memset(visited,0,sizeof(visited));
priority_queue <node>q;
getmap();
a.x=0;
a.y=0;
a.fx=0;
a.fy=0;
a.step=0;
visited[0][0]=1;
q.push(a);
path[0][0]=a;
while(!q.empty())
{
b=q.top();
q.pop();
if(b.x==N-1&&b.y==M-1)
{
break;
}
for(int i=0;i<4;++i)
{
c.x=b.x+dx[i];
c.y=b.y+dy[i];
c.fx=dx[i];
c.fy=dy[i];
if(c.x>=0&&c.x<N&&c.y>=0&&c.y<M&&map[c.x][c.y]!='X'&&!visited[c.x][c.y])
{
q.push(c);
path[c.x][c.y]=c;
visited[c.x][c.y]=1;
if(map[c.x][c.y]>='1'&&map[c.x][c.y]<='9')
path[c.x][c.y].step=path[b.x][b.y].step+map[c.x][c.y]-'0'+1;
else
path[c.x][c.y].step=path[b.x][b.y].step+1;
}
}
}
/* for(int i=0;i<N;++i)
{
for(int j=0;j<M;j++)
cout<<step[i][j]<<" ";
cout<<endl;
}//测试*/
if(visited[N-1][M-1])
{
cout<<"It takes "<<path[N-1][M-1].step<<" seconds to reach the target position, let me show you the way."<<endl;
print(N-1,M-1);
}
else
cout<<"God please help our poor hero."<<endl;
cout<<"FINISH"<<endl;
}
return 0;
}