题意:
一个n*n的表格中填了一些大写字母,需要将剩下的空填满,相邻的两个格子中的字母不能相同,如果有多种填法,则按从上到下,从左到右的顺序把所有格子连起来得到的字符串字典序最小。
思路:
按从上到下,从左到右的顺序遍历格子,如果格子空,检测周围的格子并记录,在该格子中填入除周围格子出现过的字母外字典序最小的字母。
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#define endl "\n"
using namespace std;
typedef long long ll;
const int N = 11;
const int INF = 0x3f3f3f3f;
int n;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
string s[N];
int main()
{
int t;
cin>>t;
for(int cnt=1; cnt<=t; cnt++) {
cin>>n;
for(int i=0; i<n; i++)
cin>>s[i];
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) if(s[i][j] == '.') {
char c[26] = {0};
for(int k=0; k<4; k++) {
int x = i + dx[k], y = j + dy[k];
if(x >= 0 && x < n && y >= 0 && y < n && s[x][y] != '.')
c[s[x][y]-'A']++;
}
for(int k=0; k<26; k++) if(!c[k]) {s[i][j] = 'A' + k; break;}
}
}
cout<<"Case "<<cnt<<":"<<endl;
for(int i=0; i<n; i++) cout<<s[i]<<endl;
}
}