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:
 
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static int count = 0;
  6.  
  7. public static int maxN = 0;
  8.  
  9. public static int maxM = 0;
  10.  
  11. public static void main( String[] args ) {
  12. Scanner sc = new Scanner( System.in );
  13. int n, m;
  14. while( sc.hasNext() ) {
  15. count = 0;
  16. n = sc.nextInt();
  17. m = sc.nextInt();
  18. int si = 0;
  19. int sj = 0;
  20. if( n == 0 || m == 0 )
  21. return;
  22. else {
  23. maxN = n;
  24. maxM = m;
  25. char[][] maps = new char[ m ][ n ];
  26. for( int i = 0; i < m; i++ ) {
  27. String s = sc.next();
  28. for( int j = 0; j < s.length(); j++ ) {
  29. char c = s.charAt( j );
  30. maps[ i ][ j ] = c;
  31. if( c == '@' ) {
  32. si = i;
  33. sj = j;
  34. }
  35. }
  36. }
  37. dfs( maps, si, sj );
  38. System.out.println( count );
  39. }
  40. }
  41. }
  42.  
  43. private static void dfs( char[][] maps, int i, int j ) {
  44. if( !cons( i, j ) )
  45. return;
  46. if( maps[ i ][ j ] == '@' || maps[ i ][ j ] == '.' ) {
  47. count++;
  48. maps[ i ][ j ] = '#';
  49. dfs( maps, i + 1, j );
  50. dfs( maps, i - 1, j );
  51. dfs( maps, i, j + 1 );
  52. dfs( maps, i, j - 1 );
  53. } else {
  54. return;
  55. }
  56. }
  57.  
  58. public static boolean cons( int i, int j ) {
  59. if( i < 0 || j < 0 || i >= maxM || j >= maxN )
  60. return false;
  61. return true;
  62. }
  63.  
  64. }
 
 
 
 
 
 
 
 
 
 

[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. 去掉删除discuz x3.2 的-Powered by Discuz!

    如图discuz论坛 网站标题栏的尾巴powered by discuz!是不是很想删除呢,特别是为什么会剩下短线呢?下面就叫你如何准确删除或者修改. 工具/原料 8UFTP(使用自己熟悉的网站文件上 ...

  2. jQuery 的 ready 函数是如何工作的?(源码分析)

    如果你使用过 jQuery , 就必然使用过 ready 函数,它用来注册当页面准备好之后可以执行的函数. 问题来啦,我们的页面什么时候准备好了呢? 1. onload 事件 最基本的处理方式就是页面 ...

  3. BNU Online Judge-34776-What does the fox say?

    题目链接 http://www.bnuoj.com/bnuoj/problem_show.php?pid=34776 题意: fox 的叫声 例如测试用例 输入 toot woof wa ow ow ...

  4. centos下修改hostname,ip,netmask,gateway,dns

    http://itlab.idcquan.com/linux/set/917191.html http://jingyan.baidu.com/article/e4d08ffdd417660fd3f6 ...

  5. SVG在网页中的四种使用方式

    1,直接打开simple.svg <svg xmlns="http://www.w3.org/2000/svg" width="200" height=& ...

  6. jquery checkbox全选 获取值

    <style> table { line-height:35px; }</style> <div align="left" style="m ...

  7. 利用apache的mod_rewrite做URL规则重写

    使用mod_rewrite做url重写,伪静态,做过很多次,这次用几个例子记下来,便于后面查用. 使用方法: 1.在conf目录的httpd.conf文件中找到: LoadModule rewrite ...

  8. SQL Server 2014内存优化表的使用场景

    SQL Server 2014内存优化表的使用场景 最近一个朋友找到走起君,咨询走起君内存优化表如何做高可用的问题 大家知道,内存优化表是从SQL Server 2014开始引入,可能大家对内存优化表 ...

  9. java连接ms sql server各类问题解析

    首先先来说下使用微软自己开发的架包进行ms sql server数据库的连接时,sql 2000与sql 2005的连接方式略有不同: 1.首先驱动不一样,sql 2000的连接驱动包有三个,分别是: ...

  10. xshell无法连接到linux主机原因分析

    xshell连接linux主机时,会出现错误:Could not connect to '192.168.89.144' (port 22): Connection failed.  但是这时能pin ...