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

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

#include <iostream>
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <cstring>
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于计算机专业的学生而言,参加各类比赛能够带来多方面的益处,具体包括但不限于以下几点: 技能提升: 参与比赛促使学生深入学习和掌握计算机领域的专业知识与技能,如编程语言、算法设计、软件工程、网络安全等。 比赛通常涉及实际问题的解决,有助于将理论知识应用于实践中,增强问题解决能力。 实践经验: 大多数比赛都要求参赛者设计并实现解决方案,这提供了宝贵的动手操作机会,有助于积累项目经验。 实践经验对于计算机专业的学生尤为重要,因为雇主往往更青睐有实际项目背景的候选人。 团队合作: 许多比赛鼓励团队协作,这有助于培养学生的团队精神、沟通技巧和领导能力。 团队合作还能促进学生之间的知识共享和思维碰撞,有助于形成更全面的解决方案。 职业发展: 获奖经历可以显著增强简历的吸引力,为求职或继续深造提供有力支持。 某些比赛可能直接与企业合作,提供实习、工作机会或奖学金,为学生的职业生涯打开更多门路。 网络拓展: 比赛是结识同行业人才的好机会,可以帮助学生建立行业联系,这对于未来的职业发展非常重要。 奖金与荣誉: 许多比赛提供奖金或奖品,这不仅能给予学生经济上的奖励,还能增强其成就感和自信心。 荣誉证书或奖状可以证明学生的成就,对个人品牌建设有积极作用。 创新与研究: 参加比赛可以激发学生的创新思维,推动科研项目的开展,有时甚至能促成学术论文的发表。 个人成长: 在准备和参加比赛的过程中,学生将面临压力与挑战,这有助于培养良好的心理素质和抗压能力。 自我挑战和克服困难的经历对个人成长有着深远的影响。 综上所述,参加计算机领域的比赛对于学生来说是一个全面发展的平台,不仅可以提升专业技能,还能增强团队协作、沟通、解决问题的能力,并为未来的职业生涯奠定坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值