题目描述

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.

输入

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)

The end of the input is indicated by a line consisting of two zeros.

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 解题思路:
深搜的方法解决,题目意思就是从@开始找.并与@连通,碰到#等于碰到了墙,题目很简单,@可以向四个方向上、下、左、右走,所以 用四个坐标标记出来,然后,再一一遍历,递归调用寻找,用一个30*30的数组标识此点有没有走过,避免走重复 程序代码:
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,cot;
char map[][];
int to[][] = {{,},{,},{-,},{,-}}; void dfs(int i,int j)
{
cot++;
map[i][j] = '#';
for(int k = ; k<; k++)
{
int x = i+to[k][];
int y = j+to[k][];
if(x<n && y<m && x>= && y>= && map[x][y] == '.')
dfs(x,y);
}
return;
} int main()
{
int i,j,fi,fj;
while(~scanf("%d%d%*c",&m,&n)&&m&&n)
{
for(i = ; i<n; i++)
{
for(j = ; j<m; j++)
{
scanf("%c",&map[i][j]);
if(map[i][j] == '@')
{
fi = i;
fj = j;
}
}
getchar();
}
cot= ;
dfs(fi,fj);
printf("%d\n",cot);
} return ;
}

数据结构——HDU1312:Red and Black(DFS)的更多相关文章

  1. HDU1312——Red and Black(DFS)

    Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...

  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. 数据结构:关键路径,利用DFS遍历每一条关键路径JAVA语言实现

    这是我们学校做的数据结构课设,要求分别输出关键路径,我查遍资料java版的只能找到关键路径,但是无法分别输出关键路径 c++有可以分别输出的,所以在明白思想后自己写了一个java版的 函数带有输入函数 ...

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

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

  9. hdu1312 Red and Black

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

随机推荐

  1. Android layout的横竖屏处理

    一.layout-land和layout-prot的区别与使用 默认情况下,创建的Android项目里只有一个layout文件夹,尽管这样也可以横竖屏切换用,但是某些布局横屏过后闲的格外的丑,如下图 ...

  2. sql server查询时候注意的基本优化常识

    1.在Where条件中尽量不要在=号左边进行函数.运算符.或表达式计算 2.在Where中尽量避免出现!=或<>操作符: 3.在Where中尽量避免对字段进行null值判定: 4.使用Li ...

  3. [笔记]SD卡相关资料

    ESD静电放电模块 我知道的flash分为两种NOR flash和NAND flash,NOR falsh容量一般为1~16M用于单片机代码存储,NAND flash最小的是8M最大的现在听说有90G ...

  4. iOS中JavaScript和OC交互

    转载自:http://www.devzeng.com/blog/ios-uiwebview-interaction-with-javascript.html 还可参考的文章:http://blog.c ...

  5. 334. Increasing Triplet Subsequence My Submissions Question--Avota

    问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...

  6. 第五篇、 WebSphere8.5的安装

    一.前言 WebSphere Application  Server 是IBM企业级应用服务器,与WAS6,WAS7相比较而言 WAS8发生了很大的改变,其安装介质和以前截然不同,该篇章中对于不同的安 ...

  7. date日期比较和格式化方法

    时间的比较 var now = new Date();//现在时间 var setDate = new Date(2017,0,5,17,55,55);//设定的时间 laert(now < s ...

  8. js切换换class

    1, js代码 function ntabs(thisObj,Num)            {if(thisObj.className == "active")return;   ...

  9. [原创]用python写了一个简单的markdown编辑器

    以前我常用openoffice记录东西,最喜欢它的当然是在linux上能用了,还有里面的公式输入,前几天才了解markdown这个东东,初步了解发现它正是我需要的东西,可以用它随心所欲地记录些东西,而 ...

  10. HDU 1069 Monkey and Banana(LIS最长上升子序列)

    B - LIS Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descripti ...