POJ 1979 红与黑
题目地址: http://poj.org/problem?id=1979 或者 https://vjudge.net/problem/OpenJ_Bailian-2816
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 46793 | Accepted: 25201 |
Description
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
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
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 红与黑的更多相关文章
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑
1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- POJ 1979 dfs和bfs两种解法
fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...
- poj 1979 Red and Black(dfs)
题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...
- POJ 1979 DFS
题目链接:http://poj.org/problem?id=1979 #include<cstring> #include<iostream> using namespace ...
- 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 ...
- poj 1979 Red and Black 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...
- DFS:Red and Black(POJ 1979)
红与黑 题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间? 不多说,DFS深搜即可,水题 注意一下不要把行和列搞错就好了,我就是那样弄错过一 ...
随机推荐
- Spring Boot (一): Spring Boot starter自定义
前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...
- ES的Query、Filter、Metric、Bucketing使用详解
由于笔者在实际项目仅仅将ES用作索引数据库,并没有深入研究过ES的搜索功能.而且鉴于笔者的搜索引擎知识有限,本文将仅仅介绍ES简单(非全文)的查询API. 笔者原本打算在本文中介绍聚合API的内容,但 ...
- v-html里面添加样式
项目中,使用V-html渲染的页面,要添加样式,改如何做 解决方案1:在updated生命周期函数中,js动态配置样式,代码如下: updated() { $('.msgHtmlBox').find( ...
- Oracle 行转列pivot 、列转行unpivot 的Sql语句总结
这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...
- npm常用模块
https://blog.csdn.net/heliumlau/article/details/70577727
- android入门小结一
一 Android入门基础:从这里开始 gradle介绍: Android Studio使用Gradle 编译运行Android工程. 工程的每个模块以及整个工程都有一个build.gradle文件. ...
- 最短路径之Bellman-Ford算法
第一行为源点个数,边的个数m 接下来m行为a->b和权值 5 52 3 21 2 -31 5 54 5 23 4 3 Bellman-Ford算法(1): #include<iostrea ...
- Saruman's Army(POJ3069)
Description Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. ...
- bzoj2973转移矩阵构造法!
/* 构造单位矩阵(转移矩阵) 给定n*m网格,每个格子独立按照长度不超过6的操作串循环操作 对应的操作有 0-9:拿x个石头到这个格子 nwse:把这个格子的石头推移到相邻格子 d:清空该格石子 开 ...
- Java 获取当前系统的时间
获取当前系统的时间,每隔一秒,打印一次. import java.util.Date; public class TestDate { public static void main(String[] ...