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. session会话管理,与过滤器使用,访问控制

    1 用户登录,是否注册用户,在登录处理页面进行用户验证,创建session保存用户名和密码 2否,进入用户注册页面 3是,系统保存该用户的登录信息 4进入要访问的页面 5用户直接访问某个页面, 6系统 ...

  2. webdriver自动化脚本

    1.需求如下: 启动火狐浏览器首先打开百度,等待3秒然后打开博客首页,等待2秒然后关闭浏览器 from selenium import webdriver from time import sleep ...

  3. 关于HSTS安全协议的全面详细解析

    HTTP 严格传输安全(HSTS)是一种安全功能,web 服务器通过它来告诉浏览器仅用 HTTPS 来与之通讯,而不是使用 HTTP.HSTS是网站从HTTP到HTTPS中网站性能及安全优化非常重要的 ...

  4. 刚刚明白了for循环写三角形

    for(int a = 15; a >=1; a--) { for(int b = a - 1; b >=0; b--) { System.out.print("A") ...

  5. tf.unstack()、tf.stack()

    tf.unstack 原型: unstack( value, num=None, axis=0, name='unstack' ) 官方解释:https://tensorflow.google.cn/ ...

  6. 6.ZigZag Conversion(Graph, traverse)

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  7. 4. Median of Two Sorted Arrays(Array; Divide-and-Conquer)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  8. 运行php网站需要安装什么

    php的运行环境: 为了能够运行php,有以下两种方法: 1. 使用支持php和MySQL的web主机(): 2. 本机(自己电脑)安装web服务器,然后安装MySQL和php. web虚拟主机: 大 ...

  9. 快速排序C++实现

    #include<iostream> using namespace std;class quicksort{ public: int quicks(int *a,int low,int ...

  10. [leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...