Red and Black

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6762 Accepted Submission(s): 4284
Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

 
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

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)

 
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

 
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
 
Sample Output
45
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(广度优先搜索)的更多相关文章

  1. 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 ...

  2. HDU 1312 Red and Black (DFS & BFS)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...

  3. HDU 1312 Red and Black (DFS)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  4. HDU 1312 Red and Black --- 入门搜索 DFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  5. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  6. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

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

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

  8. BFS广度优先搜索 poj1915

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

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

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

随机推荐

  1. HDU 1847 (博弈 找规律) Good Luck in CET-4 Everybody!

    为了提高题解质量还是简单证明一下:3的倍数是必败状态. 如果n % 3 = 1,那么拿走1个石子:如果n % 3 = 2,那么拿走两个石子,都将转移到3的倍数的状态.所以每个必胜状态都有一个后继是必败 ...

  2. AngularJS分页实现

    基本思路 一开始页码为1,Service向服务器端获取对应信息:点击上/下一页/跳转,通过对应的页码向服务器端获取对应的信息. 由于后台暂时没弄好,我实现的过程中直接读取准备好的JSON文件,通过页码 ...

  3. Linux Shell编程(4): 逻辑运算符、逻辑表达式详解

    shell的逻辑运算符 涉及有以下几种类型,因此只要适当选择,可以解决我们很多复杂的判断,达到事半功倍效果. 一.逻辑运算符 逻辑卷标 表示意思 1. 关于档案与目录的侦测逻辑卷标! -f 常用!侦测 ...

  4. 备忘录 - numpy基本方法总结

    一.数组方法 创建数组:arange()创建一维数组:array()创建一维或多维数组,其参数是类似于数组的对象,如列表等 反过来转换则可以使用numpy.ndarray.tolist()函数,如a. ...

  5. ajax读取json数据

    首先建立json.txt文件 { "programmers": [ { "firstName": "Brett", "lastNa ...

  6. web开发利器 fiddler

    http://mccxj.github.io/blog/20130531_introduce-to-fiddler.html

  7. 自定义View(三)--实现一个简单地流式布局

    Android中的流式布局也就是常说的瀑布流很是常见,不仅在很多项目中都能见到,而且面试中也有很多面试官问道,那么什么是流式布局呢?简单来说就是如果当前行的剩余宽度不足以摆放下一个控件的时候,则自动将 ...

  8. 【PHP】linux搭建PHP运行环境

    之前在windows下写了hello world,终归是不够用啊,因为开发环境是Linux,怎么办呢~~~学习学习再学习 写在前面的话:我从百度文库的一个文章里摘出来的,原文章名称<Linux下 ...

  9. 【转】linux_fdisk命令详解

    转自:http://www.cnblogs.com/xiaofengkang/archive/2011/06/06/2073579.html fdisk -l 可以列出所有的分区,包括没有挂上的分区和 ...

  10. JDK Environment Variable And Change default JDK

    Environment Variable : change(import) /etc/bashrc export JAVA_HOME=/software/jdk1.8.0 export PATH=$J ...