Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 31722   Accepted: 17298

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) 
The end of the input is indicated by a line consisting of two zeros. 

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

  1. 6 9
  2. ....#.
  3. .....#
  4. ......
  5. ......
  6. ......
  7. ......
  8. ......
  9. #@...#
  10. .#..#.
  11. 11 9
  12. .#.........
  13. .#.#######.
  14. .#.#.....#.
  15. .#.#.###.#.
  16. .#.#..@#.#.
  17. .#.#####.#.
  18. .#.......#.
  19. .#########.
  20. ...........
  21. 11 6
  22. ..#..#..#..
  23. ..#..#..#..
  24. ..#..#..###
  25. ..#..#..#@.
  26. ..#..#..#..
  27. ..#..#..#..
  28. 7 7
  29. ..#.#..
  30. ..#.#..
  31. ###.###
  32. ...@...
  33. ###.###
  34. ..#.#..
  35. ..#.#..
  36. 0 0

Sample Output

  1. 45
  2. 59
  3. 6
  4. 13

Source

 
  1. #include "cstdio"
  2. #include "cstring"
  3. #include "cmath"
  4. #include "iostream"
  5. #include "string"
  6.  
  7. using namespace std ;
  8. const int maxN = ;
  9.  
  10. const int dx[ ] = { , , , - } ;
  11. const int dy[ ] = { , , - , } ;
  12.  
  13. char mp[ maxN ][ maxN ] ;
  14. int N , M ;
  15.  
  16. void DFS ( const int xi , const int yi ) {
  17. if ( !mp[ xi ][ yi ] )return ;
  18. mp[ xi ][ yi ] = '%' ;
  19. for ( int i= ; i< ; ++i ) {
  20. int xx = xi + dx[ i ] ;
  21. int yy = yi + dy[ i ] ;
  22. if ( xx > && xx <= M && yy > && yy <= N && mp[ xx ][ yy ] == '.' )
  23. DFS( xx , yy ) ;
  24. }
  25. }
  26.  
  27. int sumerize ( const int n , const int m ) {
  28. int ret ( ) ;
  29. for ( int i= ; i<=n ; ++i ) {
  30. for ( int j= ; j<=m ; ++j ) {
  31. if ( mp[ i ][ j ] == '%' ) ++ret ;
  32. }
  33. }
  34. return ret ;
  35. }
  36.  
  37. int main ( ) {
  38. int start_x , start_y ;
  39. while ( scanf ( "%d%d\n" , &N , &M ) == && N && M ) {
  40. for ( int i= ; i<=M ; ++i ) {
  41. scanf ( "%s" , mp[ i ] + ) ;
  42. }
  43. for ( int i= ; i<=M ; ++i ) {
  44. for ( int j= ; j<=N+ ; ++j ) {
  45. if ( mp[ i ][ j ] == '@' ) {
  46. mp[ i ][ j ] = '.' ;
  47. start_x = i ;
  48. start_y = j ;
  49. goto Loop ;
  50. }
  51. }
  52. }
  53. Loop :
  54. DFS ( start_x , start_y ) ;
  55.  
  56. printf ( "%d\n" , sumerize ( M , N ) ) ;
  57. memset ( mp , , sizeof ( mp ) ) ;
  58. }
  59.  
  60. return ;
  61. }

2016-10-21  16:28:48

POJ 1979 题解的更多相关文章

  1. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  2. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  3. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  4. POJ 1979 dfs和bfs两种解法

      fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...

  5. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

  6. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  7. POJ 1979 DFS

    题目链接:http://poj.org/problem?id=1979 #include<cstring> #include<iostream> using namespace ...

  8. POJ 1979 红与黑

    题目地址: http://poj.org/problem?id=1979  或者  https://vjudge.net/problem/OpenJ_Bailian-2816 Red and Blac ...

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

随机推荐

  1. Lisp永远成不了编程主流语言

        Lisp语言是第二古老的高级编程语言.许多的黑客和开发者对Lisp推崇备至,Paul Graham甚至说"编程语言现在的发展,不过刚刚赶上1958年Lisp语言的水平". ...

  2. 对前台传回的list进行分割,并放在sql语句的in中

    前端数据集传回数据 var matDeptHisMonthPlanStore = Ext.data.StoreManager.lookup('matDeptHisMonthPlanStore'); m ...

  3. 项目vue2.0仿外卖APP(二)

    vue-cli开启vue.js项目 github地址:https://github.com/vuejs/vue-cli Vue.js开发利器vue-cli,是vue的脚手架工具. 在工地上,脚手架是工 ...

  4. react+redux官方实例TODO从最简单的入门(4)-- 改

    上一篇文章实现了<删>这个功能,那么我们继续添加下一功能--<改> 这个修改的功能是通过双击每个子选项实现 第一步:按规矩来,添加一个状态声明 第二步:action中约定我们要 ...

  5. PPM图片格式及其C读写代码

    PPM图像格式介绍 PPM图像格式是由Jef Poskanzer 大叔,在我出生那一年,也就是1991年所创造的,碰巧的是PPM也是天蝎座. PPM(Portable Pixmap Format)还有 ...

  6. kali python pip3 的安装和卸载

    今天很高兴安装完成调整了kali 然后看见kali已经帮助我安装了python2.7和python3.5可把我开心坏了,可是2.7有pip,而且包很全,但2.7与3.0切换使用我的就尴尬了 最后在su ...

  7. asp.net 文件下载(txt,rar,pdf,word,excel,ppt)

    aspx 文件下载说起来一点都不难,但是在做的过程中还是遇到了一些小小的问题,就是因为这些小小的问题,导致解决起来实在是太难了,其中一个就是Response.End();导致下载文件出现线程终止的情况 ...

  8. 浅谈HTTP中Get与Post的区别

    引用自:http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET ...

  9. js方法入参或局部变量和全局变量重名,用来赋值全局变量会失败

    今天遇到个bug,最后终于知道原因了,js方法入参和全局变量重名,用入参赋值全局变量失败,就是说方法入参不能和全局变量重名. 现在下面的例子也说明,局部变量和全局变量不可以同名不光是入参,只要同名赋值 ...

  10. linux系统的初化始配置

    一.网络的初始化 1.ip地址的修改(临时生效) 使用ifconfig命令 ifconfig 网卡名 ip地址 子网掩码 [root@localhost /]# ifconfig eno1677773 ...