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.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...
随机推荐
- KVC&KVO&NSNotification
KVC,即是指 NSKeyValueCoding,一个非正式的 Protocol,提供一种机制来间接访问对象的属性.KVO 就是基于 KVC 实现的关键技术之一. 一个对象拥有某些属性.比如说,一个 ...
- http server 下载地址
windows 64为位:https://www.apachelounge.com/download/
- 51nod 最长递增子序列
nlogn版最长递增子序列.线段树.(其实常数蛮大的....) #include<iostream> #include<cstring> #include<algorit ...
- web前端调试工具
1.firebug入门指南 http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html 2. Console命令详解,让调试js代码变得更 ...
- MySQL Database on Azure - 利用PowerShell快速创建使用数据库服务
如果您的应用需要通过脚本快速创建一个或多个MySQL数据库,MySQL Database on Azure目前开放了对于PowerShell的支持,可以帮助您快速利用自动化脚本进行数据库服务的创建.管 ...
- iOS 5.0 后UIViewController新增:willMoveToParentViewController和didMoveToParentViewCon
在iOS 5.0以前,我们在一个UIViewController中这样组织相关的UIView 在以前,一个UIViewController的View可能有很多小的子view.这些子view很多时候 ...
- sql loader
vi append.sh #!/bin/bash sqlldr userid=bm_weihu/itms_xianwan control=input2.ctl vi input2.ctl LOAD D ...
- Android 手写Binder 教你理解android中的进程间通信
关于Binder,我就不解释的太多了,网上一搜资料一堆,但是估计还是很多人理解的有困难.今天就教你如何从 app层面来理解好Binder. 其实就从我们普通app开发者的角度来看,仅仅对于androi ...
- Android 异常处理最佳实践
一个好的app 异常处理机制 我认为应该至少包含以下几个功能: 1.能把错误信息上传到服务器 让开发者可以持续改进app 2.错误信息至少应该包含 是否在主进程 是否在主线程 等可以帮助程序员定位的 ...
- mysql添加用户和用户权限
Mysql添加用户使用可以对mysql数据库用户表有操作权限的用户名登陆mysqlinsert into user(Host,User,Password) values('%','name','pas ...