lightoj 1012 Guilty Prince / POJ 1979 Red and Black (DFS)

66 篇文章 0 订阅
1012 - Guilty Prince
Time Limit: 2 second(s)Memory Limit: 32 MB

Once there was a king named Akbar. He had a son namedShahjahan. 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. Theprince became sad, but he followed his father's will. In the way he found thatthe place was a combination of land and water. Since he didn't know how toswim, he was only able to move on the land. He didn't know how many placesmight be his destination. So, he asked your help.

For simplicity, you can consider the place as a rectangulargrid consisting of some cells. A cell can be a land or can contain water. Eachtime the prince can move to a new cell from his current position if they sharea 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 positiveintegersW andH;W and H are the numbers of cellsin thex andy directions, respectively.W andHare not more than 20.

There will be H more lines in the data set, each ofwhich includesW characters. Each character represents the status of acell as follows.

1)'.' - land

2)'#' - water

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

Output

For each case, print the case number and the number of cellshe 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

 

题目链接 :http://lightoj.com/volume_showproblem.php?problem=1012

                   http://poj.org/problem?id=1979


题目大意 : 给个起点,求能够到达的点的数目


题目分析 :裸DFS,四个方向走一下


#include <cstdio>
#include <cstring>
int const MAX = 22;
char map[MAX][MAX];
bool vis[MAX][MAX];
int n, m;
int sx, sy;
int step;
void DFS(int x, int y)
{
    vis[x][y] = true;
    if(x > 0 && !vis[x-1][y] && map[x - 1][y] == '.')
    {
        step++;
        DFS(x - 1, y);
    }
    if(y > 0 && !vis[x][y-1] && map[x][y - 1] == '.')
    {
        step++;
        DFS(x, y - 1);
    }
    if(x + 1< n && !vis[x + 1][y] && map[x + 1][y] == '.')
    {
        step++;
        DFS(x + 1, y);
    }
    if(y + 1< m && !vis[x][y + 1] && map[x][y + 1] == '.')
    {
        step++;
        DFS(x, y + 1);
    }
}

int main()
{
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca++)
    {
        step = 1;
        memset(vis, false, sizeof(vis));
        memset(map, 0, sizeof(map));
        scanf("%d %d", &m, &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%s", map[i]);
            for(int j = 0; j < m; j++)
            {
                if(map[i][j] == '@')
                {
                    sx = i;
                    sy = j;
                }
            }
        }
        DFS(sx, sy);
        printf("Case %d: %d\n", ca, step);
    }
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值