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. C#调用opencv

    最经做一个项目,底层调用openCV编写的图像处理程序,用户界面采用C#编写. 于是学习了相关技术,总结如下: C#编写的是托管代码,编译生成微软中间语言,而普通C++代码则编译生成本地机器码,这两种 ...

  2. php 缓存加速器软件

    Xcache 和 memcached 是两个不同层面的缓存,不存在可比性.Xcache 是 php 底层的缓存,它将PHP程式编译成字节码(byte code),再透过服务器上安装对应的程式来执行PH ...

  3. Http的常见问题

    A: HTTP(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议. B: 文件传输协议FTP.电子邮件传输协议SMTP.域名系统服务DNS.HTTP协议等都同是应用层协议. C:HT ...

  4. 为Docker容器配置固定IP

    当docker以桥接的方式启动容器时,容器内部的IP是经过DHCP获取的,例如:172.17.0.8/32,且每重启依次IP都会发生变动.某些特殊的情况下,需要容器内有自己固定的一个内部IP.我的实现 ...

  5. 使用mysql profiling功能剖析单条查询

    5.1版本开始引入show profile剖析单条语句功能,支持show profiles和show profile语句,参数have_profiling;控制是否开启: 查看是否支持这个功能(查询为 ...

  6. List与Set的contains方法效率问题

    今天看到网上一篇文章说:Set检索元素效率低下,删除和插入效率高:List查找元素效率高,插入删除元素效率低.于是想到List虽然用get(index)方法查询效率高,但是若用contains方法查询 ...

  7. python :生产者和消费者模型 即简单的协程

    def consumer(name): print('%s开始准备吃包子了' %name) while True: baozi=yield print('[%s]包子来了,被[%s]吃了' %(bao ...

  8. SQL数据类型大全 《转自网络》

    数据类型是数据的一种属性,表示数据所表示信息的类型.任何一种计算机语言都定义了自己的数据类型.当然,不同的程序语言都具有不同的特点,所定义的数据类型的种类和名称都或多或少有些不同.SQLServer ...

  9. app framework map及ajax方法

    $(function () { $.ajax({ url: 'Ashx/GetProductList.ashx', contentType: "JSON", success: fu ...

  10. 从invoke简单理解反射

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