UVa 11520 - Fill the Square

UVA - 11520


思路:每个单元与其周围单元比较确定最小字母


#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int MAXN = 10 + 5;
char sq[MAXN][MAXN];

char fill(int row, int col) {
	for (int c = 'A'; c <= 'Z'; c++) {
		if (sq[row - 1][col] != c && sq[row + 1][col] != c && sq[row][col - 1] != c && sq[row][col + 1] != c)
			return c;
	}
}

int main(void) {
	int cas = 1;
	int c, n;
	cin >> c;
	while (c--) {
		memset(sq, 0, sizeof(sq));
		cin >> n;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++)
				cin >> sq[i][j];
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (sq[i][j] == '.')
					sq[i][j] = fill(i, j);
			}
		}
		printf("Case %d:\n", cas++);
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				cout << sq[i][j];
			}
			printf("\n");
		}
	}
	return 0;
}


你可能感兴趣的:(字符串)