题目地址: http://poj.org/problem?id=1979  或者  https://vjudge.net/problem/OpenJ_Bailian-2816

        Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 46793   Accepted: 25201

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

一开始我的代码没有在每次输入前将地板置零,导致在输入11 6这组数据时出错,因为在之前输入11 9 这组数据时已经将11 6 外面的地板置为了.或者#, 之后

再测试11 6 时,由于没有置空地板,所以11 9 时在11 6 外面的地板仍然保留了下来,导致11 6 这组测试数据的结果产生错误。

正确代码:
#include <iostream>

using namespace std;

char Floor[][];    //地板
int visited[][]; //访问标记,0表示未访问,1表示已访问
int num = ; //瓷砖数 void dfs(int i, int j)
{
visited[i][j] = ; //标记为已访问
++num;
if (Floor[i - ][j] == '.' && !visited[i - ][j])
dfs(i - , j); //往上走
if (Floor[i][j - ] == '.' && !visited[i][j - ])
dfs(i, j - ); //往左走
if (Floor[i][j + ] == '.' && !visited[i][j + ])
dfs(i, j + ); //往右走
if (Floor[i + ][j] == '.' && !visited[i + ][j])
dfs(i + , j); //往下走 } int main()
{
int W, H;
while (cin >> W >> H && (W != || H != )) //W是列数,H是行数
{
num = ; //将访问的黑瓷砖数置零 for (int i = ; i < ; ++i)
for (int j = ; j < ; ++j)
Floor[i][j] = '#'; //将地板置零 for (int i = ; i < ; ++i)
for (int j = ; j < ; ++j)
visited[i][j] = ; //将地板的访问状态置零 int start_i, start_j; //起点坐标 //创建地板,二维数组的第1行和第1列不用,并且地板初始为30×30,足够大,
//从而避免初始点落在边界上调用dfs时产生的数组越界问题
for (int i = ; i <= H; ++i)
for (int j = ; j <= W; ++j)
{
cin >> Floor[i][j];
if (Floor[i][j] == '@') //记录下起点坐标
{
start_i = i;
start_j = j;
}
} dfs(start_i, start_j);
cout << num << endl; } return ; }

POJ 1979 红与黑的更多相关文章

  1. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

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

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

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

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

  4. POJ 1979 dfs和bfs两种解法

      fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...

  5. poj 1979 Red and Black(dfs)

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

  6. POJ 1979 DFS

    题目链接:http://poj.org/problem?id=1979 #include<cstring> #include<iostream> using namespace ...

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

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

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

  9. DFS:Red and Black(POJ 1979)

    红与黑 题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间? 不多说,DFS深搜即可,水题 注意一下不要把行和列搞错就好了,我就是那样弄错过一 ...

随机推荐

  1. 监听 input上传文件, 获取文件名称,

    <div class="import-box pr" > <span class="model-address-txt">导入文件:&l ...

  2. 技术的热门度曲线:GHC

      全球最大的 IT 咨询公司高德纳(Gartner),有一个"技术热门度曲线"模型(Gartner Hype Cycle). 该模型认为,一门技术的发展要经历五个阶段. (1)启 ...

  3. Java二叉树的实现与特点

    二叉树是一种非常重要的数据结构,它同时具有数组和链表各自的特点:它可以像数组一样快速查找,也可以像链表一样快速添加.但是他也有自己的缺点:删除操作复杂. 我们先介绍一些关于二叉树的概念名词. 二叉树: ...

  4. 【进阶3-4期】深度解析bind原理、使用场景及模拟实现(转)

    这是我在公众号(高级前端进阶)看到的文章,现在做笔记  https://github.com/yygmind/blog/issues/23 bind() bind() 方法会创建一个新函数,当这个新函 ...

  5. layui前端框架

    项目中需要弹出层效果,使用了layui前端框架,主要使用了里面的弹出层特效(可以移动) html代码 要给这个标签绑定click方法 <a href='javascript:;' data-me ...

  6. Confluence 6 安全相关问题提交链接

    找到和报告安全漏洞 Atlassian 有关安全漏洞的报告细节,请参考如何报告一个安全问题(How to Report a Security Issue)链接. 发布 Confluence 安全公共 ...

  7. Confluence 6 从一个备份中获得文件附件

    页面中的文件附件可以从备份中获得而不需要将备份文件导入到 Confluence 中.这个在用户删掉了附件,但是你还是想恢复这个附件的时候就变得非常有用了. 自动备份和手动备份都允许你进行这个操作,但是 ...

  8. Confluence 6 有关 AD 的一些特殊说明

    当应用程序对使用 Active Directory (AD) 的 LDAP 服务器进行同步的时候,同步的任务只对 LDAP 最近修改的数据进行同步而不是对整个数据库进行同步.因为是增量同步,在第一次完 ...

  9. Flex布局新旧混合写法详解

    flex是个非常好用的属性,如果说有什么可以完全代替 float 和 position ,那么肯定是非它莫属了(虽然现在还有很多不支持 flex 的浏览器).然而国内很多浏览器对 Flex 的支持都不 ...

  10. 《剑指offer》 跳台阶

    本题来自<剑指offer> 跳台阶 题目1: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 思路: 同上一篇. C ...