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

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13 思路:使用递归,对@所在的位置四个方向查找,一直到不满足条件h>=n||l>=m||h<0||l<0||str[h][l]=='#'||boo[h][l]==1为止。。。 #include <iostream>
#include <string>
#include<algorithm>    //头文件
using namespace std;
bool boo[25][25];
string str[21];
int jishu=0;
int m,n,h=0,l=0;//h为行,l为列
void search(int h,int l)
{
 if(h>=n||l>=m||h<0||l<0||str[h][l]=='#'||boo[h][l]==1)  //此处如果没有||boo[h][l]==1就会死循环,但是却不显示一直进行,而是程序莫名其妙的结束,以后注意!!!!!!!!!!!!!!
  return ;
 boo[h][l]=1;
 jishu++; search(h,l-1);
 search(h,l+1);
 search(h-1,l);
 search(h+1,l);
}
int main()
{
 int i,j;
 cin>>m>>n;//m是一个字符串的长度 n是字符串数目
 while(m!=0&&n!=0)
 { jishu=0;
       h=0,l=0;
  for(i=0;i<n;i++)
  {
   cin>>str[i];
   for(j=0;j<m;j++)
    if(str[i][j]=='@')
    { 
     h=i;
     l=j;
    }
  }
  memset(boo,0,25*25);
  search(h,l);
  cout<<jishu<<endl;
  cin>>m>>n;
 }
 return 0;
}

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. PCA的数学原理

    PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维 数据的 ...

  2. 初试体验java多线程

    现在个人电脑以及服务器都是多核cpu,如何提高多核cpu的利用率,就要用到多线程技术了. public class TestThread1 { ; static class PThread exten ...

  3. 【solr】 solr 5.4.1 和tomcat 基础环境搭建

    下载省略; solr下载地址:http://www.apache.org/dyn/closer.cgi/lucene/solr/ tomcat 下载安装(省略). solr5.4.1 默认在jetty ...

  4. WPF性能改善---之化整为零(蜂窝的衍生应用)

    在有的项目中,有这样的需求,由于显示器的显示区域是有限的,而软件却要展示一个远大于显示区域的一些元素,此时就要引入放大.缩小.拖动等UI控制技术,而在使用这些技术的同时,在后台有效的控制渲染元素的个数 ...

  5. SSH_框架整合1

    1 WEB环境下配置Spring   因为是在WEB环境中应用Spring,所以要先配置web.xml: (1)WebContent-WEB-INF-lib包中,加入Spring包下的required ...

  6. Python输入和输出

    在很多时候,你会想要让你的程序与用户(可能是你自己)交互.你会从用户那里得到输入,然后打印一些结果.我们可以分别使用raw_input和print语句来完成这些功能.对于输出,你也可以使用多种多样的s ...

  7. Python 通过print_lol将数据保存到文件中

    1. 定义一个print_lol函数来控制列表的缩进和写入位置 import sys """this is a new fuction, which work for a ...

  8. JavaScript中对于闭包的理解

    1.什么是闭包? 闭包,官方对闭包的解释是:一个拥有很多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 闭包的特点: (1).作为一个函数变量的一个引用,当函 ...

  9. SQL 中delete和truncate区别

    1.前者按行删除,后者直接删除数据页 2.前者可带where删除部分,后者只能删除全表 3.前者在事务日志中记录每一行的记录,后者只记录页的释放 4.前者删除后,标识技术值不重置,后者重置 5.由fo ...

  10. openmp并行计算

    #include <omp.h>#include <stdio.h>#include <stdlib.h> void test(int n){ for (int i ...