HDU 搜索练习 Red and Black

Red and Black

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 13

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.<br><br>Write a program to count the number of black tiles which he can reach by repeating the moves described above. <br>

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.<br><br>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.<br><br>‘.‘ - a black tile <br>‘#‘ - a red tile
<br>‘@‘ - a man on a black tile(appears exactly once in a data set)
<br>

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). <br>

Sample Input

6 9

....#.

.....#

......

......

......

......

......

#@...#

.#..#.

11 9

#.........

.#.#######.

.#.#.....#.

.#.#.###.#.

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

.#.#####.#.

.#.......#.

.#########.

...........

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

###.###

[email protected]

###.###

..#.#..

..#.#..

0 0

Sample Output

45

59

6

13

简单题意:

  #为障碍,"."为陆地,@表示人,求这个人最大能走到少范围

思路分析:

  简单搜索问题,,,最值得注意的是,先输入的是列, 而不是行

# include <iostream>
# include <queue>
# include <cstring>
# include <fstream>
using namespace std; // 好坑呀, 先输入列 在输入行

struct Info
{
    int x;
    int y;
}start;
int n, m;
char map[101][101];
int is[101][101];
int dir[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int dfs();
bool border(Info & info)
{
    if(info.x >= 1 && info.x <= m && info.y >= 1 && info.y <= n)
    return true;
    return false;
}
int main()
{
    //fstream cin("aaa.txt");
    while(cin >> n >> m)
    {
        if(n == 0 && m == 0)
        break;
        memset(is, 0, sizeof(is));
        for(int i = 1 ; i <= m; i++)
            for(int j = 1; j <= n; j++)
            {
                cin >> map[i][j];
                if(map[i][j] == ‘@‘)
                {
                    start.x = i;
                    start.y = j;

                }
            }
        is[start.x][start.y] = 1;
        map[start.x][start.y] = ‘.‘;
        cout << dfs() << endl;
    }
    return 0;
}
int dfs()
{
    int jishu = 1;
    queue <Info> Q;
    Q.push(start);
    Info now, next;
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();

        for(int i = 0; i < 4; i++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];
            if(!border(next))
                continue;
            if(map[next.x][next.y] == ‘#‘)
            continue;
            if(is[next.x][next.y])
            continue;

            jishu++;
            is[next.x][next.y] = 1;
            Q.push(next);
        }
    }
    return jishu;
}

  

时间: 2024-08-02 06:54:31

HDU 搜索练习 Red and Black的相关文章

HDU 搜索练习 Oil Deposits

Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 61 Accepted Submission(s) : 25 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oil

HDU 搜索练习 连连看

连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27989 Accepted Submission(s): 6952 Problem Description “连连看”相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而

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

HDU 搜索练习 The magic apple tree

The magic apple tree Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submission(s) : 26 Accepted Submission(s) : 2 Problem Description Sailormoon girls all like eating many kinds of fruit, such as banana, grape, a

HDU 搜索练习 非常可乐

非常可乐 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 51 Accepted Submission(s) : 21 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一

HDU 搜索练习 Rescue

Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 44 Accepted Submission(s) : 13 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described a

HDU 搜索练习 Catch him

Catch him Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 797 Accepted Submission(s): 361 Problem Description 在美式足球中,四分卫负责指挥整只球队的进攻战术和跑位,以及给接球员传球的任务.四分卫是一只球队进攻组最重要的球员,而且一般身体都相对比较弱小,所以通常球队会安排5-7名大汉来

HDU 搜索练习 Prime Ring Problem

Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 17 Accepted Submission(s) : 10 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..

HDU 搜索练习 N皇后问题

N皇后问题 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 39 Accepted Submission(s) : 18 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.<br>你的任务是,对于给定的N,求出有多少种合