#2
yangfrancis2016-01-07 12:38
|
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.
Input
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.
Output
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.
Sample Input
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.
Sample Output
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 <string>
#include <queue>
#include <fstream>
#include <stack>
using namespace std;
struct node{
int x, y;//保存改node的坐标,输出时用的
int fight;//代表与怪物的战斗秒数
bool visited;//表示此node是否被访问过,1代表访问过
node* pre;//指向此node的前驱node, 因为输出时要输出最短路径
int g, h, f;//g代表走的step,h代表当前node与终点的距离(忽略障碍物),f=g+h
bool operator <(const node& kk)const{//优先队列插入时的比较函数
return f > kk.f;
}
};
struct Point{
int x, y;
};
int n, m;
node map[111][111];//地图
priority_queue<node> qk;//保存还没访问过的node
stack<Point> sp;//输出时用的
bool in(const node& kk){//判断node是否越界
return !(kk.x < 0 || kk.x >= n || kk.y < 0 || kk.y >= m);
}
int manha(const node& kk){//计算当前节点与终点的距离(忽略障碍物)
return n - 1 - kk.x + m - 1 - kk.y;
}
int dirs[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };//可以走的四个方向
int min(int a, int b){
return a < b ? a : b;
}
bool Astar(){
node t, s;
map[0][0].pre = 0;
bool flag = 0;
while (!qk.empty()){
t = qk.top();
qk.pop();
map[t.x][t.y].visited = 1;
for (int i = 0; i < 4; ++i){
s.x = t.x + dirs[i][0];
s.y = t.y + dirs[i][1];
if (in(s) && !map[s.x][s.y].visited){
s.g = t.g + 1;
s.h = manha(s);
s.f = s.g + s.h + map[s.x][s.y].fight;
if (s.f <= map[s.x][s.y].f){
map[s.x][s.y].pre = &map[t.x][t.y];
map[s.x][s.y].f = s.f;
}
if (s.x == n - 1 && s.y == m - 1)
flag = 1;
qk.push(s);
}
}
}
return flag;
}
void show(){
while (!sp.empty()){
Point old = sp.top();
sp.pop();
Point now = old;
int time = 1;
while (!sp.empty()){
old = now;
now = sp.top();
sp.pop();
if (map[old.x][old.y].fight == 0)
printf("%ds:(%d,%d)->(%d,%d)\n", time++, old.x, old.y, now.x, now.y);
else{
while (map[old.x][old.y].fight--)
printf("%ds:FIGHT AT (%d,%d)\n", time++, old.x, old.y);
printf("%ds:(%d,%d)->(%d,%d)\n", time++, old.x, old.y, now.x, now.y);
}
}
while (map[n - 1][m - 1].fight--)
printf("%ds:FIGHT AT (%d,%d)\n", time++, n - 1, m - 1);
}
}
int main(){
string ss;
node ok;
while (cin >> n >> m){
int i = 0;
memset(map, 0, 111 * 111 * sizeof(node));
while (i < n){
cin >> ss;
for (int j = 0; j < m; ++j){
if (ss[j] == '.'){
map[i][j].x = i;
map[i][j].y = j;
map[i][j].visited = 0;
map[i][j].fight = 0;
map[i][j].f = 9000000;
map[i][j].g = 0;
}
else if (ss[j] == 'X'){
map[i][j].x = i;
map[i][j].y = j;
map[i][j].visited = 1;
map[i][j].f = 9000000;
map[i][j].g = 0;
}
else{
map[i][j].x = i;
map[i][j].y = j;
map[i][j].fight = ss[j] - '0';
map[i][j].visited = 0;
map[i][j].f = 90000000;
map[i][j].g = 0;
}
}
++i;
}
while (!qk.empty()) qk.pop();
while (!sp.empty()) sp.pop();
ok.x = ok.y = 0;
ok.g = 0;
ok.h = manha(ok);
ok.f = ok.g + ok.h;
qk.push(ok);
if (Astar())
{
int sum = 0;
Point p;
p.x = n - 1;
p.y = m - 1;
node* temp = &map[n - 1][m - 1];
while (temp->pre){
p.x = temp->x;
p.y = temp->y;
sp.push(p);
sum += temp->fight + 1;
temp = temp->pre;
}
p.x = temp->x;
p.y = temp->y;
sp.push(p);
printf("It takes %d seconds to reach the target position, let me show you the way.\n", sum);
show();
}
else
printf("God please help our poor hero.\n");
printf("FINISH\n");
}
return 0;
}