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. Kubernetes-5.Pod资源控制器(1)

    docker version:20.10.2 kubernetes version:1.20.1 本文概述Kubernetes Pod资源控制器的ReplicaSet.Deployment.Daemo ...

  2. Python高级——多任务编程之线程

    转: Python高级--多任务编程之线程 文章目录 线程概念 1. 线程的介绍 2. 线程的概念 3. 线程的作用 多线程的使用 1. 导入线程模块 2. 线程类Thread参数说明 3. 启动线程 ...

  3. C#正则实现匹配一块代码段

    最近项目,生成聚合网关,但是生成的网关文件中,存在着不必要的代码段,比如一个类A,类B等 之前一直使用手动删除,这么做劳民伤财,浪费时间,考虑使用正则写一个工具实现自动删除. 正则写法: string ...

  4. js--this指向的相关问题

    前言 关于this的指向问题是前端面试中常考的知识点,也是我们开发学习中较难理解的问题.作为JavaScript的基础,需要我们彻底理解这一关键词.this作为JavaScript中非常复复杂的机制, ...

  5. JS(ES6)、Vue.js、node.js

    JS行为(ESMAScript, JSdom, bom)$.ajax() <- (xmlhttpRequest由这个封装来的)  -> axios(vue版)  =  ajax技术jque ...

  6. FreeRedis分布式锁实现以及使用

    前言 今日上班听到同事在准备面试题分布式锁(准备溜溜球),随即加入了群聊复习了一波,于是有了这篇小作文. 场景 本文中的演示 DEMO, 以下订单减库存为例. 无锁裸奔表现 示例代码: 先来模拟一个库 ...

  7. 如何在 ASP.Net Core 中使用 Lamar

    ASP.Net Core 自带了一个极简的 开箱即用 的依赖注入容器,实际上,你还可以使用第三方的 依赖注入容器 来替代它,依赖注入是一种设计模式,它能够有效的实现对象之间的解耦并有利于提高单元测试和 ...

  8. C# 应用 - 多线程 6) 处理同步数据之手动同步 AutoResetEvent 和 ManualResetEvent

    1. 类的关系 AutoResetEvent 和 ManualResetEvent 都继承自 System.Threading.EventWaitHandle 类(EventWaitHandle 继承 ...

  9. Hibernate学习实例

    一 Hibernate简介 Hibernate是一种Java语言下的对象关系映射(ORM)解决方案.为面向对象的领域模型到传统的关系型数据库的映射提供了一个使用方便的框架. 二 Hibernate设计 ...

  10. fianl关键词

    一.final关键字概述 final关键字具有最终或不可改变的含义,可用于修饰类.变量.方法.因此被final修饰的类.变量.方法具有以下特征: --final修饰的类不能被继承: --final修饰 ...