Red and Black

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 45   Accepted Submission(s) : 34

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem 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)

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

————————————————————————————————————————————————————

找出@所在位置的连通块,直接DFS搜下


#include<iostream>
#include<cmath>
using namespace std;
char map[105][105];
int m, n, t;
int dir[8][2] = {{ -1, 0 },{ 0, -1 }, { 0, 1 },{1, 0 }}; void dfs(int si, int sj)
{
if (si <= 0 || sj <= 0 || si > m || sj > n)
return;
t++;
for (int i = 0; i < 4; i++)
{
if (map[si + dir[i][0]][sj + dir[i][1]] != '#')
{
map[si + dir[i][0]][sj + dir[i][1]] = '#';
dfs(si + dir[i][0], sj + dir[i][1]);
}
}
return;
}
int main()
{
int si, sj;
while (cin >> n >> m&&(m||n))
{
for (int i = 1; i <= m;i++)
for (int j = 1; j <= n; j++)
cin >> map[i][j];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
{
if (map[i][j] == '@')
{
si = i;
sj = j;
break;
}
} map[si][sj] = '#';
t = 0;
dfs(si, sj);
printf("%d\n", t);
}
return 0; }

HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏的更多相关文章

  1. HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏

    Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...

  2. Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  4. HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏

    FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...

  5. HDU2212 DFS 2016-07-24 13:52 56人阅读 评论(0) 收藏

    DFS Problem Description A DFS(digital factorial sum) number is found by summing the factorial of eve ...

  6. HDU1181 变形课(DFS) 2016-07-24 13:31 73人阅读 评论(0) 收藏

    变形课 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒 ...

  7. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  8. 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 ...

  9. Hdu1427 速算24点 2017-01-18 17:26 46人阅读 评论(0) 收藏

    速算24点 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

随机推荐

  1. java多线程实例(2)

    public class ThreadDemo05 { public static void main(String args[]) { // 四个售票点应该控制同一个资源 Demo d = new ...

  2. Java 命令行运行参数

    Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOME"bin"java –option 来启动,-option为虚拟 ...

  3. 在c#下用 WCF编写restful

    1.添加WCF服务库 2.在global里面注册路由 RouteTable.Routes.Add(new ServiceRoute("api", new WebServiceHos ...

  4. hdoj1160 DP--LIS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 思路: 又是一道LIS的应用题,先预处理,按照w从小到大排列,那么原问题就转变成求该排列的LIS ...

  5. LuoguP1032 字符变换(BFS)

    题目链接为:https://www.luogu.org/problemnew/show/P1032 思路:看到数据比较小,而且最多有6个规则,就可以用搜索去做了,我用的BFS,大体思路如下: 定义结构 ...

  6. C函数指针的用法

    1.最简单的用法: #include <cstdio> int (*p)(int);//定义一个函数指针变量p(下面的f其实是一个常量函数指针) int f(int x) { printf ...

  7. collections系列之Counter

    collections模块中有一个叫做Counter的类,该类的作用就是计数器,Counter是对dict的加工,所有Counter继承了dict的方法 1.创建一个Counter,需要import ...

  8. 迭代器&生成器&yield异步

    迭代器 #迭代器是访问集合元素的一种形式,迭代器从集合的第一个元素开始访问,直到所有的元素被访问# 结束才结束,迭代器只能往前访问,不能往后访问,比如你先访问1,在访问2,在访问3.如果已经# 访问到 ...

  9. 对于局部变量,text、ntext 和 image 数据类型无效

    开发存储过程时报如上错误.大多数人说用varchar(8000)代替text,但值我这里超过8000,不可取 解决: sql2005或以上版本支持新数据类型:varchar(max)nvarchar( ...

  10. ios - 设置一个VC的navigationController的显示图案或文字,其他navigationController依旧不变

    1. override func viewDidLoad() { super.viewDidLoad() self.navigationController?.delegate = self } 2. ...