Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12441 Accepted Submission(s): 4551
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
 

思路: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(广度优先搜索))的更多相关文章

  1. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  2. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  3. hdu 1242 Rescue (BFS)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. BFS广度优先搜索 poj1915

    Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...

  5. 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想

    dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...

  6. 图的遍历BFS广度优先搜索

    图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...

  7. 算法竞赛——BFS广度优先搜索

    BFS 广度优先搜索:一层一层的搜索(类似于树的层次遍历) BFS基本框架 基本步骤: 初始状态(起点)加到队列里 while(队列不为空) 队头弹出 扩展队头元素(邻接节点入队) 最后队为空,结束 ...

  8. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  9. 步步为营(十六)搜索(二)BFS 广度优先搜索

    上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...

随机推荐

  1. org.hibernate.AnnotationException: No identifier specified for entity: cn.itcast.domain.Counter

    因为我的hibernate映射表没有主键所以报这个错. 解决方案是: 1.创建一个主键 2.hibernate处理无主键的表的映射问题,其实很简单,就是把一条记录看成一个主键,即组合主键<com ...

  2. phpstorm10.0.1和webstorm11注册

    webstorm11 for win 注册时选择“License server”输入“http://15.idea.lanyus.com/”点击“OK” phpstorm10.0.1 for win ...

  3. Android 源代码自动编译packages/apps

    /*************************************************************************** * Android 源代码自动编译packag ...

  4. (六)6.5 Neurons Networks Implements of Sparse Autoencoder

    一大波matlab代码正在靠近.- -! sparse autoencoder的一个实例练习,这个例子所要实现的内容大概如下:从给定的很多张自然图片中截取出大小为8*8的小patches图片共1000 ...

  5. YouTrack Changing Database Location for EXE Distribution(windows service)

    If you have installed YouTrack from EXE Distribution, then the best way to change the database locat ...

  6. MatrixTurn源码阅读

    在看cacheAsBitmap 相关资料时,找到bit101的一篇文章,http://www.bytearray.org/?p=290 全文如下: One of the feature I would ...

  7. mysql 数据库自增id 的总结

    有一个表StuInfo,里面只有两列 StuID,StuName其中StuID是int型,主键,自增列.现在我要插入数据,让他自动的向上增长,insert into StuInfo(StuID,Stu ...

  8. 给产品经理讲技术,不得不懂的TCP和UDP

    TCP/IP协议,你一定经常听说吧,其中TCP(Transmission Control Protocol)称为传输控制协议,IP(Internet Protocol)称为因特网互联协议,好吧,这都是 ...

  9. 深入了解 Oracle Flex ASM 及其优点

    简介 Oracle Real Application Cluster (RAC) 是 Oracle 解决方案中的一个著名产品,用于保持业务数据的高可用性.Oracle RAC 允许在所有集群节点之间共 ...

  10. Markdown解决需要输入两个回车才能为一个空行的问题

    markdownDataDiv.children().each(function(){ $(this).html($(this).html().replaceAll("\n",&q ...