题意:
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18410 Accepted Submission(s):
5929
Special Judge
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
#define inf 0x3f3f3f3f
char e[105][105];
int /*book[105][105],*/dis[105][105];
int n,m,sumn;
int fx[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int flag;
struct node
{
int x,y,sum;
};
void bfs()
{
queue<node> q;
node next,cur;
node *cu;
cu=&cur;
cu->x=cu->y=cu->sum=0;
//cur.x=cur.y=cur.sum=0;
if (e[0][0]!='.') cu->sum=e[0][0]-'0';
dis[0][0]=cu->sum;
q.push(*cu);
while(!q.empty()){
for (int i=0;i<4;i++){*cu=q.front();
int dx=fx[i][0]+cur.x;
int dy=fx[i][1]+cur.y;
if (dx<0||dy<0||dx>=n||dy>=m||e[dx][dy]=='X') continue;
if (e[dx][dy]=='.') cu->sum+=1;
else cu->sum=cu->sum+1+e[dx][dy]-'0';
if (cu->sum<dis[dx][dy]) dis[dx][dy]=cu->sum; //更新此点最小时间值
else continue;
if (dx==n-1&&dy==m-1) {
if(cu->sum<sumn) sumn=cu->sum; //更新到目标点的最小时间
}
cu->x=dx;
cu->y=dy;
q.push(*cu);
}
q.pop();
}
}
void print(int x,int y,int sum) //逆序打印
{
int i,j,tim,k,dx,dy;
if (sum==0) return;
if (e[x][y]=='.') tim=0;
else tim=e[x][y]-'0';
for (i=0;i<4;i++){
dx=x+fx[i][0];
dy=y+fx[i][1];
if (dx<0||dy<0||dx>=n||dy>=m||e[dx][dy]=='X') continue;
if (dis[dx][dy]+1+tim==dis[x][y]) {print(dx,dy,sum-1-tim);break;}
}
if (dy>=0) printf("%ds:(%d,%d)->(%d,%d)
",++flag,dx,dy,x,y);
for (int l=1;l<=tim;l++) printf("%ds:FIGHT AT (%d,%d)
",++flag,x,y);
}
int main()
{
int i,j;
while (scanf("%d%d",&n,&m)!=EOF){sumn=inf;
flag=0;
memset(dis,inf,sizeof(dis));//getchar();
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf(" %c",&e[i][j]);
//cin>>e[i][j];
bfs();
if (sumn==inf) printf("God please help our poor hero.
");
else {
printf("It takes %d seconds to reach the target position, let me show you the way.
",sumn);
print(n-1,m-1,sumn);
}
printf("FINISH
");
}
return 0;
}