Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8552 Accepted Submission(s): 2548
Special Judge
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
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
#include<iostream>
using namespace std;
typedef struct infor
{
int i1;
int j1;
int f1;
}infor;
infor save[10002],save1[10002];
int l=0,l1=0;
int n,m;
char s[102][102];
int vi[102][102];
int move[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int min1;
void dfs(int x,int y,int num){
if(x==n-1&&y==m-1)
{
if(min1>num)
{
min1=num;
l1=0;
for(int mm=0;mm<l;mm++)
{
save1[l1].i1=save[mm].i1;
save1[l1].j1=save[mm].j1;
save1[l1++].f1=save[mm].f1;
}
}
}
if(num>=min1)
return ;
int i=0;
for(i=0;i<4;i++)
{
if(!vi[x+move[i][0]][y+move[i][1]])
{
if(x+move[i][0]>=0&&x+move[i][0]<n&&y+move[i][1]>=0&&y+move[i][1]<m
&&s[x+move[i][0]][y+move[i][1]]!='X')
{
int num1=1;
if(s[x+move[i][0]][y+move[i][1]]>='1'&&s[x+move[i][0]][y+move[i][1]]<='9')
{
num1=s[x+move[i][0]][y+move[i][1]]-'0';
for(int ll=0;ll<=s[x+move[i][0]][y+move[i][1]]-'0';ll++)
{
save[l].f1=1;
save[l].i1=x+move[i][0];
save[l++].j1=y+move[i][1];
}
num1++;
}
else
{
save[l].f1=0;
save[l].i1=x+move[i][0];
save[l++].j1=y+move[i][1];
}
vi[x+move[i][0]][y+move[i][1]]=1;
dfs(x+move[i][0],y+move[i][1],num+num1);
vi[x+move[i][0]][y+move[i][1]]=0;
l=l-num1;
}
}
}
}
int main()
{
while(cin>>n>>m){
int i,j;
l=l1=0;
memset(vi,0,sizeof(vi));
min1=10000000;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
cin>>s[i][j];
int num=0;
vi[0][0]=1;
save[l].i1=0;
save[l].j1=0;
save[l++].f1=0; //是否fight
dfs(0,0,num);
int count=0;
if(min1==10000000)
cout<<"God please help our poor hero."<<endl;
else
{
cout<<"It takes "<<min1<<" seconds to reach the target position, let me show you the way."<<endl;
for(i=1;i<l1;i++)
{
if(save1[i].f1==1)
{
if(save1[i-1].f1==0)
cout<<++count<<"s:("<<save1[i-1].i1<<","<<save1[i-1].j1<<")->("<<save1[i].i1<<","<<save1[i].j1<<")"<<endl;
else
cout<<++count<<"s:FIGHT AT ("<<save1[i].i1<<","<<save1[i].j1<<")"<<endl;
}
else
cout<<++count<<"s:("<<save1[i-1].i1<<","<<save1[i-1].j1<<")->("<<save1[i].i1<<","<<save1[i].j1<<")"<<endl;
}
}
cout<<"FINISH"<<endl;
}
return 0;
}
#include<stdio.h>
#include<queue>
using namespace std;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int n,m;
int step[101][101];
char map[101][101];
struct node
{
int x,y,time;
int px,py;
};
queue<node>q;
node r,g,s,a[101][101],b[1001];
int check(int x,int y)
{
if(x<0||x>=n||y<0||y>=m)
return 1;
return 0;
}
void bfs()
{
int x,y,i,time;
while(!q.empty())
q.pop();
r.x=0;
r.y=0;
r.time=0;
step[r.x][r.y]=0;
q.push(r);
while(!q.empty())
{
s=q.front();
q.pop();
for(i=0;i<4;i++)
{
x=s.x+dx[i];
y=s.y+dy[i];
time=s.time;
if(check(x,y)||map[x][y]=='X')
continue;
if(map[x][y]=='.')
{
g.x=x;
g.y=y;
g.time=time+1;
if(g.time<step[x][y])
{
step[x][y]=g.time;
a[x][y].px=s.x;
a[x][y].py=s.y;
q.push(g);
}
}
if(map[x][y]>='1'&&map[x][y]<='9')
{
int t=map[x][y]-'0';
g.x=x;
g.y=y;
g.time=time+1+t;
if(g.time<step[x][y])
{
step[x][y]=g.time;
a[x][y].px=s.x;
a[x][y].py=s.y;
q.push(g);
}
}
}
}
}
void solve()
{
int x,y,u,v,i,j,t;
x=n-1;
y=m-1;
i=0;
while(x+y!=0)
{
b[i].time=step[x][y];
b[i].x=x;
b[i].y=y;
i++;
u=a[x][y].px;
v=a[x][y].py;
x=u;
y=v;
}
b[i].x=0;
b[i].y=0;
b[i].time=step[0][0];
for(j=i-1;j>=0;j--)
{
if(map[b[j].x][b[j].y]!='.')
{
t=b[j+1].time+1;
printf("%ds:(%d,%d)->(%d,%d)\n",t,b[j+1].x,b[j+1].y,b[j].x,b[j].y);
t++;
while(t<=b[j].time)
{
printf("%ds:FIGHT AT (%d,%d)\n",t,b[j].x,b[j].y);
t++;
}
}
else
{
printf("%ds:(%d,%d)->(%d,%d)\n",b[j].time,b[j+1].x,b[j+1].y,b[j].x,b[j].y);
}
}
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
step[i][j]=1001;
}
}
bfs();
if(step[n-1][m-1]==1001)
{
printf("God please help our poor hero.\n");
}
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",step[n-1][m-1]);
solve();
}
printf("FINISH\n");
}
return 0;
}