ACM_填格子

填格子

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

在一个n*n格子里边已经填了部分大写字母,现在给你个任务:把剩下的格子也填满大写字母,要求任意两个相邻的格子中的字母不一样。

Input:

输入有多组数据。第一行输入T,表示有T组数据。以下每组第一行输入为整数 n (n<=10).下面接着输入有大写字母‘A~Z’或者'.'组成的 n * n 的方格。

Output:

对于每一组数据,输出填满字母后的网格。若答案有多组,输出按从左到右从上到下字典序最小的一组(例如:‘abc...’字典序小于'abd...')。

Sample Input:

2
3
...
...
...
4
..B.
B...
...B
.B..

Sample Output:

Case 1:
ABA
BAB
ABA
Case 2:
ACBA
BACD
ACAB
CBCA
解题思路:简单暴力枚举,只要当前点是'.',那么就查看周围上下左右四个点是否有已被填写的字母(从'A'枚举到'Z',这样就能保证空格填写的字符是最小的字母),水过!
AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 char s[15][15];int t,n;
 4 int main(){
 5     while(cin>>t){
 6         for(int g=1;g<=t;++g){
 7             cin>>n;getchar();
 8             for(int i=0;i<n;++i)
 9                 for(int j=0;j<n;++j)
10                     cin>>s[i][j];
11             for(int i=0;i<n;++i){
12                 for(int j=0;j<n;++j){
13                     for(char k='A';k<='Z';++k){
14                         if(s[i][j]=='.'){
15                             if(i-1>=0&&s[i-1][j]==k)continue;//
16                             if(i+1<n&&s[i+1][j]==k)continue;//
17                             if(j-1>=0&&s[i][j-1]==k)continue;//
18                             if(j+1<n&&s[i][j+1]==k)continue;//
19                             s[i][j]=k;
20                         }
21                     }
22                 }
23             }
24             cout<<"Case "<<g<<":"<<endl;
25             for(int i=0;i<n;++i){
26                 for(int j=0;j<n;++j)
27                     cout<<s[i][j];
28                 cout<<endl;
29             }
30         }
31     }
32     return 0;
33 }

猜你喜欢

转载自www.cnblogs.com/acgoto/p/9265708.html