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.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...
随机推荐
- HDU 2897 (博弈 找规律) 邂逅明下
根据博弈论的两条规则: 一个状态是必胜状态当且仅当有一个后继是必败状态 一个状态是必败状态当且仅当所有后继都是必胜状态 然后很容易发现从1开始,前p个状态是必败状态,后面q个状态是必胜状态,然后循环往 ...
- UVa401 回文词
Palindromes A regular palindrome is a string of numbers or letters that is the same forward as backw ...
- HDU 1513 Palindrome
题目就是给一个字符串问最少插入多少个字符能让原字符串变为回文字符串. 算法: 用原串的长度减去原串与翻转后的串的最大公共字串的长度,就是所求答案. //#define LOCAL #include & ...
- .Net dll多个同名的程序集版本冲突共存与通过基本代码或探测定位程序集方案
.Net dll多个同名的程序集版本冲突共存与通过基本代码或探测定位程序集方案 在使用调用程序集的引用中的信息和配置文件中的信息确定了正确的程序集版本之后,并且在公共语言运行时在全局程序集缓存中进行检 ...
- python numpy argsort函数用法
numpy.argsort numpy.argsort(a, axis=-1, kind='quicksort', order=None)[source] Returns the indices th ...
- 解决 RaspberryPi 树莓派 NTP服务异常 无法自动同步时间
sudo nano /etc/ntp.conf 然后找到 # pool.ntp.org maps to about 1000 low-stratum NTP servers. Your server ...
- ASP.NET Web API 帮助(help)页面上没有 Test API按钮的解决方法
参与一个web API项目时发现它的help页面特别好用,不仅可以根据webapi的方法和注释自动生成帮助文档以方便查阅,还可以在这个页面上测试webapi方法.于是在自己新建项目时也打算将这个hel ...
- JdbcTemplate与事务
JdbcTemplate操作采用的是JDBC默认的AutoCommit模式,也就是说我们还无法保证数据操作的原子性(要么全部生效,要么全部无效),如: JdbcTemplate jdbcTemplat ...
- Android的Adapter用法
1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都需要用到Adapter.如下图直 ...
- html中的div、td 、p 等容器内强制换行和不换行的实现
div.td .p 等容器内强制换行和不换行,在某些情况下还是比较实用的,下面本文整理了一些相关方面的知识,并有具体的实现方法,需要的朋友可以参考下1.强制不换行,同时以省略号结尾. 代码如下:< ...