因为要字典序最小,直接从第一个开始求答案即可
AC代码如下:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; char s[15][15]; int n; bool judge( int x, int y, int k ){ return k != s[x][y+1] && k != s[x][y-1] && k != s[x+1][y] && k != s[x-1][y]; } int main(){ int T, Case = 1; cin >> T; while( T-- ){ cin >> n; for( int i = 0; i <= n + 1; i++ ){ for( int j = 0; j <= n + 1; j++ ){ s[i][j] = '.'; } } for( int i = 1; i <= n; i++ ){ scanf( "%s", &s[i][1] ); } for( int i = 1; i <= n; i++ ){ for( int j = 1; j <= n; j++ ){ if( s[i][j] != '.' ) continue; for( int k = 'A'; k <= 'Z'; k++ ){ if( judge( i, j, k ) ){ s[i][j] = k; break; } } } } printf( "Case %d:\n", Case++ ); for( int i = 1; i <= n; i++ ){ for( int j = 1; j <= n; j++ ){ printf( "%c", s[i][j] ); } cout << endl; } } return 0; }