hdu1242 rescue【BFS】

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21259    Accepted Submission(s): 7586


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
 
【代码】
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
#define INF 0xfffffff
int m,n,x,y,ex,ey,ans,vis[220][220],flag;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
char map[220][220];
struct node
{
	int x,y,step;
	friend bool operator < (node a,node b)
	{
		return a.step>b.step;
	}
}a,temp;
int jud(struct node a)
{
	if(a.x<0||a.x>=n)
	   return 0;
	if(a.y<0||a.y>=m)
	   return 0;
	if(vis[a.x][a.y]||map[a.x][a.y]=='#')
	   return 0;
	if(temp.step>=ans)
	   return 0;
	return 1; 
}
void bfs()
{
	flag=0;
	a.x=x;
	a.y=y;
	a.step=0;
	memset(vis,0,sizeof(vis));
	priority_queue<node>q;
	vis[x][y]=1;
	q.push(a);
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		for(int i=0;i<4;i++)
	    {
		    temp.x=a.x+dx[i];
		    temp.y=a.y+dy[i];
		    temp.step=a.step+1;
		    if(jud(temp))
		    {
			    if(map[temp.x][temp.y]=='x')
			        temp.step++;
			    if(temp.x==ex&&temp.y==ey)
			    {
				    ans=temp.step;
				    flag=1;
				    return;
			    }
			    vis[temp.x][temp.y]=1;
			    q.push(temp);
		    }
		    
	    }
	}
	
}
int main()
{
	int i,j;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=0;i<n;i++)
	       scanf("%s",map[i]);
 	    for(i=0;i<n;i++)
	       for(j=0;j<m;j++)
	       {
	   	        if(map[i][j]=='r')
	   	            x=i,y=j;
	   	        if(map[i][j]=='a')
	   	            ex=i,ey=j;
	       }
	    if(x==ex&&y==ey)
	    {
	    	printf("0\n");
	    	continue;
		}
	    ans=INF;
	    bfs();
	    if(flag)
	       printf("%d\n",ans);
	    else 
		   printf("Poor ANGEL has to stay in the prison all his life.\n");
	    
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在现如今这个互联网以及信息技术飞速发展的时代,信息技术被广泛地运用于人们的日常生活与生产中,并有效地提高了办事、办公效率。教育信息化的高速发展,使得学校在线考试系统,作为一种新的考试管理工具,在各大院校及相关教育机构的运用日趋普遍。绝大多数的考试机构需要不断地推进自身信息化的建设,而在考务管理方面,以往手工记账、登记信息的方式,已经逐渐不能满足现如今大数据量、高效率完成的要求,使用学校在线考试系统,对学校及相关教育机构的考试事宜管理已成当下教育信息化发展的必要手段。现代化的考试机构都应该进行学校在线考试系统的替换工作,学校在线考试系统一经推出,便在考试机构范围内引起了极大地反响。 本次学校在线考试系统的实现过程,它的开发使用B/S结构即浏览器和服务器结构框架,采用SSM框架技术,数据库使用了mysql数据库,页面设计采用了MVC框架,后端采用了SSM框架技术scrip等其他一些脚本语言,使用到在大学里面学的软件工程导论课程、mysql数据库、数据库原理、SSM框架技术高级程序设计等方面的知识完成本系统。本文结合全流程网站系统开发过程,详细介绍了此次学校在线考试系统的设计理念、模型结构,将所学知识融入到本网站的开发实践中,简略介绍了研究的背景和系统存在的现实意义,开发使用的技术背景,并对系统概要设计、系统实现与系统测试等进行了详细的介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值