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.

InputThe 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) 
OutputFor 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求连通,读入时找到起点即可,代码如下:
const int maxm = ;
const int dx[] = {-, , , };
const int dy[] = {, , -, }; int m, n, sx, sy, vis[maxm][maxm], t;
char buf[maxm][maxm]; bool inside(int x,int y) {
return x >= && x < m && y >= && y < n;
} void dfs(int x,int y) {
if(vis[x][y])
return;
vis[x][y] = ;
for (int i = ; i < ; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if(inside(nx,ny) && buf[nx][ny] == '.' &&!vis[nx][ny]) {
++t;
dfs(nx, ny);
}
}
} int main() {
while(scanf("%d%d",&n,&m) && m + n) {
getchar();
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
scanf("%c", &buf[i][j]);
if(buf[i][j] == '@') {
sx = i, sy = j;
}
}
getchar();
}
memset(vis,,sizeof(vis));
t = ;
dfs(sx, sy);
printf("%d\n", t);
}
return ;
}

 

Day2-K-Red and Black-HDU1312的更多相关文章

  1. 计蒜客 Red Black Tree(树形DP)

    You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of ...

  2. 用php随机生成福彩双色球号码的2种方法

    不瞒您说,俺也是个双色球爱好者,经常买,但迟迟没有中过一等奖,哈哈.这里为大家介绍用php随机生成福彩双色球号码的二种方法,供朋友们学习参考.新的一年,祝大家中大奖,发大财. 方法一 复制代码代码如下 ...

  3. switch语句的使用,非常好

    这是谭浩强课本上枚举类型的例子,但是我贴这个例子的代码不是因为枚举类型,是因为这个代码使用switch语句用得非常好,值得一贴. 题目是这样的:有红.黄.蓝.白.黑5中颜色的球若干,依次取出3个球,求 ...

  4. css3绘制中国结

    <!doctype html> <html> <head> <title></title> <meta charset='utf-8' ...

  5. php随机生成福彩双色球号码

    发布:thebaby   来源:net     [大 中 小] 不瞒您说,俺也是个双色球爱好者,经常买,但迟迟没有中过一等奖,哈哈.这里为大家介绍用php随机生成福彩双色球号码的二种方法,供朋友们学习 ...

  6. NOI 2013 书法家

    http://uoj.ac/problem/125 我真是日狗了...... 果然还是没有耐心读题,搞到读题读错了2个地方,结果调试了半天...... 言归正传. 动态规划. 这种题目很常见. 我们发 ...

  7. NOI2015

    D1T1 并查集. #include<cstdio> #include<cstdlib> #include<iostream> #include<fstrea ...

  8. 基础dp

    队友的建议,让我去学一学kuangbin的基础dp,在这里小小的整理总结一下吧. 首先我感觉自己还远远不够称为一个dp选手,一是这些题目还远不够,二是定义状态的经验不足.不过这些题目让我在一定程度上加 ...

  9. 【C++/函数】实验2

    1.实验内容 函数声明和函数定义: 形参和实参: 主调函数把实参的值传送给被调函数的形参从而实现主调函数向被调函数的数据传送. 形参:形式参数,表示主调函数与被调函数之间的联系.标识了在形参出现的位置 ...

  10. 实验一:C++简单程序设计

    [实验一] #2-28 实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入.A表示增加,D表示删除,S表示排序 ...

随机推荐

  1. ubuntu apache 通过端口新建多个站点

    cd /etc/apache2/sites-available 最近的虚拟机没绑定域名,所以呢,就先用域名加端口新建几个站点用着 1. vim /etc/apapche2/apapche2.conf ...

  2. ZOJ1005 Jugs

    题意:有两个容量互质的容器,需要用这两个容器量出目标重量的水,找到其中一组解.bfs,使得搜索得到的解是步数最少的,遍历前驱法输出路径~ #include<bits/stdc++.h> u ...

  3. IDEA中常用优化设置

    1.设置鼠标悬浮提示 Editor->General 这里要勾选下,后面设置的是延迟时间 默认半秒:设置后,我们鼠标移动到类上看看: 2.显示方法分隔符 Editor->General - ...

  4. thymeleaf模板引擎简介

    一:thymeleaf 学习笔记---http://www.blogjava.net/bjwulin/articles/395185.html thymeleaf是一个支持html原型的自然引擎,它在 ...

  5. P4710 平抛运动

    题目:https://www.luogu.org/problemnew/show/P4710 $$ v_x = v_{x_0} = v \ sin \ \theta, \  v_y = v_{y_0} ...

  6. C语言中的快速排序函数

    C库中有自带的快排函数 qsort() ; 它的函数原型为: void qsort(void * , size_t ,size_t size , int (__cdecl *)(const  void ...

  7. Jquery实现ajax常用属性

  8. 图书商城(基于Jsp+Servlet)

    这个项目主要是加深一下对于servlet和jsp知识相关的了解以及简单业务逻辑的处理. 用户更新的逻辑: 1.点击修改用户的那一行可以获取到用户的id 2.跳转到一个servlet,去查询该用户的基本 ...

  9. 【协作式原创】自己动手写docker之run代码解析

    linux预备知识 urfave cli预备知识 准备工作 阿里云抢占式实例:centos7.4 每次实例释放后都要重新安装go wget https://dl.google.com/go/go1.1 ...

  10. MD5 加密解密字符串

    方法1: using System.Text; using System.Security.Cryptography; public string Hash(string toHash) { MD5C ...