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. js中Window 对象及其的方法

    window.location 对象 window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面.window.location 对象在编写时可不使用 wind ...

  2. hdu 1429

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 一个广搜的简单题吧,不过有意思的事这个题目用到了位运算,还有就是很恶心的MLE #include < ...

  3. socket泄露的问题

    在增加keepalive和libevent 超时后,仍然会几个月后出现lsof统计句柄过多的问题,如下图,红线圈定的是不定期出现的错误关闭流,时间点和server端日志(只看到accpet fd,没有 ...

  4. ->code vs 2879 堆的判断(堆的学习一)

    2879 堆的判断  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold   题目描述 Description 堆是一种常用的数据结构.二叉堆是一个特殊的二叉树,他的父 ...

  5. JSP复习整理(五)JavaBean使用表单处理数据

    一.先建立用户输入的数据 usingGetparameter.html <!DOCTYPE html> <html> <head> <meta charset ...

  6. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  7. Linux学习笔记之兄弟连

    systemctl --user enable pulseaudio说明:安装完成后系统没有声音,用该命令可以打开.ifconfig eth0 192.168.118.1说明:给网卡设置IP地址.ap ...

  8. html5 websocket 实时日志推送

    http://blog.csdn.net/neutrojan/article/details/46119645

  9. UE3名称结构(Name)

    解释说明: (1) 直接通过FName的index进行比较来判断两个FName是否相等 (2) 通过FName的index从全局Names数组中取出对应的FNameEntry,可以获得FName的字符 ...

  10. SSH ProxyCommand

    简单的说就是SSH层面的代理服务器,实现通过跳板机执行SSH相关命令到目标机器的代理服务.