zoj 1649 bfs
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 <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=200+10;
const int inf=99999999;
int vis[maxn][maxn];
int mp[maxn][maxn];
int sx,sy,ex,ey;
int ans;
int n,m,flag;
int dis[4][2]={-1,0,0,1,1,0,0,-1};
struct node {
int x,y;
int step;
};
int check(int x,int y) {
if(x<1||y<1||x>n||y>m) return 1;
//if(vis[x][y]) return 1;
if(mp[x][y]==0) return 1;
return 0;
}
void bfs() {
node a,next;
a.x=sx;
a.y=sy;
a.step=0;
vis[sx][sy]=0;
queue<node> q;
q.push(a);
while(!q.empty()) {
a=q.front();
// cout<<a.x<<" "<<a.y<<endl;
q.pop();
if(a.x==ex&&a.y==ey) {
if(ans>a.step) {
flag=1;
ans=a.step;
}
}
for(int i=0;i<4;i++) {
next.x=a.x+dis[i][0];
next.y=a.y+dis[i][1];
if(check(next.x,next.y))
continue;
if(mp[next.x][next.y]==1) next.step=a.step+1;
if(mp[next.x][next.y]==2) next.step=a.step+2;//bfs并不是等距的,造成某些短路被提前走过
if(vis[next.x][next.y]>a.step) {
q.push(next);
vis[next.x][next.y]=a.step;
}
}
//cout<<a.x<<" "<<a.y<<endl;
}
}
int main() {
// freopen("input.txt","r",stdin);
while(scanf("%d%d\n",&n,&m)!=EOF) {
flag=0;
char ch[300];
ans=99999999999;
memset(mp,0,sizeof(mp));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
vis[i][j]=inf;
}
for(int i=1;i<=n;i++) {
scanf("%s",ch);
for(int j=0;j<m;j++) {
if(ch[j]=='.') mp[i][j+1]=1;
if(ch[j]=='x') mp[i][j+1]=2;
if(ch[j]=='r') {
mp[i][j+1]=1;
sx=i;
sy=j+1;
}
if(ch[j]=='a') {
mp[i][j+1]=1;
ex=i;
ey=j+1;
}
}
}
bfs();
if(flag) printf("%d\n",ans);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
zoj 1649 bfs的更多相关文章
- BFS zoj 1649
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- zoj 1649 Rescue (BFS)(转载)
又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...
- ZOJ 1649:Rescue(BFS)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- ZOJ 1649 Rescue(有敌人迷宫BFS)
题意 求迷宫中从a的位置到r的位置须要的最少时间 经过'.'方格须要1s 经过'x'方格须要两秒 '#'表示墙 因为有1s和2s两种情况 须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...
- zoj 2081 BFS 延迟标记 读入问题
Mission Impossible Time Limit: 2 Seconds Memory Limit: 65536 KB ...
- zoj 1649 Rescue
BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...
- zoj 1649
#include <iostream> #include <queue> using namespace std; int n,m,s2,e2; int b[205][205] ...
- HZNU Training 1 for Zhejiang Provincial Collegiate Programming Contest
赛后总结: TJ:今天我先到实验室,开始看题,一眼就看了一道防AK的题目,还居然觉得自己能做wwww.然后金姐和彭彭来了以后,我和他们讲了点题目.然后金姐开始搞dfs,我和彭彭看榜研究F题.想了很久脑 ...
随机推荐
- python小总结2(time,os)
一.时间模块 1.1 Time time.time():获取时间戳,结果为一个浮点数 time.gtime():获取时间的元组形式 time.localtime():获取的是当地时间 time.mkt ...
- Divisor Subtraction
Description You are given an integer number nn. The following algorithm is applied to it: if n=0, th ...
- IntelliJ IDEA 安装和破解教程
1.首先下载IntelliJ IDEA,下载链接:http://www.jetbrains.com/idea/download/#section=windows:记得一定要选择UItimate版! 2 ...
- 面向对象编程思想(OOP)
本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性.七大设计原则和23种设计模式之间的关系. 软件开发中疑难问题: 软件复杂庞 ...
- leetcode python 041首个缺失正数
##限定时间复杂度O(n)num=[0,5,3,1,2,-2,4,8,5,6]num=set(num)d=1for i in range(1,len(num)+1): if d in num: ...
- mybatis支持jdk8等localdate类型
大家知道,在实体Entity里面,可以使用java.sql.Date.java.sql.Timestamp.java.util.Date来映射到数据库的date.timestamp.datetime等 ...
- 阶段01Java基础day25网络编程
26.01_网络编程(网络编程概述) A:计算机网络 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源 ...
- 以太坊Bootstrap和Kademlia算法实现逻辑简介(基于cpp-ethereum)
- 远程桌面服务当前正忙,因此无法完成您尝试执行的任务-win2008R2
远程桌面服务当前正忙,因此无法完成您尝试执行的任务,近来我服务器出现这情况, 到达主机房看主机...不可以登陆,也没有登陆框.只能关机. 在微软找到的原因是:Csrss.exe 进程和某些应用程序 ( ...
- phpstrom常用快捷键
mark一下 格式化(应设置QQ快捷键) 自动代码提示 Ctrl+Alt+L Ctrl+J 页面查找 页面查找并替换 Ctrl+F Ctrl+R 全局查找 全局查找并替换 ...