hdu 1242 Rescue

Rescue

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


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
 

Author
CHEN, Xue
 

Source
 



#include<cstdio>
#include<cstdlib>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define ms(x) memset( (x),0,sizeof(x) );
using namespace std;
typedef long long int ll;
char v[210][210];
int vis[210][210];
int n,m; 
struct node{
	int x,y;
	node(int a, int b){
		x=a,y=b;
	}
};
int check(int x,int y){
	if( x < 1 || y < 1 ) return 1;
	if( x > n || y > m ) return 1;
	if( v[x][y] == '#' ) return 1;
	return 0;
}
int k[4][2]={1,0,-1,0,0,1,0,-1};
void bfs(int x,int y){
	queue<node> q;
	vis[x][y] = 0,v[x][y]='.';
	q.push(node(x,y));
	while( !q.empty() ){
		node t = q.front();
		q.pop();
		for(int i = 0;i < 4;i++){
			int tx=t.x+k[i][0];
			int ty=t.y+k[i][1];
			if( check(tx,ty) ) continue;
			if( v[tx][ty] == '.' || v[tx][ty] == 'a' ){
				if( vis[tx][ty] > vis[t.x][t.y] + 1 ){
					vis[tx][ty] = vis[t.x][t.y] + 1;
					q.push( node(tx,ty) );
				}
				continue;
			}
			else if( v[tx][ty] == 'x' ){
				if( vis[tx][ty] > vis[t.x][t.y] + 2 ){
					vis[tx][ty] = vis[t.x][t.y] + 2;
					q.push( node(tx,ty) );
				}
				continue;
			}
			else if( v[tx][ty] == 'r' ){
				v[tx][ty] = '.',vis[tx][ty] = 0;
				q.push( node(tx,ty) );
			}
		}
	}
}
int main()
{
//	freopen("E:\\workspace\\acm---C++\\acm\\1.txt","r",stdin);
	while(scanf("%d%d",&n,&m)!=EOF){
		ms(v);memset(vis,0x7f,sizeof(vis));
		for(int i = 1;i <= n;i++) scanf("%s",v[i]+1);
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m; j++){
				if( v[i][j]=='r' ){
					bfs(i,j);
					j = m+1 , i = n+1;
				}
			}
		}
		int f = 1;
		for(int i = 1;i <= n;i++){
			for(int j = 1;j <= m;j++){
				if( v[i][j] == 'a' ){
					f = 0;
					if( vis[i][j] ==  2139062143) printf("Poor ANGEL has to stay in the prison all his life.\n");
					else printf("%d\n",vis[i][j]);
				 	i = n+1,j = m+1;
				}
			}
		}
		if( f )  printf("Poor ANGEL has to stay in the prison all his life.\n");
	}
	return 0;
}


ps
1.注意地图中没有a的情况
基于python实现的粒子群的VRP(车辆配送路径规划)问题建模求解+源码+项目文档+算法解析,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 算法设计的关键在于如何向表现较好的个体学习,标准粒子群算法引入惯性因子w、自我认知因子c1、社会认知因子c2分别作为自身、当代最优解和历史最优解的权重,指导粒子速度和位置的更新,这在求解函数极值问题时比较容易实现,而在VRP问题上,速度位置的更新则难以直接采用加权的方式进行,一个常见的方法是采用基于遗传算法交叉算子的混合型粒子群算法进行求解,这里采用顺序交叉算子,对惯性因子w、自我认知因子c1、社会认知因子c2则以w/(w+c1+c2),c1/(w+c1+c2),c2/(w+c1+c2)的概率接受粒子本身、当前最优解、全局最优解交叉的父代之一(即按概率选择其中一个作为父代,不加权)。 算法设计的关键在于如何向表现较好的个体学习,标准粒子群算法引入惯性因子w、自我认知因子c1、社会认知因子c2分别作为自身、当代最优解和历史最优解的权重,指导粒子速度和位置的更新,这在求解函数极值问题时比较容易实现,而在VRP问题上,速度位置的更新则难以直接采用加权的方式进行,一个常见的方法是采用基于遗传算法交叉算子的混合型粒子群算法进行求解,这里采用顺序交叉算子,对惯性因子w、自我认知因子c1、社会认知因子c2则以w/(w+c1+c2),c1/(w+c1+c2),c2/(w+c1+c2)的概率接受粒子本身、当前最优解、全局最优解交叉的父代之一(即按概率选择其中一个作为父代,不加权)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值