HDU1312:Red and Black

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

.#.........

.#.#######.

.#.#.....#.

.#.#.###.#.

.#.#[email protected]#.#.

.#.#####.#.

.#.......#.

.#########.

...........

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

###.###

[email protected]

###.###

..#.#..

..#.#..

0 0

Sample Output

45

59

6

13

dfs搜索,懒得解释,自己看代码。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,ans;
char map[25][25];
int yi[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y)
{
    int i,rx,ry;
    map[x][y]=‘#‘;
    ans++;
    for (i=0;i<4;i++)
    {
        rx=x+yi[i][0];
        ry=y+yi[i][1];
        if (rx>=1&&rx<=n&&ry>=1&&ry<=m&&map[rx][ry]==‘.‘)
        {
            dfs(rx,ry);
        }
    }
}
int main()
{
    int i,j,x,y;
    while (~scanf("%d%d",&m,&n))
    {
        if (n==0&&m==0) break;
        ans=0;
        for (i=1;i<=n;i++)
        for (j=1;j<=m;j++)
        {
            scanf(" %c",&map[i][j]);
            if (map[i][j]==‘@‘)
            {
                x=i;
                y=j;
            }
        }
        dfs(x,y);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-19 07:44:35

HDU1312:Red and Black的相关文章

HDU1312:Red and Black(DFS)

题目描述 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 blac

hdu1312(Red and Black)

点击打开杭电1312 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

Linux:Red Hat系统的安装

今天高兴,所以我写这一期,这一期写的是Red Hat系统的安装,这个开发系统也是红帽企业制作出来的,关于这款系统的相关资料就自行百度吧!话不多说,直接进入这期的内容吧! 安装Red Hat系统 系统下载 RHEL7.5 最近正式发布,以下是下载地址: https://blog.csdn.net/msdnchina/article/details/79944819 百度云链接: 链接:https://pan.baidu.com/s/1BnsXL4ju6o5QT1dpGPA5Hg 密码:5n5z 系

HDU - 1312 : Red and Black

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 til

Hadoop:Windows 7 32 Bit 编译与运行

所需工具 1.Windows 7 32 Bit OS(你懂的) 2.Apache Hadoop 2.2.0-bin(hadoop-2.2.0.tar.gz) 3.Apache Hadoop 2.2.0-src(hadoop-2.2.0-src.tar.gz) 3.JDK 1.7 4.Maven 3.2.1(apache-maven-3.2.1-bin.zip) 5.Protocol Buffers 2.5.0 6.Unix command-line tool Cygwin(Setup-x86.e

深入理解Java:注解(Annotation)--注解处理器

深入理解Java:注解(Annotation)--注解处理器 如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了.使用注解的过程中,很重要的一部分就是创建于使用注解处理器.Java SE5扩展了反射机制的API,以帮助程序员快速的构造自定义注解处理器. 注解处理器类库(java.lang.reflect.AnnotatedElement): Java使用Annotation接口来代表程序元素前面的注解,该接口是所有Annotation类型的父接口.除此之外,Java在java.l

css003 选择器:明确设置哪些样式

css003 选择器:明确设置哪些样式 1.每个样式的两个部分:选择器和声明块 1.标签选择器:整体控制 2.类选择器:精确控制(.+字母.数字.连字符或下划线) Css允许的类名为.+字母.数字.连字符或下划线(最前面的点一定要记得)且点之后的类名必须以字母开头.类名区分大小写. 3.ID选择器:控制特殊的网页元素 4.通用选择器:*{font-size: 18px;} 2.标签 1.祖先标签 2.派生标签:子孙标签 3.父标签:离一个标签最近的祖先标签为父标签(每个标签只有一个父标签) 4.

Python游戏引擎开发(四):TextField文本类

上一章我们主要介绍了显示对象和如何显示图片.本章来讲述显示文本. 以下是本系列教程前几章地址,在阅读本篇正文之前,请务必阅读前几章内容. Python游戏引擎开发(一):序 Python游戏引擎开发(二):创建窗口以及重绘界面 Python游戏引擎开发(三):显示图片 文本类TextField 使用过flash的朋友都知道,这个类其实不光是显示文本用的,还可以用于显示输入框.我这里就只实现一些基础实用的功能,以后有时间了慢慢拓展.和上一章一样,TextField是一个显示对象,继承自Displa

基于网络的 Red Hat 无人值守安装

基于网络的 Red Hat 无人值守安装 本文介绍了 PC 平台上的一种快速 Red Hat Linux 安装方案.它具有很高的自动化程度--用户只需手工启动机器并选择从网络启动,就可以完成整个安装过程.在需要批量安装大量 Red Hat Linux 系统和需要技术人员 Red Hat Linux 安装支持的情况下,这种方案可以节省大量的时间.而且由于不需要额外的软件和光驱和软驱等硬件设备,在成本上也有很大优势.所有安装 Red Hat Linux 的开发/测试人员,和 IT 支持人员都能够从本