HDU 1242 Rescue (搜索)

[题目链接](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了。

深度优先搜索dfs

#include 
#include 
#include 
#include 
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;
}

广度优先搜索bfs+优先队列

#include 
#include 
#include 
#include 
#include 
const int maxn=202;
using namespace std;

char a[maxn][maxn];
int vis[maxn][maxn];
int n,m;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
struct node
{
	int x,y;
	int step;
	friend bool operator <(node a,node b)
	{
		return a.step>b.step;
	}
	
};
priority_queue<node>q;
int judge(int x,int y)
{
	if(x>=0&&x<n&&y>=0&&y<m&&(!vis[x][y])&&a[x][y]!='#')
		return 1;
	return 0;
}
void  bfs(node start)
{
	q.push(start);
	vis[start.x][start.y]=1;
	while(!q.empty())
	{
		node now=q.top();
		q.pop();
		for(int i=0;i<4;i++)
		{
			node nw;
			int nx,ny;
			nx=now.x+dx[i],ny=now.y+dy[i];
			if(judge(nx,ny))
			{
				vis[nx][ny]=1;
				if(a[nx][ny]=='x')
					nw.step=now.step+2;
				else if(a[nx][ny]=='r')	
				{
					cout<<now.step+1<<endl;
					return ;
				}
				else		
					nw.step=now.step+1;
				nw.x=nx,nw.y=ny;
				q.push(nw);
				
			}
		}
	}
	cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;

}
int main(int argc, char const *argv[])
{
	node start;
	while(cin>>n>>m)
	{
		memset(vis,0,sizeof(vis));
		while(!q.empty())q.pop();
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				cin >> a[i][j];
				if (a[i][j] == 'a')
				{
					start.x = i, start.y = j;
				}
			}
		}
		start.step = 0;
		bfs(start);
	}
	
	return 0;
}

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