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. Mysql根据数据库的时间字段到点更新另外的状态。

    转载:https://blog.csdn.net/xingfuzhijianxia/article/details/53727820 需求如下: 添加一条提醒,被提醒人在提醒时间未到达的时候收不到此提 ...

  2. mybatis-plus 获取新增id

    <insert id="insert" parameterType="com.xxx.xxxx.pojo.User"> insert into t_ ...

  3. docker 端口被占用问题解决

    启动容器A, A的端口映射是 80:8080 外部的25000端口映射到服务内部的8080端口:有时候将容器关闭,重新构建镜像及启动容器时会出现一些报错, 比如端口被占用的报错,但通过docker p ...

  4. Miller&&Pollard HDOJ 4344 Mark the Rope

    题目传送门 题意:一个长为n(n<2^63)的管子,在管子上做标记,每隔L个长度单位做一个标记,从管子头端开始,保证最后一次标记恰好在管子的尾端.让你找出有多少个这样的L(L<n),且他们 ...

  5. stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  6. PHP获取今天内的时间 今天开始和结束的时间戳

    $t = time(); $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y", ...

  7. 工作中js和jquery 函数收集

    1. 判断单选框是否选中  $("xxx").is(":checked")       返回类型是 true/false 2. $(this)[0].nextS ...

  8. SharedPrefences的用处

    存储数据 SharedPreferences.Editor edit = getSharedPreferences("data", MODE_PRIVATE).edit(); ed ...

  9. spark on yarn模式下内存资源管理(笔记1)

    问题:1. spark中yarn集群资源管理器,container资源容器与集群各节点node,spark应用(application),spark作业(job),阶段(stage),任务(task) ...

  10. 【译】x86程序员手册38-10.2实在址模式下的软件初始化

    10.2 Software Initialization for Real-Address Mode   实地址模式的软件初始化 In real-address mode a few structur ...