首页 > 代码库 > UVa11520 Fill the Square (字典序枚举)

UVa11520 Fill the Square (字典序枚举)

链接:http://vjudge.net/problem/18268

分析:从上到下从左到右按字典序从小到大枚举。

 1 #include <cstdio> 2  3 const int maxn = 10 + 5; 4  5 int n; 6 char grid[maxn][maxn]; 7  8 int main() { 9     int T;10     scanf("%d", &T);11     for (int kase = 1; kase <= T; kase++) {12         scanf("%d", &n);13         for (int i = 0; i < n; i++) scanf("%s", grid[i]);14         for (int i = 0; i < n; i++)15             for (int j = 0; j < n; j++) if (grid[i][j] == .)16                 for (char ch = A; ch <= Z; ch++) {17                     if (i > 0 && grid[i - 1][j] == ch) continue;18                     if (i < n - 1 && grid[i + 1][j] == ch) continue;19                     if (j > 0 && grid[i][j - 1] == ch) continue;20                     if (j < n - 1 && grid[i][j + 1] == ch) continue;21                     grid[i][j] = ch; break;22                 }23         printf("Case %d:\n", kase);24         for (int i = 0; i < n; i++) printf("%s\n", grid[i]);25     }26     return 0;27 }

 

UVa11520 Fill the Square (字典序枚举)