Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2126 Accepted Submission(s): 617
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>
include<queue>
using namespace std;
const int INF = 10000;
struct fuck
{
int i,j,hp,step;
char ch;
}map[102][102];
struct Way
{
int x,y;
}w[102][102];
int flag;
int c;
int n,m;
int mark[102][102];
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
int judge(int a,int b)
{
if(a>=0&&a<n&&b>=0&&b<m)
return 1;
return 0;
}
void bfs()
{
queue<fuck> Q;
fuck t;
map[0][0].step = 0;
Q.push(map[0][0]);
mark[0][0] = 1;
int i,q,p;
while(!Q.empty())
{
t = Q.front();
Q.pop();
while(mark[t.i][t.j] > 1)
{
mark[t.i][t.j] --;
t = Q.front();
Q.pop();
}
mark[t.i][t.j] = 0;
for(i=0;i<4;i++)
{
q = t.i + dir[i][0];
p = t.j + dir[i][1];
if(judge(q,p) && map[q][p].ch != 'X' && t.step+t.hp + 1 < map[q][p].hp + map[q][p].step )
{
w[q][p].x = t.i;
w[q][p].y = t.j;
map[q][p].step = t.step+t.hp + 1;
Q.push(map[q][p]);
mark[q][p] ++;
}
}
}
if(map[n-1][m-1].step != INF)
flag = 0;
}
void show_way(int a,int b)
{
if(w[a][b].x == a && w[a][b].y==b)
{
while(map[a][b].hp--)
printf("%ds:FIGHT AT (%d,%d)/n",c ++,a,b);
return ;
}
show_way(w[a][b].x,w[a][b].y);
printf("%ds:(%d,%d)->(%d,%d)/n",c++,w[a][b].x,w[a][b].y,a,b);
while(map[a][b].hp--)
printf("%ds:FIGHT AT (%d,%d)/n",c ++,a,b);
}
int main()
{
int i,j;
while(cin>>n>>m)
{
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<m;j++)
{
scanf("%c",&map[i][j].ch);
map[i][j].step = INF;
if(map[i][j].ch>'0' &&map[i][j].ch <='9')
map[i][j].hp = map[i][j].ch - '0';
else
map[i][j].hp = 0;
map[i][j].i = i;
map[i][j].j = j;
w[i][j].x = i;
w[i][j].y = j;
mark[i][j] = 0;
}
}
flag = 1;
bfs();
if( flag)
cout<<"God please help our poor hero./nFINISH/n";
else
{
printf("It takes %d seconds to reach the target position, let me show you the way./n",map[n-1][m-1].hp + map[n-1][m-1].step);
c = 1;
show_way(n-1,m-1);
cout<<"FINISH/n";
}
}
return 0;
}