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 ...
随机推荐
- Ubuntu 设定壁纸自动切换的shell脚本
升级到Ubuntu14.04后,感觉bug的确比12.04少多了.顶部任务栏支持半透明效果,所以整个桌面也看上去漂亮了很多.这样的桌面也是值得瞎捣鼓一下的,想到换壁纸,但是没找到设定动态更换壁纸的选项 ...
- Hits算法
HITS(HITS(Hyperlink - Induced Topic Search) ) 算法是由康奈尔大学( Cornell University ) 的Jon Kleinberg 博士于1997 ...
- Java中的栈:java.util.Stack类
public class Stack<E>extends Vector<E>Stack 类表示后进先出(LIFO)的对象堆栈.它通过五个操作对类 Vector 进行了扩展 ,允 ...
- 鼠标滑过提示title
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1. ...
- String.Format数字格式化输出 {0:N2} {0:D2} {0:C2
//格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = ...
- HTML5 drag & drop 拖拽与拖放简介
DataTransfer 对象:退拽对象用来传递的媒介,使用一般为Event.dataTransfer. draggable 属性:就是标签元素要设置draggable=true,否则不会有效果,例如 ...
- C++ 对数组sizeof 和对数组元素sizeof
这一段程序 下面这段程序很有看点://arr1 is an array of intsint *source=arr1;size_t sz=sizeof(arr1)/sizeof(*arr1);//n ...
- [ Windows] [ OS ] [ Remote Desktop ] 開啟同一個帳號同時2的連線RDP的方式
感謝同事 Allen 的Support :) 執行>gpedit.msc 電腦設定>Windows元件>遠端桌面服務>遠端桌面工作階段主機>連線>限制遠端桌面服務的 ...
- WordPress 后台禁用Google Open Sans字体,加速网站
解决方法很简单,安装启用 Disable Google Fonts 或者 Remove Open Sans font Link from WP core 其中之一即可.或者如果你没有使用WP自带的官方 ...
- 解决BLOB/TEXT column can't have a default value query问题
Create table的时候,报错BLOB/TEXT column 'xxxxxx( 表名称)' can't have a default value query ,意思是TEXT类型的表字段不能够 ...