http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2515

11520 - Fill the Square

Time limit: 1.000 seconds

In this problem, you have to draw a square using uppercase English Alphabets. To be more precise, you will be given a square grid with some empty blocks and others already filled for you with some letters to make your task easier. You have to insert characters in every empty cell so that the whole grid is filled with alphabets. In doing so you have to meet the following rules:

  1. Make sure no adjacent cells contain the same letter; two cells are adjacent if they share a common edge.
  2. There could be many ways to fill the grid. You have to ensure you make the lexicographically smallest one. Here, two grids are checked in row major order when comparing lexicographically.

Input

The first line of input will contain an integer that will determine the number of test cases. Each case starts with an integer n (n ≤ 10), that represents the dimension of the grid. The next n lines will contain n characters each. Every cell of the grid is either a ‘.’ or a letter from [A, Z]. Here a ‘.’ represents an empty cell.

Output

For each case, first output ‘Case #:’ (# replaced by case number) and in the next n lines output the input matrix with the empty cells filled heeding the rules above.

Sample Input

2

3

...

...

...

3

...

A..

...

Sample Output

Case 1:

ABA

BAB

ABA

Case 2:

BAB

ABA

BAB

AC代码:

 // UVa11520 Fill the Square

 #include<cstdio>

 #include<cstring>

 const int maxn =  + ;

 char grid[maxn][maxn];

 int n;

 int main() {

   int T;

   scanf("%d", &T);

   for(int kase = ; kase <= T; kase++) {

     scanf("%d", &n);

     for(int i = ; i < n; i++) scanf("%s", grid[i]);

     for(int i = ; i < n; i++)

       for(int j = ; j < n; j++) if(grid[i][j] == '.') {//没填过的字母才需要填

         for(char ch = 'A'; ch <= 'Z'; ch++) {  //按照字典序依次尝试

           bool ok = true;

           if(i> && grid[i-][j] == ch) ok = false;  //和上面的字母冲突

           if(i<n- && grid[i+][j] == ch) ok = false;

           if(j> && grid[i][j-] == ch) ok = false;

           if(j<n- && grid[i][j+] == ch) ok = false;

           if(ok) { grid[i][j] = ch; break; } //没有冲突,填进网格,停止继续尝试

         }

       }

     printf("Case %d:\n", kase);

     for(int i = ; i < n; i++) printf("%s\n", grid[i]);

   }

   return ;

 }

uva 11520 - Fill the Square的更多相关文章

  1. Uva 11520 - Fill the Square 贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  2. UVa 11520 Fill the Square 填充正方形

    在一个 n * n 网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如果有多重填法,则要求按照从上到下,从左到右的顺序把所有格子连 ...

  3. UVa 11520 Fill the Square (水题,暴力)

    题意:给n*n的格子里填上A-Z的字符,保证相邻字符不同,并且字典序最小. 析:直接从第一个格子开始暴力即可,每次判断上下左是不是相同即可. 代码如下: #pragma comment(linker, ...

  4. UVA 11520 Fill the Square(模拟)

    题目链接:https://vjudge.net/problem/UVA-11520 这道题我们发现$n\leq 10$,所以直接进行暴力枚举. 因为根据字典序所以每个位置试一下即可,这样的复杂度不过也 ...

  5. UVa 11520 Fill in the Square

    题意:给出 n*n的格子,把剩下的格子填上大写字母,使得任意两个相邻的格子的字母不同,且从上到下,从左到右的字典序最小 从A到Z枚举每个格子填哪一个字母,再判断是否合法 #include<ios ...

  6. 【贪心】【uva11520】 Fill the Square

    填充正方形(Fill the Square, UVa 11520) 在一个n×n网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如 ...

  7. UVA 11520 填充正方形

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ ...

  9. UVA 10603 - Fill BFS~

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...

随机推荐

  1. OAuth的机制原理讲解及开发流程

    本想前段时间就把自己通过QQ OAuth1.0.OAuth2.0协议进行验证而实现QQ登录的心得及Demo实例分享给大家,可一直很忙,今天抽点时间说下OAuth1.0协议原理,及讲解下QQ对于Oaut ...

  2. Qt之窗体拖拽、自适应分辨率、自适应大小 good

    Qt之自定义界面(实现无边框.可移动) Qt之自定义界面(窗体缩放-跨平台终极版) Qt之自定义界面(窗体缩放) http://blog.csdn.net/liang19890820/article/ ...

  3. qmake理解(还可以加入Lex Yacc文件)

    关于qmake,好一段时间令我一头雾水,不知道用来干嘛的,只知道怎么用,而且也只懂那么一两个命令,详细看过资料以后整理如下: 1.首先,感性的认识是,qmake可以利用源文件(包括头文件h,实现文件c ...

  4. membership db注册工具

    C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe

  5. C++简单单例模式

    #ifndef _SINGLETON_H_ #define _SINGLETON_H_ #include <stdio.h> template<typename T> clas ...

  6. jQuery 复选框全选反选

    <script type="text/javascript"> $(function(){ //全选 $("#CheckedAll").click( ...

  7. 创建ArcGIS API for JavaScript的第一个示例程序

    原文:创建ArcGIS API for JavaScript的第一个示例程序 在上一篇博客中已经介绍了如何搭建ArcGIS API for JavaScript开发环境,如果您还没有搭建好开发环境的话 ...

  8. ubuntu12.04进入单用户模式

    更改ubuntu配置文件出错了,只能进入单用户模式改回来. 转载至: http://blog.csdn.net/lfyaa/article/details/9899497 1.重启ubuntu,VMw ...

  9. ie下的onscroll和onresize的优化

    ie下的scroll和resize的优化 1.onscroll function scrollEvent(){ //do something... console.log('do something. ...

  10. LeetCode Longest Increasing Path in a Matrix

    原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, ...