HDU 1026 Ignatius and the Princess I

题目链接:传送门

Problem Description

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

题目大意:公主被大魔王BEelzebub feng5166绑架了,关在一个城堡里面,我们的英雄Ignatius要去拯救公主,从(0,0)出发,到(N-1,M-1)。城堡中有一些怪物,遇见了必须要杀死它们。以下是一些规则:

1.Ignatius每秒只能向上、下、左、右四个方向移动一步。步骤定义如下:如果当前位置是(x,y),在一个步骤之后,Ignatius只能站在(x-1,y),(x+1,y),(x,y-1)或(x,y+1)上。

2.数组中有一些字符和数字。我们这样定义它们:

. :Ignatius可以行走的地方。

X:这个地方是个陷阱,Ignatius不应该走在上面。

N:这里有一个怪物的生命值为N(1<=N<=9),如果Ignatius走了,他要花N秒才能杀死这个怪物。

你的任务是给出伊格那丢到达目标位置所需的最少时间。起始位置和目标位置永远不会是陷阱,并且起始位置永远不会有怪物。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
	int x,y;
	int time;
	string route;
	friend bool operator<(node a,node b)
	{
		return a.time>b.time;
	}
}; 
char m[205][205];
char tion[4]={'s','x','z','y'};
int n[205][205];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int s,w;
bool falg;
void opt(node a)
{
	printf("It takes %d seconds to reach the target position, let me show you the way.\n",a.time);
	int x1,x2,y1,y2,t,len,i,j;
	x1=0;
	y1=0;
	t=0;
	len=a.route.size();
	for(i=0;i<len;i++)
	{
		if(a.route[i]=='s')
		{
			x2=x1+dir[0][0];
			y2=y1+dir[0][1];
			printf("%ds:(%d,%d)->(%d,%d)\n",++t,x1,y1,x2,y2);
			if(m[x2][y2]>='0'&&m[x2][y2]<='9')
			{
				j=m[x2][y2]-'0';
				while(j--)
				{
					printf("%ds:FIGHT AT (%d,%d)\n",++t,x2,y2);
				}
			}
		}
		else if(a.route[i]=='x')
		{
			x2=x1+dir[1][0];
			y2=y1+dir[1][1];
			printf("%ds:(%d,%d)->(%d,%d)\n",++t,x1,y1,x2,y2);
			if(m[x2][y2]>='0'&&m[x2][y2]<='9')
			{
				j=m[x2][y2]-'0';
				while(j--)
				{
					printf("%ds:FIGHT AT (%d,%d)\n",++t,x2,y2);
				}
			}
		}
		else if(a.route[i]=='z')
		{
			x2=x1+dir[2][0];
			y2=y1+dir[2][1];
			printf("%ds:(%d,%d)->(%d,%d)\n",++t,x1,y1,x2,y2);
			if(m[x2][y2]>='0'&&m[x2][y2]<='9')
			{
				j=m[x2][y2]-'0';
				while(j--)
				{
					printf("%ds:FIGHT AT (%d,%d)\n",++t,x2,y2);
				}
			}
		}
		else if(a.route[i]=='y')
		{
			x2=x1+dir[3][0];
			y2=y1+dir[3][1];
			printf("%ds:(%d,%d)->(%d,%d)\n",++t,x1,y1,x2,y2);
			if(m[x2][y2]>='0'&&m[x2][y2]<='9')
			{
				j=m[x2][y2]-'0';
				while(j--)
				{
					printf("%ds:FIGHT AT (%d,%d)\n",++t,x2,y2);
				}
			}
		}
		x1=x2;
		y1=y2;
	}
	printf("FINISH\n");
}
void bfs()
{
	node next,head;
	falg=false;
	head.x=0;
	head.y=0;
	head.time=0;
	head.route="";
	priority_queue<node>qu;
	qu.push(head);
	n[0][0]=1;
	while(!qu.empty())
	{
		head=qu.top();
		qu.pop();
		for(int i=0;i<4;i++)
		{
			next.x=head.x+dir[i][0];
			next.y=head.y+dir[i][1];
			next.time=head.time+1;
			next.route=head.route+tion[i];
			//cout<<next.x<<"   "<<next.y<<"    "<<next.time<<endl;
			if(next.x==s-1&&next.y==w-1)
			{
				if(m[s-1][w-1]>='1'&&m[s-1][w-1]<='9')
					next.time+=m[s-1][w-1]-'0';
				opt(next);
				return ;
			}
			if(next.x>=0&&next.x<s&&next.y>=0&&next.y<w&&!n[next.x][next.y]&&m[next.x][next.y]!='X')
			{
				n[next.x][next.y]=1;
				if(m[next.x][next.y]>='1'&&m[next.x][next.y]<='9')
					next.time+=m[next.x][next.y]-'0';
				qu.push(next);
				//cout<<next.x<<"   "<<next.y<<"    "<<next.time<<endl;
			}
		}
	}
	cout<<"God please help our poor hero."<<endl<<"FINISH" <<endl;
}
int main()
{
	int i,j;
	while(cin>>s>>w)
	{
		for(i=0;i<s;i++)
		{
			for(j=0;j<w;j++)
			{
				cin>>m[i][j];
				n[i][j]=0;
			}
		}
		bfs();
	}
	return 0;
}

 

 

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务