Input
The first line of input will contain an integer that will determine the number of test cases. Each case
starts with an integer n (n ≤ 10), that represents the dimension of the grid. The next n lines will
contain n characters each. Every cell of the grid is either a ‘.’ or a letter from [A, Z]. Here a ‘.’
represents an empty cell.
Output
For each case, first output ‘Case #:’ (# replaced by case number) and in the next n lines output the
input matrix with the empty cells filled heeding the rules above.
Sample Input
2
3
...
...
...
3
...
A..
...
Sample Output
Case 1:
ABA
BAB
ABA
Case 2:
BAB
ABA
BAB
遍历就好了。
#include <cstdio> #define FOR(I, F, N) for(I=F;I<=N;I++) //这个不好调试 const int N = 10 + 2; int A[N][N] = {0}; int main() { int T, n, i, j, c; scanf("%d", &T); for (int base = 1; base <= T; ++base) { scanf("%d", &n); getchar(); //坑爹的回车呀 FOR(i, 1, N - 2)FOR(j, 1, N - 2)A[i][j] = 0; //归零 FOR(i, 1, n) { FOR(j, 1, n)scanf("%c", A[i] + j); //实际上可用scanf("%s")存入数组 getchar(); } FOR(i, 1, n) FOR(j, 1, n)if (A[i][j] == '.') FOR(c, 'A', 'Z') if (A[i - 1][j] != c && A[i + 1][j] != c && A[i][j - 1] != c && A[i][j + 1] != c) { A[i][j] = c; break; } printf("Case %d: ", base); FOR(i, 1, n) { FOR(j, 1, n)printf("%c", A[i][j]); printf(" "); } } }