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 这道题依然是DFS求连通块,首先找到@,然后将它的值改成.,再对这一块进行填充就行,每填充一点,计数器就加一
#include"iostream"
#include"cstring"
#include"cstdio"
using namespace std; char a[101][101];
int book[101][101];
int n,m;
int ans; void dfs(int x,int y,int z)
{
if(x<0||x>=n||y<0||y>=m)
return;
if(book[x][y]==1||a[x][y]!='.')
return;
book[x][y]=z;
ans++;
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
if(j==0||i==0) dfs(x+i,y+j,z);
} int main()
{
while(scanf("%d%d",&m,&n)==2&&n&&m)
{
// memset(a,0,sizeof(a));
for(int i=0;i<n;i++) scanf("%s",a[i]);
memset(book,0,sizeof(book));
ans=0;
for(int j=0;j<n;j++)
for(int k=0;k<m;k++)
{
if(a[j][k]=='@') {a[j][k]='.';dfs(j,k,1);}
}
cout<<ans<<endl;
}
return 0;
}

I - DFS(依然是漫水填充)的更多相关文章

  1. DFS求连通块(漫水填充法)

    G - DFS(floodfill),推荐 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I6 ...

  2. OpenCV3编程入门笔记(4)腐蚀、膨胀、开闭运算、漫水填充、金字塔、阈值化、霍夫变换

    腐蚀erode.膨胀dilate 腐蚀和膨胀是针对图像中的白色部分(高亮部分)而言的,不是黑色的.除了输入输出图像外,还需传入模板算子element,opencv中有三种可以选择:矩形MORPH_RE ...

  3. 漫水填充算法 - cvFloodFill() 实现

    前言 漫水填充算法是用来标记一片区域的:设置一个种子点,然后种子点附近的相似点都被填充同一种颜色. 该算法应用性很广,比如目标识别,photoshop 的魔术棒功能等等,是填充类算法中应用最为广泛的一 ...

  4. 【OpenCV】漫水填充

    漫水填充:也就是用一定颜色填充联通区域,通过设置可连通像素的上下限以及连通方式来达到不同的填充效果;漫水填充经常被用来标记或分离图像的一部分以便对其进行进一步处理或分析,也可以用来从输入图像获取掩码区 ...

  5. 【OpenCV新手教程之十五】水漫金山:OpenCV漫水填充算法(Floodfill)

    本系列文章由@浅墨_毛星云 出品,转载请注明出处.    文章链接: http://blog.csdn.net/poem_qianmo/article/details/28261997 作者:毛星云( ...

  6. OpenCV漫水填充算法示例代码

    #include<cv.h> #include<highgui.h> int main(int argc, char** argv) { IplImage* img = cvL ...

  7. opencv 4 图像处理(漫水填充,图像金字塔与图片尺寸缩放,阈(yu)值化)

    漫水填充 实现漫水填充算法:floodFill函数 简单调用范例 #include <opencv2/opencv.hpp> #include <opencv2/imgproc/im ...

  8. Opencv之漫水填充效果

    下面是opencv的漫水填充效果代码 这篇文章仅限个人的笔记 没有详细的注释 放代码 这是简单的示范 int main()//*******************简单的漫水填充算法实例 { Vide ...

  9. opencv —— floodFill 漫水填充法 实现证件照换背景

    漫水填充:floodFill 函数 简单来说,漫水填充就是自动选中与种子像素相连的区域,利用指定颜色进行区域颜色填充.Windows 画图工具中的油漆桶功能和 Photoshop 的魔法棒选择工具,都 ...

随机推荐

  1. Qt下存储读写应用程序设置的三种方法

    一.简介 用户对应用程序经常有这样的要求:要求它能记住它的settings,比如窗口大小.位置和密码等等.有三种方法可以实现: 使用注册表: 使用配置文件(.ini): 使用自定义文件(例如.txt) ...

  2. Codeforces Round #459 (Div. 2)C. The Monster

    C. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Liferay门户部署至Apusic Application Server域

    Liferay默认情况下部署在Tomcat容器中,因项目原因需要将Liferay部署至金蝶Apusic Application Server应用服务器中,部署过程如下,特此记录. 1.liferay- ...

  4. android 系统的时间间隔和睡眠用哪个?

    原文 : https://developer.android.com/reference/android/os/SystemClock.html SystemClock.elapsedRealtime ...

  5. net start iisadmin报错:系统找不到指定的文件

    IIS Admin Service不能启动 ,直接启动或命令(net start iisadmin)都不成功.导致IIS站点访问异常. 最终参考网上解决方案: 这是大多是由于windows\syste ...

  6. 动态生成li标签,并设置点击事件

    今天要解释的是如下界面              主要实现了: 1.模拟后台的json数据,动态生成li标签 2.导航栏的下划线 3.给li标签右边设置图片 4.动态生成的li标签,设置选中的li的点 ...

  7. 设置webbrowser浏览器内核

    var hklm = Microsoft.Win32.Registry.LocalMachine;            var lmRun64 = hklm.OpenSubKey(@"SO ...

  8. java 缓冲流 Buffer

    缓冲流 Buffer :设置缓冲区加快执行效率 子类: (一)BufferedInputStream : 缓冲输入字节流 ,目的:提高读取文件的效率  注意: BufferedInputStream ...

  9. 前端Unicode转码的好处

    站长工具支持Unicode转码:http://tool.chinaz.com/Tools/Unicode.aspx (这是一个网页标题)转码后 ------>变为:\u8fd9\u662f\u4 ...

  10. 以JSONobject形式提交http请求

    总结一下设置图标的三种方式: (1)button属性:主要用于图标大小要求不高,间隔要求也不高的场合. (2)background属性:主要用于能够以较大空间显示图标的场合. (3)drawableL ...