题目描述

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. JQ 让光标在文本框最末尾

    function setFocus() { //文本末尾获得焦点 var obj = event.srcElement; var txt = obj.createTextRange(); txt.mo ...

  2. Linq 调试

    void Main() { var MyMonitor = new Devart.Data.Oracle.OracleMonitor(); MyMonitor.IsActive = true; var ...

  3. UITableViewCell 左滑删除

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...

  4. Flightgear 编译

    一.FlightGear简介 FlightGear 始于1997年,是一个开源的多平台飞行模拟器. 二.FlightGear编译过程 FlightGear平台的说明文档见:http://wiki.fl ...

  5. Extjs 4学习2

    主要学习采自:http://www.ishowshao.com/blog/2012/06/19/extjs-4-getting-started/ 用的sdk为extjs4.2.1 根据其中的提示装了一 ...

  6. centos 安装vnc服务

    1.安装tigervnc-server yum install tigervnc-server 2.启动vnc服务 vncserver:1 [错误提示待解决bad display name " ...

  7. xv6中存储cpu和进程信息的技巧

    xv6是一个支持多处理器的Unix-like操作系统, 近日阅读源码时发现xv6在记录当前CPU和进程状态时非常tricky 首先,上代码: extern struct cpu cpus[NCPU]; ...

  8. splice从数组中删除指定定数据

    /*从数组中删除指定定数据var somearray = ["mon", "tue", "wed", "thur"]so ...

  9. asp.net在应用母版的页面下采用了ModalPopupExtender弹出窗中应用autocomplete

    autocomplete是jqueryUI的一个插件,可以实现自动填充的功能. 要点:1.应用了母版页,所以取页面上控件的ID时与一般方法不同 2.由于用了ajax的updatepanel,所以会出现 ...

  10. myisam和innodb区别

      InnoDB MyIsam 事务                        支持 不支持 锁 行锁 表锁 索引 B+树,数据和索引在一个文件中,必须有主键,如果不指定,会自动生成一个隐藏字段作 ...