uva 11520 - Fill the Square
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:
- Make sure no adjacent cells contain the same letter; two cells are adjacent if they share a common edge.
- 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的更多相关文章
- Uva 11520 - Fill the Square 贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 11520 Fill the Square 填充正方形
在一个 n * n 网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如果有多重填法,则要求按照从上到下,从左到右的顺序把所有格子连 ...
- UVa 11520 Fill the Square (水题,暴力)
题意:给n*n的格子里填上A-Z的字符,保证相邻字符不同,并且字典序最小. 析:直接从第一个格子开始暴力即可,每次判断上下左是不是相同即可. 代码如下: #pragma comment(linker, ...
- UVA 11520 Fill the Square(模拟)
题目链接:https://vjudge.net/problem/UVA-11520 这道题我们发现$n\leq 10$,所以直接进行暴力枚举. 因为根据字典序所以每个位置试一下即可,这样的复杂度不过也 ...
- UVa 11520 Fill in the Square
题意:给出 n*n的格子,把剩下的格子填上大写字母,使得任意两个相邻的格子的字母不同,且从上到下,从左到右的字典序最小 从A到Z枚举每个格子填哪一个字母,再判断是否合法 #include<ios ...
- 【贪心】【uva11520】 Fill the Square
填充正方形(Fill the Square, UVa 11520) 在一个n×n网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同.如 ...
- UVA 11520 填充正方形
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ ...
- UVA 10603 - Fill BFS~
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...
随机推荐
- Blender to XPS(blender 2.7x Internal materials)
Things we are gonna need are Blender 2.7x www.blender.org/ XPS tools addon for Blender A model made ...
- OC接收数据时毫秒转date时间最简略方法
一般项目中接收后台的数据会收到毫秒格式的date,需要换算成正规日期格式,这时候我们的好朋友command + c 和 command + v就得出来帮忙了: 可以复制使用如下方法: + (NSStr ...
- Asp.net Session保存到Redis: 使用 RedisSessionStateProvider
Install-Package Microsoft.Web.RedisSessionStateProvider 依赖于: Dependencies StackExchange.Redis.Strong ...
- 解决父类加载iframe,src参数过大导致加载失败
原文:解决父类加载iframe,src参数过大导致加载失败 <iframe src="*******.do?param=****" id="leftFrame&qu ...
- Emiller's Advanced Topics In Nginx Module Development
Emiller的Nginx模块开发指南 By Evan Miller DRAFT: August 13, 2009 (changes) 翻译:Kongch @2010年1月5日 0:04am -- 2 ...
- .net framework环境
microsoft.com/downloads/details.aspx?FamilyId=262D25E3-F589-4842-8157-034D1E7CF3A3&displaylang=z ...
- hdu1231最大连续子序列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1231 #include<iostream> #include<cstdio> ...
- Java学习-011-创建文件实例及源代码
此文源码主要为应用 Java 创建文件的源代码.若有不足之处,敬请大神指正,不胜感激! 创建文件的源代码如下所示: /** * @function 文件操作:创建文件.若文件不存在,则直接创建文件:若 ...
- emmc boot1 boot2 partition
使用mfg tool烧写android5.1的镜像之后,再使用旧版的mfg tool烧写linux或者android镜像,都不能正常启动,而且运行的uboot还是android5.1版本的uboot. ...
- T450的Fn lock
新到手一台T450,有一点让我比较恼火,就是F1~F12不能直接按必须先按Fn. 使用一阵突然发现,按住Fn+Esc能锁定/解锁Fn,锁定后F1~F12就可以直接按了. 设计者想得还是比较周到的. 2 ...