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. C++Template 模版的本质

    我想知道上帝的構思,其他的都祇是細節.                                                                                  ...

  2. 百度AI api使用

    # *********************************baidu-api-通用文字识别******************************************** # im ...

  3. 手把手教你手写一个最简单的 Spring Boot Starter

    欢迎关注微信公众号:「Java之言」技术文章持续更新,请持续关注...... 第一时间学习最新技术文章 领取最新技术学习资料视频 最新互联网资讯和面试经验 何为 Starter ? 想必大家都使用过 ...

  4. hiho一下 第195周 奖券兑换[C solution][Accepted]

    时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi在游乐园中获得了M张奖券,这些奖券可以用来兑换奖品. 可供兑换的奖品一共有N件.第i件奖品需要Wi张奖券才能兑换到, ...

  5. CodeBlocks的安装配置以及使用教程

    CodeBlocks的安装配置以及使用教程 教程写的很啰嗦,本来几句话就能搞定的,但为了照顾到那部分真正的小白还请大家见谅! 一.下载 前往CodeBlocks官网下载带编译器的版本,目前的最新版本为 ...

  6. Azure Front Door(二)对后端 VM 进行负载均衡

    一,引言 上一篇我们讲到通过 Azure Front Door 为我们的 Azure App Service 提供流量转发,而整个 Azure Front Door 在添加后端池的时候可选的后端类型是 ...

  7. bjd_ctf

    1.抓包修改 ​ 提示修改id,postman修改headers里面的id 分析得到id是admin加admin的base64编码,payload为id: adminYWRtaW4= 请求后又提示请使 ...

  8. 深入理解Java并发框架AQS系列(二):AQS框架简介及锁概念

    深入理解Java并发框架AQS系列(一):线程 深入理解Java并发框架AQS系列(二):AQS框架简介及锁概念 一.AQS框架简介 AQS诞生于Jdk1.5,在当时低效且功能单一的synchroni ...

  9. java 面试经典题

    面向对象编程(OOP) Java是一个支持并发.基于类和面向对象的计算机编程语言.下面列出了面向对象软件开发的优点: 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可靠性和灵活性. 增加代码 ...

  10. 《C++反汇编与逆向分析技术揭秘》--算术运算和赋值

    一.加法 1.Debug下: 14: int nVarOne0 = 1 + 5 - 3 * 6;//编译时计算得到结果 00C0550E C7 45 F8 F4 FF FF FF mov dword ...