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)的更多相关文章

  1. 数据结构——HDU1312:Red and Black(DFS)

    题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or blac ...

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

  3. HDU1312 Red and Black(dfs+连通性问题)

    这有一间铺满方形瓷砖的长方形客房. 每块瓷砖的颜色是红色或者黑色. 一个人站在一块黑色瓷砖上, 他可以从这块瓷砖移动到相邻(即,上下左右)的四块瓷砖中的一块. 但是他只能移动到黑色瓷砖上,而不能移动到 ...

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

  5. HDU 1312 Red and Black (DFS)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

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

  7. HDOJ1312 Red and black(DFS深度优先搜索)

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

  8. hdu1312 Red and Black

    I - Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. I - Red and Black DFS

    There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...

随机推荐

  1. Visual Studio 2015和.Net 2015 预览版在线安装和ISO镜像安装光盘下载

    微软刚刚宣布了 Visual Studio 2015和.Net 2015 预览版,并同时提供了下载. 微软在纽约正进行中的#Connect# 全球开发者在线大会上宣布了Visual Studio 20 ...

  2. Android Wear预览版——尝鲜

    前两天Google推出了Android Wear的SDK,稍稍的瞧了一眼,发现这个预览版的功能还是比较简单的,只有一个通知转发的功能,不过就这么一个功能,带来的效果却是Very Good~~ 功能:发 ...

  3. php调试利器 -- xdebug

    之前整理在印象笔记中,现在搬出来.分本地调试和远程调试.本文亲测通过并截图,有问题欢迎留言探讨. (参考网上多位前辈的博客,整理在笔记里忘记保存原文链接,这里无法贴出链接,望见谅)   # 痛处 一般 ...

  4. Python在Windows下开发环境配置汇总

    最近比较关注学习Python方面的资料和课程,由于Python本身基本都是在Linux下开发,本人windows用习惯了初用Linux各种别扭啊. 下面将我在配置Windows环境下的禁言写出来,与大 ...

  5. WPF 概述

    WPF 全称是:Windows Presentation Foundation,直译为Windows表示基础.WPF是专门为GUI(Graphic User Interface)程序开发设计的. 在过 ...

  6. PIL 安装

    1.安装依赖包 1.1 ubuntu安装 apt-get install python-devapt-get install libjpeg-dev apt-get install libjpeg8- ...

  7. Java实现mysql数据库备份

    Runtime是一个与JVM运行时环境有关的类,这个类是Singleton的. Runtime.getRuntime()可以取得当前JVM的运行时环境,这也是在Java中唯一一个得到运行时环境的方法. ...

  8. envi中多波段图层叠加layer stacking

    Basic Tools——layer stacking 选择投影和输出的文件 波段1-7波段图层都叠加在一个文件中了

  9. Eval 表达式 GridView ItemCommand

    <asp:TemplateColumn HeaderText="查看审批数据">    <ItemTemplate>     <a onclick=& ...

  10. fineui框架

    http://fineui.com/demo/#/demo/layout/fit.aspx 虽然比较丑陋,但功能实用 此框架比较简单, 框架的作用你懂的,重点是要有帮助文档, 进阶型的容易上手的帮助文 ...