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
  5.  
  6. 思路:使用递归,对@所在的位置四个方向查找,一直到不满足条件h>=n||l>=m||h<0||l<0||str[h][l]=='#'||boo[h][l]==1为止。。。
  7.  
  8. #include <iostream>
  9. #include <string>
  10. #include<algorithm>    //头文件
  11. using namespace std;
  12. bool boo[25][25];
  13. string str[21];
  14. int jishu=0;
  15. int m,n,h=0,l=0;//h为行,l为列
  16. void search(int h,int l)
  17. {
  18.  if(h>=n||l>=m||h<0||l<0||str[h][l]=='#'||boo[h][l]==1)  //此处如果没有||boo[h][l]==1就会死循环,但是却不显示一直进行,而是程序莫名其妙的结束,以后注意!!!!!!!!!!!!!!
  19.   return ;
  20.  boo[h][l]=1;
  21.  jishu++;
  22.  
  23. search(h,l-1);
  24.  search(h,l+1);
  25.  search(h-1,l);
  26.  search(h+1,l);
  27. }
  28. int main()
  29. {
  30.  int i,j;
  31.  cin>>m>>n;//m是一个字符串的长度 n是字符串数目
  32.  while(m!=0&&n!=0)
  33.  { jishu=0;
  34.        h=0,l=0;
  35.   for(i=0;i<n;i++)
  36.   {
  37.    cin>>str[i];
  38.    for(j=0;j<m;j++)
  39.     if(str[i][j]=='@')
  40.     { 
  41.      h=i;
  42.      l=j;
  43.     }
  44.   }
  45.   memset(boo,0,25*25);
  46.   search(h,l);
  47.   cout<<jishu<<endl;
  48.   cin>>m>>n;
  49.  }
  50.  return 0;
  51. }

A - Red and Black(3.2.1)(小递归)的更多相关文章

  1. html5小游戏基础知识

    显示一个DIV和隐藏一个DIV 首先,我们要显示一个DIV和隐藏一个DIV需要使用css里面使用: .hide{ display:none;} .show{display:block;} 在需要显示或 ...

  2. 微信小程序视频学习笔记

    [清华大学]学做小程序 https://www.bilibili.com/video/av21987398 2.2创建项目和文件结构 小程序包含一个描述整体程序的app和多个描述各自页面的page 配 ...

  3. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  4. KVM 介绍(1):简介及安装

    学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分 ...

  5. C# 绘制统计图(柱状图, 折线图, 扇形图)【转载】

    统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...

  6. leach协议matlab仿真代码

    http://www.ilovematlab.cn/thread-177006-1-1.html LEACH協議clear;%清除內存變量 xm=100;%x軸範圍ym=100;%y軸範圍 sink. ...

  7. Asp.net 用 Graphics 统计图(柱状图, 折线图, 扇形图)

    统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...

  8. HTML 基础 2

    1. 认识CSS样式: CSS:层叠样式表(Cascading Style Sheets),主要用于定义HTML内容在浏览器内的显示样式 语法: 选择符{ 属性: 值} 举例: p{ color: b ...

  9. 【CSS3】布局

    浮动布局: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

随机推荐

  1. eclipse报An error has occurred,See error log for more details. java.lang.NullPointerException错误

    eclipse报An error has occurred,See error log for more details. java.lang.NullPointerException错误,解决办法: ...

  2. CentOS快速搭建subversion服务器

    环境:centos x86_64说明:使用daemon的方式来跑svn,不使用apache首先到:http://subversion.tigris.org/ 下载最新的subversion源码 我下的 ...

  3. oracle 行转列的例子

    with test as(select '1' bit from dual union select '0' from dual )select replace(sys_connect_by_path ...

  4. Java compiler level does not match the version of the installed Java project facet.问题

    从同事那里拷贝过来的web项目,导入到eclipse中,出现Java compiler level does not match the version of the installed Java p ...

  5. AngularJs中的服务

    一.angularJs中的简单服务应用 下面的例子让我们明白在AngularJs中如何去调用文件中的数据,从而将文件中的数据显示在页面上;改变url的地址,也可以去调用后台接口. 实例: <!D ...

  6. HackerRank "Median Updates"

    Same as LintCode "Sliding Window Median", but requires more care on details - no trailing ...

  7. Softerra LDAP Browser 使用及配置 有图有真相

    Softerra LDAP Browser 4.5 我使用Softerra LDAP Browser的目的,是为了查找公司的人员信息.网上关于Softerra LDAP Browser配置太少了,所以 ...

  8. 关于AVD无法使用问题

    因为工作需要,刚入手android,搭建开发环境挺麻烦,整个环境的搭建配置我就不说了,网上很多,这里就只说一下我遇到的一个问题. Java JDK安装完毕,环境变量配置完毕,eclipse下载好后解压 ...

  9. CentOs of Tomcat commands

    1.启动tomcat 进入tomcat目录的bin目录下 执行 [root@iZ253lxv4i0Z bin]# ./startup.sh Using CATALINA_BASE: /usr/loca ...

  10. Qt属性表控件的使用 QtTreePropertyBrowser

    属性表是vs2003时引入的的新控件,用于流量和设置大量的信息,现在,很多软件上都能看到它的身影,如vs,Qt Creator等IDE的详细设置里都离不开属性表. 下图是Qt Creator里的属性表 ...