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. Redis 内存淘汰机制详解

    一般来说,缓存的容量是小于数据总量的,所以,当缓存数据越来越多,Redis 不可避免的会被写满,这时候就涉及到 Redis 的内存淘汰机制了.我们需要选定某种策略将"不重要"的数据 ...

  2. Prism.WPF -- Prism框架使用(上)

    本文参考Prism官方示例 创建Prism项目 将App.xaml中的WPF标准Application替换为PrismApplication,移除StartupUri属性: 将App.xaml.cs中 ...

  3. 华硕主板开机无法进入BIOS

    先说下本人情况,自己组装的台式机,华硕TUF B550M-Plus (wifi)的主板,CPU是锐龙 4650G(带核显),其他配件不涉及问题就不提了. 原来用独显的时候,没啥问题,开机有品牌logo ...

  4. burpsuite 隐藏 detectportal.firefox.com

        0x00原由 抓包时经常出现detectportal.firefox.com,不利于我们的渗透工作 0x01解决方法 在输入框输入:about:config 然后设置以下选项:network. ...

  5. jdk 集合大家族之Collection

    jdk 集合大家族之Collection 前言:   此处的集合指的是java集合框架中的实现了Collection接口相关的类.所以主要为List Set 和 Queue 其他章节会专门介绍Map相 ...

  6. WPF 基础 - 图片之界面截图

    1. 功能 系统截图. 2. 实现 2.1 思路 控件继承自 System.Windows.Media.Visual, 通过 System.Windows.Media.Imaging.RenderVi ...

  7. LNMP配置——Nginx配置 ——域名重定向

    一.配置 #vi /usr/local/nginx/conf/vhost/test.com.conf 写入: server { listen 80; server_name test.com test ...

  8. 安全框架Drozer安装和简单使用

    安全框架Drozer安装和简单使用 说明: drozer(即以前的Mercury)是一个开源的Android安全测试框架 drozer不是什么新工具,但确实很实用,网上的资料教程都很多了,最近自己项目 ...

  9. [Python] 波士顿房价的7种模型(线性拟合、二次多项式、Ridge、Lasso、SVM、决策树、随机森林)的训练效果对比

    目录 1. 载入数据 列解释Columns: 2. 数据分析 2.1 预处理 2.2 可视化 3. 训练模型 3.1 线性拟合 3.2 多项式回归(二次) 3.3 脊回归(Ridge Regressi ...

  10. concurrentHashMap的put方法详解

    本文主要介绍ConcurrentHashMap的put操作如果有错误的地方欢迎大家指出. 1.ConcurrentHashMap的put操作 ConcurrentHashMap的put操作主要有3种方 ...