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. Java多线程(三) 多线程间的基本通信

    多条线程在操作同一份数据的时候,一般需要程序去控制好变量.在多条线程同时运行的前提下控制变量,涉及到线程通信及变量保护等. 本博文主要总结:①线程是如何通信  ②如何保护线程变量 1.Java里的线程 ...

  2. HTTP 错误 404.2 解决方案

    HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面 详细错误:HTTP 错误 404.2 - Not Found ...

  3. 开源 侧滑 和 Tab滑动翻页 控件

    侧滑 https://github.com/jfeinstein10/SlidingMenu Tab滑动翻页 https://github.com/astuetz/PagerSlidingTabStr ...

  4. Asp.Net Web API VS Asp.Net MVC

    http://www.dotnet-tricks.com/Tutorial/webapi/Y95G050413-Difference-between-ASP.NET-MVC-and-ASP.NET-W ...

  5. Opencv 的数据结构

    opencv的基本数据结构 结构 成员 意义 CvPoint int x,y 图像中的点 CvPoint2D32f float x,y 二维空间中的点 CvPoint3D32f float x,y,z ...

  6. fedora -- java多版本切换

    一般java开发时会下载多个版本的SDK,所以需要多个版本中进行切换 1. 设置JAVA_HOME环境变量需要打开.bashrc文件 2. 安装时使用alternatives将不同版本的java连接到 ...

  7. Device disconnected

    问题:android 调试的时候,Logcat没有任何输出,提示Device  disconnected 解决:Devices -- Reset adb

  8. Oracle监听器—动态注册

    注册就是将数据库作为一个服务注册到监听程序.客户端不需要知道数据库名和实例名,只需要知道该数据库对外提供的服务名就可以申请连接到数据库.这个服务名可能与实例名一样,也有可能不一样. 注册分: 1. 静 ...

  9. Unity3d 如何找到游戏对象并改变其颜色

    //游戏对象 private var obj:GameObject; //渲染器 private var render:Renderer; //贴图 private var texture:Textu ...

  10. OO之策略模式

    以下为策略模式详解: 引子: 使用策略就是要实现可扩展性,那么多态是不可少的.何谓可扩展性呢? 比如:我们用面向对象的思想来设计飞机,基类为飞机,飞机可以有很多种,客机,直升机,战斗机等,不同种类的飞 ...