HDOJ 1312题Red and Black

Red and Black
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13508 Accepted Submission(s): 8375

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

题意:
n*m的方阵有红格或是黑格,只能走黑格
每次只能走上下左右四个紧邻方向的格子,求
这个人最后能走多少个黑格子。

分析:
dfs水题。从第一个黑格子开始递归的搜索,
每次搜索一个黑格子后为了以后不再重复走
这个黑格子,就把当前搜索的这个黑格子换
成红格子,然后继续dfs。。。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312
题目大意:一个长方形空间,上面铺红色和黑色瓦片,一个人起初站在黑色瓦片上,每次可以走到相邻的4个黑色瓦片上,输入数据,求其能走过多少瓦片
题意:某人在@处为起点(也包括@点)#为墙,点(.)为通路,问最多能走多远统计能走几个点(加上@这个点)
思路:用dfs;
代码:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
char a[30][30];
int ss,n,m;//这3个值需要用全局变量
int b[4][2]= {{0,-1},{0,1},{1,0},{-1,0}};
int dfs(int x,int y)
{
    int xx,yy;
    if(x<0||y<0||x>=m||y>=n)
        return 0;
    int i;
    for(i=0; i<4; i++)
    {
        xx=x+b[i][0];
        yy=y+b[i][1];
        if(xx<0||yy<0||xx>=m||yy>=n||a[xx][yy]=='#')
        //检查该点上下左右的点是否符合题目要求。
            continue;
        ss++;
        a[xx][yy]='#';
        //如果该点已经检查过,就把它变成'#',防止再次被检查。
        dfs(xx,yy);
    }
}
int main()
{

    while(~scanf("%d%d",&n,&m)&&(n||m))//n,m不能同时为0
    {
        int i,j;
        int pi,pj;
        getchar();//吸收换行符。
        for(i=0; i<m; i++)
        {
            for(j=0; j<n; j++)
            {
                scanf("%c",&a[i][j]);
                if(a[i][j]=='@')
                {
                    pi=i;
                    pj=j;
                }
            }
            getchar();//吸收换行符。
        }
        a[pi][pj]='#';
        ss=1;
        dfs(pi,pj);
        printf("%d\n",ss);
    }
    return 0;
}
时间: 2024-10-23 16:31:23

HDOJ 1312题Red and Black的相关文章

HDOJ 1312 (POJ 1979) 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 mo

HDOJ 1004题 Let the Balloon Rise strcmp()函数

Problem Description Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color

HDOJ 1013题Digital Roots 大数,9余数定理

Problem Description The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits a

HDOJ 1237题 简单计算器

简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15220 Accepted Submission(s): 5195 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符

在Red Hat Linux 9下安装VMWare tools

环境 Red Hat Linux 9 + Windows 7 Ultimate +VMWare 7.1 问 题 Red Hat Linux 9安装VMWare tools 解决 1.VM-------- -->Install Vmwaretools---------->等待虚拟机挂载安装文件: 2.进入/mnt/cdrom---------->将VMwareTools-8.4.4-301548.tar.gz 拷贝到桌面: 3.选中VMwareTools-8.4.4-301548.tar.

hdu 1312 Red And Black

hdu 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 ti

HDOJ/HDU 2537 8球胜负(水题.简单的判断)

Problem Description 8球是一种台球竞赛的规则.台面上有7个红球.7个黄球以及一个黑球,当然还有一个白球.对于本题,我们使用如下的简化规则:红.黄两名选手轮流用白球击打各自颜色的球,如果将该颜色的7个球全部打进,则这名选手可以打黑球,如果打进则算他胜.如果在打进自己颜色的所有球之前就把黑球打进,则算输.如果选手不慎打进了对手的球,入球依然有效. 现在给出打进的球(白球除外)的顺序,以及黑球由哪方打进,你的任务是判定哪方是胜者. 假设不会有一杆同时打进一颗黑球和其他彩球. Inp

算法题:uva 1312

题目大意: 在w*h的坐标上给n个点, 然后求一个最大的矩形,使得这个矩形内(不包括边界 )没有点,注意边界上是可以有点的. 首先要把这些坐标进行离散化,离散话时要注意一点,如 果有两个点原来坐标不是相邻的,但是离散化以后却变成相邻了,这时候要在它们之间加上一个点. 预处理后,枚举矩形上下界,然后再用O(n)的复杂度找出左右边界. 代码: #include<iostream> #include<cstdio> #include<algorithm> #include&l

HDOJ/HDU 1256 画8(绞下思维~水题)

Problem Description 谁画8画的好,画的快,今后就发的快,学业发达,事业发达,祝大家发,发,发. Input 输入的第一行为一个整数N,表示后面有N组数据. 每组数据中有一个字符和一个整数,字符表示画笔,整数(>=5)表示高度. Output 画横线总是一个字符粗,竖线随着总高度每增长6而增加1个字符宽.当总高度从5增加到6时,其竖线宽度从1增长到2.下圈高度不小于上圈高度,但应尽量接近上圈高度,且下圈的内径呈正方形. 每画一个"8"应空一行,但最前和最后都无空