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

1012 - Guilty Prince
PDF (English) Statistics Forum
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);
    }
} 


你可能感兴趣的:(poj,DFS,lightoj)