Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17930 Accepted Submission(s): 5755
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.
.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.
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
//0MS 1688K 1977B G++
#include<iostream>
#include<queue>
#include<algorithm>
#include<string.h> using namespace std;
const int MAXN = 0xffffff; struct Node{
int x;
int y;
int step;
char c;
};
int n,m, ans;
int mov[][]={,,,,,-,-,};
int nxt[][];
int map[][];
char g[][]; void bfs(int sx, int sy)
{
queue<Node>Q;
Node node;
if(g[sx][sy] != 'X'){
node.x=sx;
node.y=sy;
node.step=;
node.c = g[sx][sy];
Q.push(node);
map[sx][sy]=-;
}
while(!Q.empty()){
node = Q.front();
Q.pop();
if(node.x == n- && node.y==m-){
if(ans > node.step){
ans = node.step + ((g[node.x][node.y]=='.')?:(g[node.x][node.y]-''));
}
break;
}
node.step += ; if(node.c != '.' && node.c != ''){
node.c -= ;
Q.push(node);
continue;
}
for(int i=;i<;i++){
int tx = node.x + mov[i][];
int ty = node.y + mov[i][]; if(tx>= && tx<n && ty>= && ty<m && g[tx][ty]!='X' && map[tx][ty]!=-){
Node tnode = {tx, ty, node.step, g[tx][ty]};
Q.push(tnode);
map[tx][ty] = -;
nxt[tx][ty] = i;
}
}
}
} void print(int x, int y, int sec)
{ if(sec <= ) return;
int id = nxt[x][y]; int use = (g[x][y]=='.')?:(g[x][y]-'');
print(x-mov[id][], y-mov[id][], sec--use); if(sec- use > )
printf("%ds:(%d,%d)->(%d,%d)\n", sec-use, x-mov[id][], y-mov[id][], x, y);
if(g[x][y]!='X'){
for(int i=use-;i>=;i--){
printf("%ds:FIGHT AT (%d,%d)\n", sec-i, x, y);
}
} } int main()
{
while(scanf("%d%d",&n,&m)!=EOF){ for(int i=;i<n;i++){
scanf("%s", &g[i]);
} memset(map, , sizeof(map));
memset(nxt, , sizeof(nxt));
ans = MAXN;
bfs(, ); if(ans != MAXN){
printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
print(n-, m-, ans);
}else{
puts("God please help our poor hero.");
}
puts("FINISH");
}
return ;
}