HDU 1242 Rescue (BFS(广度优先搜索))
Rescue
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.)
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.
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
思路:BFS(广度优先搜索)
import java.io.*;
import java.util.*;
public class Main {
Queue<Node> que=new LinkedList<Node>();
int n,m,sx,sy;
char ch[][];
int fx[]={1,-1,0,0};
int fy[]={0,0,1,-1};
boolean boo[][]=new boolean[202][202];
public static void main(String[] args) {
new Main().work(); }
void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
que.clear();
n=sc.nextInt();
m=sc.nextInt();
ch=new char[n][m];
for(int i=0;i<n;i++){
String s=sc.next();
ch[i]=s.toCharArray();
Arrays.fill(boo[i],false);
}
Node bode=new Node();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(ch[i][j]=='a'){
bode.x=i;
bode.y=j;
}
}
}
boo[bode.x][bode.y]=true;
bode.t=0;
que.add(bode);
BFS();
}
}
void BFS(){
while(!que.isEmpty()){
Node bode=que.poll();
if(ch[bode.x][bode.y]=='r'){
System.out.println(bode.t);
return;
}
for(int i=0;i<4;i++){
int px=bode.x+fx[i];
int py=bode.y+fy[i];
if(check(px,py)&&!boo[px][py]){
Node t1=new Node();
if(ch[px][py]=='x'){
t1.t=bode.t+2;
}
else{
t1.t=bode.t+1;
}
t1.x=px;
t1.y=py;
boo[px][py]=true;
que.add(t1);
}
}
}
System.out.println("Poor ANGEL has to stay in the prison all his life.");
}
boolean check(int px,int py){
if(px<0|px>n-1||py<0||py>m-1||ch[px][py]=='#')
return false;
return true;
}
class Node{
int x;
int y;
int t;
}
}
HDU 1242 Rescue (BFS(广度优先搜索))的更多相关文章
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- hdu 1242 Rescue (BFS)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- BFS广度优先搜索 poj1915
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...
- 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想
dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...
- 图的遍历BFS广度优先搜索
图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...
- 算法竞赛——BFS广度优先搜索
BFS 广度优先搜索:一层一层的搜索(类似于树的层次遍历) BFS基本框架 基本步骤: 初始状态(起点)加到队列里 while(队列不为空) 队头弹出 扩展队头元素(邻接节点入队) 最后队为空,结束 ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- 步步为营(十六)搜索(二)BFS 广度优先搜索
上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...
随机推荐
- 51nod1262 扔球
相关讨论里的答案:(by mint_my ) 1.反弹n次,那起点S,每次反弹点,终点S共连接n+1条边,那么原问题变为从S走n+1条边回到S,为令n=n+12.设步长为a条边,gcd(a,n)==1 ...
- 多个MapReduce作业相互依赖时,使用JobControl进行管理
要处理复杂关系的数据,一个工程里面绝对不止一个MapReduce作业,当有多个MapReduce作业时, 并且每个作业之间有依赖关系,所谓的依赖就是一个作业得到的结果是另外一个作业的输入, ...
- java基础:数据类型
一:基本数据类型 (1):整数类型 byte,short,int,long (2):浮点类型 float , double (3):布尔类型 boolean 注意: long 类型的变量后面要 ...
- 添加第三方类库造成的linker command failed with exit code 1 (use -v to see invocation)的错误调试
linker command failed with exit code 1 (use -v to see invocation)这个错误貌似遇见并不止一次,当我想用某个第三方类库的时候(如SBJso ...
- jupyterhub
pkill jupyterhub #激活python环境 pyenv activate jupyterhub #启动jupyterhub /fly/start_jupyterhub.sh cd ~/r ...
- Oracle buffer cache与相关的latch等待事件
buffer cache与相关的latch等待事件 1.buffer cache 2.latch:cache buffers lru chain 3.latch:cache buffers chain ...
- Java中HashMap的数据结构
类声明: 概述: 线程不安全: <Key, Value>两者都可以为null: 不保证映射的顺序,特别是它不保证该顺序恒久不变: HashMap使用Iterator: HashMap中ha ...
- 使用JQuery Mobile实现手机新闻浏览器
jQuery Mobile项目是jQuery项目下的在移动开发方面的又一力作,在本文中,笔者将带你一步步更深入学习使用jQuery Mobile框架去实现一个能在android手机上运行的新闻浏览器, ...
- [Everyday Mathematics]20150301
设 $f(x)$ 在 $[-1,1]$ 上有任意阶导数, $f^{(n)}(0)=0$, 其中 $n$ 是任意正整数, 且存在 $C>0$, $$\bex |f^{(n)}(x)|\leq C^ ...
- yii框架AR详解
虽 然Yii DAO可以处理事实上任何数据库相关的任务,但很可能我们会花费90%的时间用来编写一些通用的SQL语句来执行CRUD操作(创建,读取,更新和删除). 同时我们也很难维护这些PHP和SQL语 ...