POJ 1979 Red and Black 红与黑

Time Limit: 1000MS    Memory Limit: 30000K

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.

多组测试用例。每组数组开头有两个正整数W和H;W与H分别表示 x- 与 y- 方向上瓷砖的数量。W和W均不超过20。

还有H行数据,每行包含W个字符。每个字符表示各色瓷砖如下。

‘.’- 一块黑砖

‘#’- 一块红砖

‘@’- 一个黑砖上的人(一组数据一个人)

输入以一行两个零为结束。

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 - 输入样例

Sample Output - 输出样例

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
45
59
6
13

【题解】

  数据不大,DFS可解。

【代码 C++】

 #include <cstdio>
#include <cstring>
char data[][];
int sum;
void DFS(int y, int x){
if (data[y][x] == '#') return;
++sum; data[y][x] = '#';
DFS(y + , x); DFS(y - , x);
DFS(y, x + ); DFS(y, x - );
}
int main(){
int w, h, i, j, stY, stX;
while (~scanf("%d%d ", &w, &h)){
if (w + h == ) break;
memset(data, '#', sizeof(data));
for (i = ; i <= h; ++i){
gets(&data[i][]);
for (j = ; j <= w; ++j) if (data[i][j] == '@') stY = i, stX = j;
data[i][j] = '#';
}
sum = ; DFS(stY, stX);
printf("%d\n", sum);
}
return ;
}

POJ 1979 Red and Black (红与黑)的更多相关文章

  1. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

  2. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  3. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  4. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

  5. POJ 1979 Red and Black (zoj 2165) DFS

    传送门: poj:http://poj.org/problem?id=1979 zoj:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  6. HDOJ 1312 (POJ 1979) Red and Black

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  7. poj 1979 Red and Black(dfs水题)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  8. POJ 1979 Red and Black (DFS)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  9. POJ 1979 Red and Black 四方向棋盘搜索

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 50913   Accepted: 27001 D ...

随机推荐

  1. JAVA 集合List,数组,Set,Map,直接的相互转换

    Java集合转换[List<-->数组.List<-->Set.数组<-->Set.Map-->Set.Map-->List] //List--> ...

  2. 鸟哥的linux私房菜学习记录之程序管理和SElinux初探

    process是进程的意思也就是说进程是正在运行的程序 将后台程序的错误信息等等输出到某个文档 终端关闭后会停止运行,如果想终端关闭后继续运行可以使用nohup命令,man nohup.

  3. windows prompt personalize 设置cmd提示的相关

    由于有一篇随笔种我说要引用这篇文章,所以不得已也出来了,就像你说大话『我明天去吃屎』,结果你做到了. 我这记录一下有关windows prompt这是的变量,我不知道这算不算变量,因为windows变 ...

  4. web.xml完整配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java ...

  5. PRINCE2七大原则(1)

    PRINCE2七大原则(1) 我们先来回顾一下,PRINCE2七大原则分别是持续的业务验证,经验学习,角色与责任,按阶段管理,例外管理,关注产品,剪裁. 第二个原则:吸取经验教训. PRINCE2要求 ...

  6. 从invoke简单理解反射

    前言 程序集   : 程序集是.NET应用程序的基本单位,包含了程序的资源.类型元数据和MSIL代码.根据程序集生成方式的不同,可分为静态程序集和动态程序集.程序集又可分为单文件程序集和多文件程序集, ...

  7. jquery 实现页面局部刷新ajax做法

    这个方法就多了去了,常见的有以下几种:下面介绍全页面刷新方法:有时候可能会用到 window.location.reload()刷新当前页面. parent.location.reload()刷新父亲 ...

  8. Android listview下拉刷新 SwipeRefreshLayout

    今天在Google+上看到了SwipeRefreshLayout这个名词,遂搜索了下,发现竟然是刚刚google更新sdk新增加的一个widget,于是赶紧抢先体验学习下. SwipeRefreshL ...

  9. Ugly Numbers

    Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21918 Accepted: 9788 Descrip ...

  10. Python学习笔记-Day2-Python基础之列表操作

    列表的常用操作包括但不限于以下操作: 列表的索引,切片,追加,删除,切片等 这里将对列表的内置操作方法进行总结归纳,重点是以示例的方式进行展示. 使用type获取创建对象的类 type(list) 使 ...