Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 40685   Accepted: 22079

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

Source

题解:简单的BFS

AC代码

 1 #include<stdio.h>
2 #include<iostream>
3 #include<stdio.h>
4 #include<string>
5 #include<cmath>
6 #include<algorithm>
7 using namespace std;
8
9 char a[25][25];
10 int w, h;
11 int ant;
12
13 void dfs(int x, int y)
14 {
15 if(a[x][y] != '#' && x >= 0 && x < h && y >= 0 && y < w)
16 {
17 ant++;
18 a[x][y] = '#';
19 dfs(x+1, y);
20 dfs(x-1, y);
21 dfs(x, y+1);
22 dfs(x, y-1);
23 }
24 }
25
26 int main()
27 {
28 while(scanf("%d%d", &w, &h) != EOF)
29 {
30 ant = 0;
31 if(w == 0 && h == 0)
32 break;
33 for(int i = 0; i < h; i++)
34 {
35 for(int j = 0; j < w; j++)
36 {
37 cin >> a[i][j];
38 }
39 }
40 for(int i = 0; i < h; i++)
41 for(int j = 0; j < w; j++)
42 if(a[i][j] == '@')
43 dfs(i, j);
44 cout << ant << endl;
45 }
46 return 0;
47 }

red and black(BFS)的更多相关文章

  1. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  2. POJ 1979 Red and Black (BFS)

    链接 : Here! 思路 : 简单的搜索, 直接广搜就ok了. /****************************************************************** ...

  3. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  4. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  5. Red and Black(poj 1979 bfs)

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27891   Accepted: 15142 D ...

  6. HDU 1312 Red and Black(bfs)

    Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descr ...

  7. HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

    题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...

  8. B - Red and Black 直接BFS+队列

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  9. hdu1312 Red and Black 简单BFS

    简单BFS模版题 不多说了..... 直接晒代码哦.... #include<cstdlib> #include<iostream> #include<cstdio> ...

随机推荐

  1. 你要是还学不会,请提刀来见 Typora+PicGo+Gitee + node.js 打造个人高效稳定优雅图床

    你要是还学不会,请提刀来见 Typora+PicGo+Gitee + node.js 打造个人高效稳定优雅图床 经过前面两弹的介绍,相信大家对图床都不陌生了吧, 但是小魔童觉得这样做法还是不方便,使用 ...

  2. Cassandra数据操作管理工具tableplus

    一.概述 Cassandra是一个NoSQL数据库,具有类SQL CQL入口,基本语法与SQL保持一致.其实笔者认为 Cassandra的自带的cqlsh已经满足本的需求:如: 但是用习惯了数据库操作 ...

  3. mtk相机冷启动拆解

    1 概述 冷启动大致可以分成以下几块内容: S0 (system) 主要是 Activity 的创建耗时(从 Touch up,即 ptr:up 开始) ptr:up S1 App 从 Activit ...

  4. Spring MVC 配置记录

    目录 1.从pom.xml配置Maven文件开始 2.web.xml 3.springmvc-config.xml 4.controller 使用 idea 编辑器 + Maven + spring ...

  5. 解读KMP算法

    前后断断续续搞了5个月,每次都以为自己懂了, 但是要写的时候都不知从何下手,然后又是各种找博客,看帖子,所以这次试着用自己的语言写一个博客. 首先,KMP算法就是从一个模板字符串(S) 中匹配目标字符 ...

  6. 【python+selenium的web自动化】- Selenium WebDriver原理及安装

    简单介绍 selenium ​ selenium是一个用于测试web网页的自动化测试工具,它直接运行在浏览器中,模拟用户的操作.

  7. 仿String()构造器函数 【总结】

    需求 实现以下方法: 控制台结果: 需求分析: 首先确定new调用的this和什么对象绑定,如果跟默认返回的对象绑定肯定做不到 [ ] 这样的访问,所以要在构造器内部返回一个包装过的数组 1.leng ...

  8. Windows + Jenkins + .NetFramework + SVN 持续部署

    Windows + Jenkins + .NetFramework + SVN 持续部署 环境准备 服务端环境 安装 Windows 服务器 1.阿里云购买临时服务器 阿里云:https://www. ...

  9. MySQL-索引简介

    一.索引是什么? 索引是本质是一种数据结构,索引的目的在于提高查询效率.[排好序的快速查找的数据结构] 每个节点包含索引键值和一个指向对应数据记录物理地址的指针. 索引往往以索引文件的形式存储在磁盘. ...

  10. 201871030125-王芬 实验二 个人项目-《D{0-1}问题》软件项目报告

    实验二 个人项目-<D{0-1}问题>软件项目报告 项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/2018CST 这个作业要求链接 ...