题目地址: 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[30][30];    //地板
int visited[30][30];    //访问标记,0表示未访问,1表示已访问
int num = 0;   //瓷砖数void dfs(int i, int j)
{visited[i][j] = 1;    //标记为已访问++num;if (Floor[i - 1][j] == '.' && !visited[i - 1][j])dfs(i - 1, j);    //往上走if (Floor[i][j - 1] == '.' && !visited[i][j - 1])dfs(i, j - 1);    //往左走if (Floor[i][j + 1] == '.' && !visited[i][j + 1])dfs(i, j + 1);    //往右走if (Floor[i + 1][j] == '.' && !visited[i + 1][j])dfs(i + 1, j);    //往下走

}int main()
{int W, H;while (cin >> W >> H && (W != 0 || H != 0))    //W是列数,H是行数
    {num = 0;    //将访问的黑瓷砖数置零for (int i = 0; i < 30; ++i)for (int j = 0; j < 30; ++j)Floor[i][j] = '#';        //将地板置零for (int i = 0; i < 30; ++i)for (int j = 0; j < 30; ++j)visited[i][j] = 0;        //将地板的访问状态置零int start_i, start_j;    //起点坐标//创建地板,二维数组的第1行和第1列不用,并且地板初始为30×30,足够大,//从而避免初始点落在边界上调用dfs时产生的数组越界问题for (int i = 1; i <= H; ++i)for (int j = 1; 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 0;}

转载于:https://www.cnblogs.com/FengZeng666/p/10381694.html

POJ 1979 红与黑相关推荐

  1. Red and Black 红与黑 POJ 1979 深度搜索算法

    原题 Red and Black 题意: 只走黑砖,不走红砖,最多能走多少 黑砖. 有一个长方形的房间,覆盖了正方形的磁砖.每块磁砖的颜色,要么是红色,要么是黑色.一名男子站在一块黑色的磁砖上.他  ...

  2. poj 1979 Red and Black(BFS)

    题意:在一个矩形房间里面被瓦片覆盖,分为白色的和红色的,白的可以走,红的不能走,起始位置在白色的瓦片上,可以上下左右移动: ".":白色的瓦片: "#":红色的 ...

  3. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

  4. POJ 1979 Red and Black DFS

    简单DFS 注意边界就行了. // #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstd ...

  5. POJ 1979: Red and Black

    2019独角兽企业重金招聘Python工程师标准>>> 题目在此 解题思路:直接 DFS 或 BFS 就行了. 之前被 STL 拖过后腿,偏执劲儿又上来了,这次刻意不用 std::q ...

  6. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  7. nomasp 博客导读:Lisp/Emacs、Algorithm、Android

    Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸,只要我的技术有提升,这个博客就会一直更新下去,感谢您的支持,欢迎您的关注与留言.目前博客有多个专栏,分别是关于 ...

  8. nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……

    Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸.本博客会持续更新.感谢您的支持.欢迎您的关注与留言.博客有多个专栏,各自是关于 Android应用开发 .Wi ...

  9. 《挑战程序设计竞赛(第2版)》习题册攻略

    本项目来源于GitHub 链接: 项目GitHub链接 1 前言 项目为<挑战程序设计竞赛(第2版)>习题册攻略,已完结.可配合书籍或笔记,系统学习算法. 题量:约200道,代码注释内含详 ...

最新文章

  1. HLG 1481 Attack of the Giant n-pus【二分+二分图完全匹配】
  2. 清华通信本硕巨佬秋招经验总结,收割互联网大厂后端 SP/SSP offer,太强了!
  3. python pip命令技巧
  4. idea(三)最值得安装的几款插件
  5. 配置项、基线以及软件配置控制委员会
  6. hbase建表语句_HBase 中文参考指南真不少~
  7. 建立一个vs+qt打开系统摄像头的程序
  8. Canal中间件学习总结
  9. 空洞卷积(膨胀卷积)的相关知识以及使用建议(HDC原则)
  10. 2020年各省二建房建挂靠价格汇总
  11. Keil5 平台 S3C2440裸机程序开发-----定时器中断
  12. 2019春第十二周作业
  13. 人生的三重境界(山在那?水在那?)
  14. sql查询数据库中所有表名
  15. 非计算机专业人员的程序之路
  16. PTA7-1 厘米换算英尺英寸
  17. PTA 作业 福到啦
  18. Linux配置JAVA环境变量(全部)
  19. 一加3t运行linux,一加3T手机A3010系统运行速度变慢变卡顿了_怎么进行刷机教程解决...
  20. LeetCode刷题之路:11. 盛最多水的容器

热门文章

  1. 本次安装visual studio所用的安装程序不完整_阁楼影院安装案例
  2. python数值类型教程_Python数值类型 int、float、complex 详解
  3. 有序列表ol与无序列表ul用法
  4. 使用JMeter 进行接口并发性能测试
  5. mfc搜索新建access字段_vs2010MFC中使用ODBC链接ACCESS数据库,怎样编写查找功能?...
  6. 没有期刊申请清华博士_ICLR飞升,IJCAI降级:清华的新版AI顶会评级引发学术圈热议...
  7. Learning to Segment Object Candidates
  8. hibernate4调用mysql存储过程_Hibernate4.x执行mysql的存储过程
  9. 图像数集据增广的15+种功能总结和Python代码实现
  10. pdf增强锐化软件_终于找到这款神器!高级锐化插件 让你的画面更清晰