POJ 2386 Lake Counting

来源:互联网 发布:怎么把淘宝小号养到2心 编辑:程序博客网 时间:2024/06/06 18:40

Description:

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
Output
* Line 1: The number of ponds in Farmer John's field.
Sample Input
10 12W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.W.W.....W..W.W......W...W.......W.
Sample Output
3
Hint
OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

题目大意:

求图中联通块个数, ’W‘为有效可以拓展到8个方向。

解题思路:

裸的DFS练习, 别忘记判断越界。

代码:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>

using namespace std;

/*tools:
 *ios::sync_with_stdio(false);
 *freopen("input.txt", "r", stdin);
 */

typedef long long ll;
typedef unsigned long long ull;
const int dir[9][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 2e5 + 7;

char str[Max], ans[Max];
int num[Max], vis[Max];

int main() {
    //freopen("input.txt", "r", stdin);

    scanf("%d", str);
    int len = strlen(str);
    for (int i = 0; i < len; ++i) {
        num[str[i]]++;
    }

    for (int i = 0; i < Max; ++i) {
        if (num[i] > n / 2) {
            printf("impossible\n");
            break;
        }
    }
    for (int i = 0; i < n; ++i) {
        else {
            for (int j = 0; j < n; ++j) {
                if (str[i] != str[j] && !vis[j]) {
                    ans[j] = str[i];
                    vis[j] = 1;
                }
            }
        }
    }
    return 0;
}



原创粉丝点击