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. 运维技能大全 | Devops Tools 周期表

    老外整理的 Devops Tools 周期表,可以用酷炫屌炸天形容,划分了数据库.CI.日志.安全.监控.配置管理.云服务等15个大类,120个工具.我是有点孤陋寡闻,很多都没听说过,你要是全学会了你 ...

  2. 读书笔记——《图解TCP/IP》(2/4)

    经典摘抄 第三章 数据链路 1.实际的通信媒介之间处理的却是电压的高低.光的闪灭以及电波的强弱等信号. 2.数据链路层的相关技术:MAC寻址.介质共享.非公有网络.分组交换.环路检测.VLAN等 3. ...

  3. Bulk Insert & BCP执行效率对比(续)

    上回由于磁盘空间(约70G)不足,导致Bulk Insert和BCP导入中途失败:今次统一一些操作,以得到Bulk insert与BCP分别执行效率: 1. 15435390笔数据,21.7G csv ...

  4. Cygwin + CMake 测试

    https://www.cygwin.com/ apt-get for cygwin? wget rawgit.com/transcode-open/apt-cyg/master/apt-cyg in ...

  5. MVC分页控件之二,为IQueryable定义一个扩展方法,直接反回PagedList<T>结果集(转)

    namespace Entity { public interface IPagedList { /// <summary> /// 记录数 /// </summary> in ...

  6. [LeetCode]题解(python):80-Remove Duplicates from Sorted Array II

    题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ Follow up for "Remov ...

  7. 非常详细的docker学习笔记

    http://www.open-open.com/lib/view/open1423703640748.html 一.Docker 简介 Docker 两个主要部件: Docker: 开源的容器虚拟化 ...

  8. logback配置详解(二)

    <appender> <appender>: <appender>是<configuration>的子节点,是负责写日志的组件. <appende ...

  9. Windows Server 2008标准证书使用记录

    Windows Server 2008标准证书使用记录   近期准备将单位的服务器全部升级到Windows Server 2008,但有一些“遗留”问题需要解决: (1)现在单位还有一台Windows ...

  10. 还原ORACLE DUMP 的值

    还原DUMP出来的数字SQL> select dump(2000,16) from dual; DUMP(2000,16)------------------Typ=2 Len=2: c2,15 ...