HDU1026:Ignatius and the Princess I

点击打开题目链接

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8688    Accepted Submission(s): 2590
Special Judge


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
 

Author
Ignatius.L
 


=====================================题目大意=====================================


公主被魔王抓走了,勇士挺身而出前往魔王城堡拯救公主。

魔王城堡是规格为N*M(左上角为(0,0),右下角为(N-1,M-1))的迷宫,勇士一开始在左上角,而恶魔卧室的门在右下角。

勇士可以选择往上下左右四个方向行走,每行走一次需要花费1秒钟的时间。

城堡里有可以走的地方(“.”),有不可以走的地方(“X”),还有生命值为n(“n”)的怪物(这意味着勇士需要花费n秒钟来消

灭这个怪物)。

找出勇士前往恶魔卧室门的最短时间并输出选择最短路线行走时的详细情况。


=====================================算法分析=====================================


搜索题,用BFS+优先队列的话倒不如直接Dijkstra搜索。


=====================================数据处理=====================================


直接使用字符地图的话判断时较繁琐,因此把字符地图转换为相应的数字地图 从邻接点行走到这个点需要花费的时间):
‘.’转换为1,‘X’转换为INF,‘n’转换为n+1。

=======================================代码=======================================




#include<queue>
#include<cctype>
#include<cstdio>
#include<cstring>

using namespace std;

const int INF1=0x1f;                                            //单字节整形最大值
const int INF4=0x1f1f1f1f;                                      //四字节整形最大值
const int MAXS=105;
const int Dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

int N,M,Map[MAXS][MAXS],Time[MAXS][MAXS];

bool Vis[MAXS][MAXS];

struct NODE
{
	NODE(int IniR,int IniC,int IniT) 
	{
		R=IniR;  C=IniC;  T=IniT;
	}
    friend bool operator < (const NODE& A,const NODE& B) 
	{
		return A.T>B.T;
	}
    int R,C,T;
};

void Dijkstra()                                                 
{
	memset(Vis,0,sizeof(Vis));
	memset(Time,INF1,sizeof(Time));
	priority_queue<NODE>q;  
	q.push(NODE(0,0,Time[0][0]=0));
    while(!q.empty())
    {
        NODE cur=q.top();  q.pop();
        if(cur.R==N-1&&cur.C==M-1) { return; }
		if(Vis[cur.R][cur.C]) { continue; }
		Vis[cur.R][cur.C]=1;
        for(int n=0;n<4;++n)
        {
			int tmprow=cur.R+Dir[n][0];
			int tmpcol=cur.C+Dir[n][1];
			if((0<=tmprow&&tmprow<N)&&(0<=tmpcol&&tmpcol<M)) 
			{
				if(Time[cur.R][cur.C]<Time[tmprow][tmpcol]-Map[tmprow][tmpcol])
				{
					Time[tmprow][tmpcol]=Time[cur.R][cur.C]+Map[tmprow][tmpcol];
					q.push(NODE(tmprow,tmpcol,Time[tmprow][tmpcol]));
				}
			}
        }
    }
}

void OutPath(int CurR,int CurC)                                         
{
    for(int n=0;n<4;++n)                                       
    {
		int tmprow=CurR+Dir[n][0];
		int tmpcol=CurC+Dir[n][1];
		if((0<=tmprow&&tmprow<N)&&(0<=tmpcol&&tmpcol<M)) 
		{
			if(Time[tmprow][tmpcol]==Time[CurR][CurC]-Map[CurR][CurC])
			{
				if(!(tmprow==0&&tmpcol==0))
				{
					OutPath(tmprow,tmpcol);
				}
				printf("%ds:(%d,%d)->(%d,%d)\n",Time[tmprow][tmpcol]+1,tmprow,tmpcol,CurR,CurC); 
				for(int t=1;Time[tmprow][tmpcol]+1+t<=Time[CurR][CurC];++t)
				{
					printf("%ds:FIGHT AT (%d,%d)\n",Time[tmprow][tmpcol]+1+t,CurR,CurC);
				}
				break;
			}
		}
	}
}

void ReaDate()                                
{
    char tmp[MAXS];
    for(int i=0;i<N;++i)                                        
    {
        scanf("%*c%s",tmp);
        for(int j=0;j<M;++j)
        {
            if(isdigit(tmp[j]))  Map[i][j]=1+tmp[j]-'0';
            else if(tmp[j]=='.') Map[i][j]=1;
            else if(tmp[j]=='X') Map[i][j]=INF4;
        }
	}
}

int main()
{
    while(scanf("%d%d",&N,&M)==2)
    {
        ReaDate();
        Dijkstra();
        if(Time[N-1][M-1]<INF4)
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",Time[N-1][M-1]);
            OutPath(N-1,M-1);
        }
        else
		{
			printf("God please help our poor hero.\n");
        }
		printf("FINISH\n");
    }
    return 0;
}

你可能感兴趣的:(搜索,bfs)