HDU 1242 Rescue (搜索 DFS)

[題目鏈接](http://acm.hdu.edu.cn/showproblem.php?pid=1242)

Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14138 Accepted Submission(s): 5135

Problem Description

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel’s friends want to save Angel. Their task is: approach Angel. We assume that “approach Angel” is to get to the position where Angel stays. When there’s a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

Input

First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. “.” stands for road, “a” stands for Angel, and “r” stands for each of Angel’s friend.

Process to the end of the file.

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing “Poor ANGEL has to stay in the prison all his life.”

Sample Input
7 8 #.#####. #.a#…r. #…#x… …#…#.# #…##… .#… …

Sample Output
13

題意:天使被困在監獄,他的朋友們想見他,監獄的地形複雜,包括路(用點標示),牆(用#標示),天使的位置(用a標示),他的朋友(用r標示),監獄裏還有守衛(用x標示),他的朋友只能向左右上下四個方向走,走以不花一單位時間,若碰上守衛,消滅守衛需要額外花費一單位時間。問最少多長時間天使能見到他的朋友。

本題需要注意的是,天使的朋友可能不只一個,所以,應該從天使的位置開始搜去找其朋友就ok了。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;

int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
char Map[201][201];
int vis[201][201];
int n,m,ans,k;
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&(Map[x][y]=='.'))
        return 1;
    else if(x>=0&&x<n&&y>=0&&y<m&&(Map[x][y]=='x'))
        return 2;
    else if(x>=0&&x<n&&y>=0&&y<m&&(Map[x][y]=='r'))
        return 3;//結束
    return 0;

}
void dfs(int x,int y,int step)
{
    if(judge(x,y)==3)
    {
        if(!k)//k用來標記
            ans=step,k=1;//先找到一條通向隊友的路(r)
        else
            ans=min(ans,step);//回溯,找步數最小值
        return ;
    }
    for(int i=0;i<4;i++)
    {
        int nx=x+dx[i],ny=y+dy[i];
        if(judge(nx,ny)&&!vis[nx][ny])
        {
            vis[nx][ny]=1;
            dfs(nx,ny,step+judge(nx,ny));//最後要減掉2
            vis[nx][ny]=0;//注意回溯清零標記
        }
    }
}
int main()
{
        
    int i,j,x,y;//x,y標記a位置
    while(~scanf("%d%d",&n,&m))
    {
    	ans=0;k=0;
    	memset(Map,0,sizeof(Map));
    	memset(vis,0,sizeof(vis));
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < m; j++)
			{
				cin >> Map[i][j];
				if (Map[i][j] == 'a')
				{
					x = i, y = j;
					Map[i][j] = '.';
				}
			}
		}
		dfs(x, y, 0);
		if (!ans)//if(ans==0),則找不到r
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		else
			printf("%d\n", ans - 2 );//減2
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章