【题解】UVa 11520【Fill the Square】

分析

我们可以从上到下,从左往右,一位一位的填上最小的可能字母就好了。

代码

#include<cstdio>
const int MAXN = 10 + 6;
char grid[MAXN][MAXN];

int main(){
    int id = 0,n,T;scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i = 0;i < n;i++) scanf("%s",grid[i]);
        for(int i = 0;i < n;i++)
            for(int j = 0;j < n;j++) if(grid[i][j] == '.'){
                for(char c = 'A';c <= 'Z';c++){
                    bool ok = true;
                    if(i > 0 && grid[i - 1][j] == c) ok = false;
                    if(i < n - 1 && grid[i + 1][j] == c) ok = false;
                    if(j > 0 && grid[i][j - 1] == c) ok = false;
                    if(j < n - 1 && grid[i][j + 1] == c) ok = false;
                    if(ok){grid[i][j] = c;break;}
                }
            }
        printf("Case %d:\n",++id);
        for(int i = 0;i < n;i++) printf("%s\n",grid[i]);
    }
}