lightoj 1012 - Guilty Prince

http://lightoj.com/volume_showproblem.php?problem=1012

1012 - Guilty Prince

Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a new place. The prince became sad, but he followed his father's will. In the way he found that the place was a combination of land and water. Since he didn't know how to swim, he was only able to move on the land. He didn't know how many places might be his destination. So, he asked your help.

For simplicity, you can consider the place as a rectangular grid consisting of some cells. A cell can be a land or can contain water. Each time the prince can move to a new cell from his current position if they share a side.

Now write a program to find the number of cells (unit land) he could reach including the cell he was living.

Input

Input starts with an integer T (≤ 500), denoting the number of test cases.

Each case starts with a line containing two positive integers W and HW and H are the numbers of cells in the x and y directions, respectively. W and H are not more than 20.

There will be H more lines in the data set, each of which includes W characters. Each character represents the status of a cell as follows.

1) '.' - land

2) '#' - water

3) '@' - initial position of prince (appears exactly once in a dataset)

Output

For each case, print the case number and the number of cells he can reach from the initial position (including it).

Sample Input

Output for Sample Input

4

6 9

....#.

.....#

......

......

......

......

......

#@...#

.#..#.

11 9

.#.........

.#.#######.

.#.#.....#.

.#.#.###.#.

.#.#..@#.#.

.#.#####.#.

.#.......#.

.#########.

...........

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

###.###

...@...

###.###

..#.#..

..#.#..

Case 1: 45

Case 2: 59

Case 3: 6

Case 4: 13

 

题目大意:求和‘@’相连的‘.’最多有多少个(包括‘@’)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

#define N 31
#define met(a, b) memset (a, b, sizeof (a))

typedef long long LL;
//const int INF = ((1<<31)-1);

const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

char str[N][N];
int ans, n, m, vis[N][N];

int DFS (int x, int y)
{
    for (int i=0; i<4; i++)
    {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];

        if (xx>=0 && xx<m && yy>=0 && yy<n && str[xx][yy]=='.' && !vis[xx][yy])
        {
            ans++;
            vis[xx][yy] = 1;
            DFS (xx, yy);
        }
    }
    return ans;
}

int main ()
{
    int t, nCase = 1;

    scanf ("%d", &t);

    while (t--)
    {
        scanf ("%d %d", &n, &m);

        met (str, 0);
        met (vis, 0);

        int x, y;
        ans = 1;

        for (int i=0; i<m; i++)
        {
            getchar ();
            for (int j=0; j<n; j++)
            {
                scanf ("%c", &str[i][j]);
                if (str[i][j] == '@')
                    x = i, y = j;
            }
        }

        printf ("Case %d: %d\n", nCase++, DFS (x, y));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_大太阳_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值