2013
11-26

# Ignatius and the Princess I

The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166′s castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166′s room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

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.

The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

For each test case, you should output "God please help our poor hero." if Ignatius can’t reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.

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
FINISH

#include <iostream>
#include <queue>
using namespace std;
const int MAX = 99999999;
int n,m;
struct st
{
char cc;
int num,x,y,prex,prey;
}map[105][105];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
void outPut()
{
int x=0,y=0,a,b,num=1,i;
if(map[0][0].num!= MAX)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",map[0][0].num);
while(x!= n-1| y!=m-1)
{
a=map[x][y].prex;
b=map[x][y].prey;
if(map[x][y].cc!='.')
for(i=0;i<map[x][y].cc - '0';i++)
printf("%ds:FIGHT AT (%d,%d)\n",num++,x,y);
printf("%ds:(%d,%d)->(%d,%d)\n",num++,x,y,a,b);
x=a; y=b;
}
if(map[x][y].cc!='.')
{
for(i=0;i<map[x][y].cc - '0';i++)
printf("%ds:FIGHT AT (%d,%d)\n",num++,x,y);
}
printf("FINISH\n");
}
else
{
}
}

void bfs()
{
int temp;
struct st fir;
queue<st> qu;
map[n-1][m-1].num=0;
if(map[n-1][m-1].cc>='1'&&map[n-1][m-1].cc<='9')
map[n-1][m-1].num=map[n-1][m-1].cc - '0';
qu.push(map[n-1][m-1]);
while(!qu.empty())
{
fir=qu.front();
for(int i=0;i<4;i++)
{
int tx=fir.x+dir[i][0],ty=fir.y+dir[i][1];
if(tx<0||ty<0||tx>=n||ty>=m||map[tx][ty].cc=='X') continue;
if(map[tx][ty].cc!='.')
temp=fir.num + map[tx][ty].cc-'0';
else
temp=fir.num;
if(temp+1<map[tx][ty].num)
{
map[tx][ty].num=temp+1;
map[tx][ty].prex=fir.x;
map[tx][ty].prey=fir.y;
qu.push(map[tx][ty]);
}
}
qu.pop();
}
outPut();
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%c",&map[i][j].cc);
map[i][j].num = MAX;
map[i][j].x=i;
map[i][j].y=j;
map[i][j].prey=map[i][j].prex=-1;
}
getchar();
}
bfs();
}
}



1. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同，其树的前、中、后序遍历是相同的，但在此处不能使用中序遍历，因为，中序遍历的结果就是排序的结果。经在九度测试，运行时间90ms，比楼主的要快。