Description:
Given a diagram of Farmer John's field, determine how many ponds he has.
* 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.
10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.....W. .W.W......W. ..W.......W.
3
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;
}