LightOJ1012---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 H; W 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

/************************************************************************* > File Name: LightOJ1012.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年06月07日 星期日 17时48分45秒 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

char mat[55][55];
int num[55][55];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

namespace DSU {
    int father[3000];
    int num[3000];

    int find(int x) {
        if (father[x] == -1) {
            return x;
        }
        return father[x] = find(father[x]);
    }

    void init(int n) {
        for (int i = 0; i < n; ++i) {
            father[i] = -1;
            num[i] = 1;
        }
    }
}

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        int cnt = 0;
        int w, h;
        int sx, sy;
        scanf("%d%d", &h, &w);
        for (int i = 0; i < w; ++i) {
            scanf("%s", mat[i]);
            for (int j = 0; j < h; ++j) {
                if (mat[i][j] != '#') {
                    num[i][j] = cnt++;
                }
                if (mat[i][j] == '@') {
                    sx = i;
                    sy = j;
                }
            }
        }
        DSU :: init(cnt);
        for (int i = 0; i < w; ++i) {
            for (int j = 0; j < h; ++j) {
                if (mat[i][j] == '#') {
                    continue;
                }
                for (int k = 0; k < 4; ++k) {
                    int x = i + dir[k][0];
                    int y = j + dir[k][1];
                    if (x < 0 || x >= w || y < 0 || y >= h) {
                        continue;
                    }
                    if (mat[x][y] == '#') {
                        continue;
                    }
                    int fu = DSU :: find(num[i][j]);
                    int fv = DSU :: find(num[x][y]);
                    if (fu != fv) {
                        DSU :: father[fu] = fv;
                        DSU :: num[fv] += DSU :: num[fu];
                    }
                }
            }
        }
        int fu = DSU :: find(num[sx][sy]);
        printf("Case %d: %d\n", icase++, DSU :: num[fu]);
    }
    return 0;
}

你可能感兴趣的:(并查集)