Monday, June 8, 2015

UVa 469 - Wetlands of Florida

// UVa 469 - Wetlands of Florida

#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;

int move[8][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, { 1, 1 } };

int cmp, n, m;
vector<string> grid;
int comp[100][100];
int count[10001];

void dfs(int a, int b) {
	grid[a][b] = 'L';
	comp[a][b] = cmp;
	count[cmp]++;
	for (int k = 0; k < 8; k++) {
		int x = a + move[k][0];
		int y = b + move[k][1];
		if (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == 'W')
			dfs(x, y);
	}
}

int main() {
	int t;
	cin >> t;
	string l;
	getline(cin, l);
	getline(cin, l);
	for (; t; t--) {
		getline(cin, l);
		grid.clear();
		while (l[0] == 'L' || l[0] == 'W') {
			grid.push_back(l);
			getline(cin, l);
		}
		n = grid.size();
		m = grid[0].length();
		cmp = 0;
		memset(comp, 0, sizeof(comp));
		while (cin && l.length() > 0) {
			istringstream strm(l);
			int a, b;
			strm >> a >> b;
			if (a < 1 || a > n || b < 1 || b > m || comp[a - 1][b - 1] == 0 && grid[a - 1][b - 1] == 'L')
				cout << 0 << endl;
			else {
				if (comp[a - 1][b - 1] == 0) {
					cmp++;
					count[cmp] = 0;
					dfs(a - 1, b - 1);
					cout << count[comp[a - 1][b - 1]] << endl;
				} else
					cout << count[comp[a - 1][b - 1]] << endl;
			}
			getline(cin, l);
		}
		if (t > 1)
			cout << endl;
	}
	return 0;
}

No comments:

Post a Comment