Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16129    Accepted Submission(s): 9939

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
 
 
 
 
 
 
 
一道很经典的bfs题目,比较简单,适合刚接触dfs的人。
注意:
1.在初始点进行dfs,因为初始点也是一个符合要求的点,所以别忘记了加上。
 
Code:
 
import java.util.Scanner;

public class Main {

    public static int count = 0;

    public static int maxN = 0;

    public static int maxM = 0;

    public static void main( String[] args ) {
Scanner sc = new Scanner( System.in );
int n, m;
while( sc.hasNext() ) {
count = 0;
n = sc.nextInt();
m = sc.nextInt();
int si = 0;
int sj = 0;
if( n == 0 || m == 0 )
return;
else {
maxN = n;
maxM = m;
char[][] maps = new char[ m ][ n ];
for( int i = 0; i < m; i++ ) {
String s = sc.next();
for( int j = 0; j < s.length(); j++ ) {
char c = s.charAt( j );
maps[ i ][ j ] = c;
if( c == '@' ) {
si = i;
sj = j;
}
}
}
dfs( maps, si, sj );
System.out.println( count );
}
}
} private static void dfs( char[][] maps, int i, int j ) {
if( !cons( i, j ) )
return;
if( maps[ i ][ j ] == '@' || maps[ i ][ j ] == '.' ) {
count++;
maps[ i ][ j ] = '#';
dfs( maps, i + 1, j );
dfs( maps, i - 1, j );
dfs( maps, i, j + 1 );
dfs( maps, i, j - 1 );
} else {
return;
}
} public static boolean cons( int i, int j ) {
if( i < 0 || j < 0 || i >= maxM || j >= maxN )
return false;
return true;
} }
 
 
 
 
 
 
 
 
 
 

[HDUOJ1312]Red And Black (经典的DFS)的更多相关文章

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

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

  2. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  3. 题解报告:hdu 1312 Red and Black(简单dfs)

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

  4. 78. Subsets(中等,集合的子集,经典问题 DFS)

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  5. 蓝桥杯 历届试题 约数倍数选卡片 (经典数论+DFS)

    闲暇时,福尔摩斯和华生玩一个游戏: 在N张卡片上写有N个整数.两人轮流拿走一张卡片.要求下一个人拿的数字一定是前一个人拿的数字的约数或倍数.例如,某次福尔摩斯拿走的卡片上写着数字“6”,则接下来华生可 ...

  6. POJ 1321 棋盘问题(非常经典的dfs,入门题)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66277   Accepted: 31639 Descriptio ...

  7. poj(1011)——Sticks(经典的dfs+剪枝)

    题目的大致意思是: 如今有n根木棍,然后须要把它们拼成相同长度的木棍,问满足这个条件的最短的长度是多少? 想法嘛:那肯定是dfs把长度搜一遍就好,但问题的关键是这里会超时.那么就要用到剪枝的原理了. ...

  8. POJ 1979 Red and Black (zoj 2165) DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  9. HDU 1312 Red and Black(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...

随机推荐

  1. Activity的生命周期与加载模式——Activity的生命周期演示

    当Activity处于Android应用中运行时,它的活动状态由Android以Activity栈的形式管理.当前活动的Activity位于栈顶.随着不同应用的运行,每个Activity都有可能从活动 ...

  2. Cocoa 新的依赖管理工具:Carthage

    昨天搞了一下pod的安装 因为之前我都是在使用pod来进行第三方库的管理 但是拿到项目之后 竟发现这个前辈是用Carthage 说真的在这之前我从来没有用过这个玩意因为我感觉用POD已经很好了啊 很方 ...

  3. Promise基础

    前言: ES2015将Promise引入语言规范,包括fetch等在内的API也构建在Promise之上.作为让js摆脱“回调地狱”的重要一环和众多框架中的重要基础设施之一,学习如何自己实现一个Pro ...

  4. Spring3.2AOP实现需要添加的三个包

    Spring3.2AOP实现需要添加的三个包 http://down.51cto.com/data/1001395 http://down.51cto.com/data/519542

  5. HttpURLConnection从网上获取Json数据并解析详解

    HttpURLConnection从网上获取Json数据并解析 1.HttpURLConnection请求数据的步骤 (1)构造一个URL接口地址: URL url = new URL("h ...

  6. 支付宝 Android 版使用的开源组件

    支付宝 Android 版使用的开源组件 前言: 花了点时间整理了 支付宝 Android 客户端使用的开源组件,给需要的同学.在你不知道用什么开源框架的时候可以作下参考,毕竟支付宝是阿里的重量级产品 ...

  7. CodeForces 451B

    Sort the Array Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  8. 微信前端面试题----js实现LazyMan

    这是微信小程序的一道面试题,题目是这样的: 实现一个LazyMan,可以按照以下方式调用:LazyMan("Hank")输出:Hi! This is Hank! LazyMan(& ...

  9. Linux Platform驱动模型(二) _驱动方法

    在Linux设备树语法详解和Linux Platform驱动模型(一) _设备信息中我们讨论了设备信息的写法,本文主要讨论平台总线中另外一部分-驱动方法,将试图回答下面几个问题: 如何填充platfo ...

  10. Express与NodeJs创建服务器的两种方法

    NodeJs创建Web服务器 var http = require('http'); var server = http.createServer(function(req, res) { res.w ...