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. (转载)UITableView使用详解

    在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况 UITableView使用详解 - (void)viewDidLoad { ...

  2. The Robust Fuzzy C-means

    摘要: 基于FCM的在图像处理方面对噪声敏感的不足,本文通过引入空间模型建立空间模糊C均值聚类提高算法的鲁棒性,在此基础上,结合抑制式对算法进一步优化.最后,给图像加不同程度的噪声,通过MATLAB编 ...

  3. Hadoop Hive基础sql语法

     目录 Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件映射为一张数据库表,并提供完整的 ...

  4. mysql 插入汉字出现问号 解决方法

    mysql中文显示乱码或者问号是因为选用的编码不对或者编码不一致造成的,最简单的方法就是修改mysql的配置文件my.cnf.在[mydqld]和[client]段加入 default-charact ...

  5. JDK 1.6 下载 地址

    JDK1.6官方下载_JDK6官方下载地址: http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin ...

  6. app如何节省流量

    前言:“客户端上传时间戳”的玩法,你玩过么?一起聊聊时间戳的奇技淫巧! 缘起:无线时代,流量敏感.APP在登录后,往往要向服务器同步非常多的数据,很费流量,技术上有没有节省流量的方法呢?这是本文要讨论 ...

  7. bzoj1251 序列终结者(splay)

    人生第一发splay,写得巨丑,最后忘记了push_down以后要将子节点maintain 9k代码不忍直视 #define NDEBUG #include<cstdio> #includ ...

  8. C++异常处理assert,throw,exit用法

    常见的几个小细节问题. assert应用: 在现实世界中,我们脑袋时刻都在判断对与错,对的事情我们会继续深入下去,而错的事情我们会马上停止,那么在编程开发中我们如何赋予程序这种判断事物对错的能力呢?其 ...

  9. CString-int-string-char-BSTR之间的转换

    一.CString, int, string, char*之间的转换 string 转 CString CString.Format("%s", string.c_str());c ...

  10. dispatch_once单例初始化

    static GHCache *instance = nil; /** *  单例,静态初始化方法 * *  @return 返回一个单例 */ + (GHCache*)shareCache{ sta ...