HDU1312——Red and Black(DFS)
Red and Black
Problem 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)
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
题目大意:
一个瓦片地图,'.'代表黑色的瓦片,'#'代表红色的瓦片,'#'是主人公站的位置,主人公只会下上左右四种移动方式,且只能去黑色的瓦片(初始位置也是黑色的瓦片);
求可以去的黑色瓦片个数,包括初始位置。
解题思路:
简单的DFS,搜索一下与初始位置上下左右相连的所有黑色瓦片,并记录输出即可。
Code;
#include<iostream>
#include<string>
#include<cstdio>
#define MAXN 50
using namespace std;
bool vis[MAXN+][MAXN+],is_black[MAXN+][MAXN+]; //黑色瓦片标记
char tile[MAXN+][MAXN+];
int n,m;
int dfs(int i,int j)
{ if (is_black[i][j]==||vis[i][j]==) return ; //搜索时遇到已经搜索过的或者红色瓦片则返回,不记录瓦片数。
vis[i][j]=;
int sum=;
if (i->=) sum+=dfs(i-,j); //搜索上下左右四种情况
if (i+<=n) sum+=dfs(i+,j);
if (j->=) sum+=dfs(i,j-);
if (j+<=m) sum+=dfs(i,j+);
return sum;
}
int main()
{
int first_i,first_j;
while (cin>>m>>n)
{
if (m==&&n==) break;
memset(is_black,,sizeof(is_black));
memset(vis,,sizeof(vis));
getchar();
for (int i=; i<=n; i++)
{
for (int j=; j<=m; j++)
{
cin>>tile[i][j];
if (tile[i][j]=='@') first_i=i,first_j=j,tile[i][j]='.'; //记录初始位置用于调用DFS,并用题意将初始位置转换成黑色瓦片(貌似没有必要--!)
if (tile[i][j]=='#') is_black[i][j]=;//用Is_Black数组标记瓦片颜色
else is_black[i][j]=;
}
getchar();
} printf("%d\n",dfs(first_i,first_j));
}
return ;
}
HDU1312——Red and Black(DFS)的更多相关文章
- 数据结构——HDU1312:Red and Black(DFS)
题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...
- HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏
Red and Black Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- HDU1312 Red and Black(dfs+连通性问题)
这有一间铺满方形瓷砖的长方形客房. 每块瓷砖的颜色是红色或者黑色. 一个人站在一块黑色瓷砖上, 他可以从这块瓷砖移动到相邻(即,上下左右)的四块瓷砖中的一块. 但是他只能移动到黑色瓷砖上,而不能移动到 ...
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1312 Red and Black (DFS)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ1312 Red and black(DFS深度优先搜索)
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
- hdu1312 Red and Black
I - Red and Black Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- I - Red and Black DFS
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
随机推荐
- SecureCRT for Linux突破30天使用限制
当然还有一种方法,就是当你试用点i agree到时候,在~/.vandyke/Config 会生成一个文件SecureCRT_eval.lic,删除以后就可以恢复30天试用
- C/C++输入输出总结
*string类: 1.cin>>string时,遇到'\n'或者空格即停止,并且'\n'或空格仍留在输入里,即只读了一个单词或什么都没读,但string类自己处理好了空字符什么的.下一 ...
- 十天学会零基础入门学习Photoshop课程(在线观看)
适合人群:在校学生 在职工作者 淘宝运营者等一系列会操作电脑的人群 课程目录 试学课 课时11前言 8分钟1秒 课时22工作界面 试学课 课时33文件的新建 试学课 课时44文档保存 11分钟24秒 ...
- Angularjs在线编辑器
1.TextAngular: https://github.com/fraywing/textAngular textAngular是一个强大的Text-Editor/Wysiwyg 编辑器,用于An ...
- jQuery ui 中文日历
jQuery ui 中文日历 <link href="css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet&qu ...
- github项目filter_firewall说明
本文编写的目的: 本文是对上传到github上的项目进行说明.github链接:filter_firewall有任何意见或者建议可以Email:18277973721@sina.cn 项目介绍: 包过 ...
- 关于HTML代码的转义
笔记: 1.在通过jsonp方式传输HTML代码的时候,为了防止代码中的一些字符影响json的语法,需要对HTML代码进行转义. 2.转义的时候可以只转义特殊字符(引号之类的),也可以把所有字符(中文 ...
- Using jQuery to add a dynamic “Back To Top” floating button with smooth scroll
Ever read a really long blog post or article and then had to scroll all the way up to the top of the ...
- SQL动态更新表字段 传入字段可能为空
小技巧: 项目组有修改产品的基本信息字段 但有时候传入的字段可能为空 也可能不为空 动态修改表中字段. USE [BetaProductMarket_DB] GO )) BEGIN DROP PRO ...
- web.xml中JSP配置及 EL表达式
web.xml 中JSP配置.servlet配置 及 EL表达式 [摘要] servlet 基本配置 <servlet> <servlet-name>LoginServlet& ...