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. 实验三--for语句及分支结构else-if

    本节课学习到的知识点: 1.for语句的表达式的应用与掌握.流程形式. 2.多分支else-if,用来判断真假等. 实验中遇到的问题及解决方法: 这次课的逻辑要求比之前的课要难许多,而且对于一些数学逻 ...

  2. 智能手机,医疗诊断,云会议(gotomeeting/citrix)

    在诊断领域已出现很多大有希望的创新,它们可能会起到真正的变革作用. 例如,有一种新技术可以让健康护理工作者用一部智能手机拍摄高质量的视网膜图像.这些数码照片像素很高,足以帮助检测白内障.黄斑退化.糖尿 ...

  3. Linq分组功能

    Linq在集合操作上很方便,很多语法都借鉴自sql,但linq的分组却与sql有一定的区别,故整理发布如下. 1.  Linq分组 分组后以Key属性访问分组键值. 每一组为一个IEnumberAbl ...

  4. yaf性能测试(wamp环境)

    1实现mvc 出现helloword,成功 2.controller重定向 $get = $this->getRequest()->getQuery("get", &q ...

  5. C#中String 和string 的区别

    C#中同时存在String与string MSDN中对string的说明: string is an alias for String in the .NET Framework.string是Str ...

  6. Wordpress更改后台地址

    wordpress默认的后台地址是 xx/wp-admin  或xx/wp-login.php ,谁都知道感觉很不安全, 方法一:使用插件 通过插件在地址上加上只有你知道的参数才能访问 1.后台搜索插 ...

  7. NSNotificationCenter带参

    (1)post Notification类 [[NSNotificationCenter defaultCenter] postNotificationName:CRMPerformanceNewCe ...

  8. (leetcode)Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  9. Magento Service Temporarily Unavailable解决方法

    插件升级错误或安装失败时 会出现Service Temporarily Unavailable错误,使网站前台后台都无法显示. 在操作完成的情况下,仍然出现这个错误时可以采用以下方法: 1.删除网站站 ...

  10. undefined和void

    1.undefined undefined在js中并不是关键字/保留字,因此在IE5.5~8中可以对undefined赋值,但是在IE9以上,对其赋值是无效的 <script> var a ...