Friday, April 24, 2015

UVa 11953 - Battleships

//UVa 11953 - Battleships

#include <stdio.h>
using namespace std;

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

char mat[100][101];
int n;

void mark(int i, int j) {
	mat[i][j] = '-';
	for (int k = 0; k < 4; k++) {
		int a = i + move[k][0];
		int b = j + move[k][1];
		if (a >= 0 && a < n && b >= 0 && b < n && (mat[a][b] == 'x' || mat[a][b] == '@'))
			mark(a, b);
	}
}

int main() {
	int cases;
	scanf("%d\n", &cases);
	for (int cas = 1; cas <= cases; cas++) {
		scanf("%d\n", &n);
		for (int i = 0; i < n; i++)
			scanf("%s\n", mat[i]);

		int components = 0;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				if (mat[i][j] == 'x') {
					mark(i, j);
					components++;
				}
		printf("Case %d: %d\n", cas, components);
	}
	return 0;
}

No comments:

Post a Comment