Monday, November 16, 2015

UVa 11110 - Equidivisions

// UVa 11110 - Equidivisions
#include <iostream>
#include <string.h>
#include <sstream>
#include <string>
using namespace std;

bool yet[100][100];
int grid[100][100];
int n;

int count(int i, int j, int g) {
	yet[i][j] = false;
	int c = 1;
	if (i > 0 && yet[i - 1][j] && grid[i - 1][j] == g)
		c += count(i - 1, j, g);
	if (j > 0 && yet[i][j - 1] && grid[i][j - 1] == g)
		c += count(i, j - 1, g);
	if (i < n - 1 && yet[i + 1][j] && grid[i + 1][j] == g)
		c += count(i + 1, j, g);
	if (j < n - 1 && yet[i][j + 1] && grid[i][j + 1] == g)
		c += count(i, j + 1, g);
	return c;
}

bool valid() {
	memset(yet, true, sizeof(yet));
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			if (yet[i][j]) {
				if (count(i, j, grid[i][j]) != n)
					return false;
			}
	return true;
}

int main() {
	for (cin >> n; n; cin >> n) {
		string l;
		getline(cin, l);
		memset(grid, 0, sizeof(grid));
		for (int i = 1; i < n; i++) {
			getline(cin, l);
			istringstream ss(l);
			int a, b;
			while (ss >> a >> b)
				grid[a - 1][b - 1] = i;
		}
		if (valid())
			cout << "good\n";
		else
			cout << "wrong\n";
	}
	return 0;
}

No comments:

Post a Comment