HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
59
6
13
方法一 DFS(深度优先搜素)
import java.io.*;
import java.util.*;
public class Main {
int M=22,w,h,sx,sy;
char ch[][];
int fx[]={1,-1,0,0};
int fy[]={0,0,1,-1};
int number;
boolean boo[][]=new boolean[100][100];
public static void main(String[] args) {
new Main().work();
}
void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
w=sc.nextInt();
h=sc.nextInt();
if(h==0&&w==0)
System.exit(0);
ch=new char[h][w];
for(int i=0;i<h;i++){
String s=sc.next();
ch[i]=s.toCharArray();
Arrays.fill(boo[i], false);
}
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(ch[i][j]=='@'){
sx=i;
sy=j;
}
}
}
number=1;
boo[sx][sy]=true;
DFS(sx,sy);
System.out.println(number);
}
}
void DFS(int sx,int sy){
for(int i=0;i<4;i++){
int px=sx+fx[i];
int py=sy+fy[i];
if(check(px,py)&&!boo[px][py]){
number++;
boo[px][py]=true;
DFS(px,py);
}
}
}
boolean check(int px,int py){
if(px<0||px>h-1||py<0||py>w-1||ch[px][py]!='.')
return false;
return true;
}
}
方法二 BFS( 广度优先搜索)
import java.io.*;
import java.util.*; public class Main {
Queue<Node> que = new LinkedList<Node>();
boolean boo[][] = new boolean[100][100];
char ch[][];
int w, h;
int fx[] = { 1, -1, 0, 0 };
int fy[] = { 0, 0, 1, -1 };
int number; public static void main(String[] args) {
new Main().work();
} void work() {
Scanner sc = new Scanner(new BufferedInputStream(System.in));
while (sc.hasNext()) {
w = sc.nextInt();
h = sc.nextInt();
if(h==0&&w==0)
System.exit(0);
ch = new char[h][w];
for (int i = 0; i < h; i++) {
String s = sc.next();
ch[i] = s.toCharArray();
Arrays.fill(boo[i], false);
}
Node node = new Node();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (ch[i][j] == '@') {
node.x = i;
node.y = j;
node.number = 1;
}
}
}
boo[node.x][node.y] = true;
que.add(node);
number = 1;
BFS();
System.out.println(number); }
} void BFS() {
while (!que.isEmpty()) { Node node = que.poll();
for (int i = 0; i < 4; i++) {
int px = node.x + fx[i];
int py = node.y + fy[i];
if (check(px, py) && !boo[px][py]) {
number++;
Node td = new Node();
td.x = px;
td.y = py;
boo[px][py] = true;
ch[px][py] = 'S';
que.add(td);
}
}
}
} boolean check(int px, int py) {
if (px < 0 || px > h - 1 || py < 0 || py > w - 1 || ch[px][py] != '.')
return false;
return true;
} class Node {
int x;
int y;
int number; }
}
HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)的更多相关文章
- HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU 1312 Red and Black (DFS & BFS)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...
- HDU 1312 Red and Black (DFS)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想
dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...
- BFS广度优先搜索 poj1915
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...
- 图的遍历BFS广度优先搜索
图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...
随机推荐
- 51nod1199 Money out of Thin Air
链剖即可.其实就是利用了链剖后子树都在一段连续的区间内所以可以做到O(logn)查询和修改. 线段树细节打错了..要专心!肉眼差错都能找出一堆出来显然是不行的!. #include<cstdio ...
- IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)
一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...
- Java 动态写轮眼 SharinganJPanel (整理)
/** * Java 动态写轮眼 SharingganJPanel (整理) * * 2016-1-2 深圳 南山平山村 曾剑锋 * 设计声明: * 1.虽然岸本是日本人,而我个人作为其模仿者,依 ...
- HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)
题意: 给一个n*m矩阵,每个格子上有一个数字a[i][j],给定L和U,问:是否有这样两个序列{a1...an}和{b1...bn},满足 L<=a[i][j]*ai/bj<=U .若存 ...
- [转] gc tips(1)
所有应用软件都需要管理内存,一个应用软件的内存管理系统包括了如下准则:什么时候派发内存,要派发多少内存,什么时候把东西放到回收站,以及什么时候清空回收站.MMgc是Flash Player几乎所有内存 ...
- 深入学习Heritrix---解析CrawlController(转)
当我们以Web UI方式使用Heritrix时,点击任务开始(start)按钮时,Heritrix就开始了它的爬取工作.但它的内部 执行流程是怎样的呢?别急,下面将慢慢道来. (一)CrawlJobH ...
- RAC 环境下修改归档模式
RAC环境下的归档模式切换与单实例稍有不同,主要是共享存储所产生的差异.在这种情况下,我们可以将RAC数据库切换到非集群状态下,仅仅在一个实例上来实施归档模式切换即可完成RAC数据库的归档模式转换问题 ...
- 让层遮挡select(ie6下的问题)
虽然现在很多比较大的网站已经不考虑ie6了,不过这些方法,或者其中原理还是值得记录下来的.所以整理的时候,把这篇文章留下了. <script language="javascript& ...
- PHP中最容易忘记的一些知识点总结
1.require 和require_once 区别: 前者遇到即包含文件,后者会判断是否已经包含过了,如果包含过了,则不再包含文件.一可以节省资源,二可以避免重复定义的错误. 2.include 和 ...
- Bootstrap学习之路(3)---列表组件
列表是几乎所有网站都会用到的一个组件,正好bootstrap也给我们提供了这个组件的样式,下面我给大家简单介绍一下bootstrap中的列表组件的用法! 首先,重提一下引用bootstrap的核心文件 ...