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 ...
随机推荐
- JAVA 调用matlab 出错总结
1.Java:Unsupported major.minor version 51.0 (unable to load class 出现该错误是由于class编译器的JDK版本高于运行期的JDK版本. ...
- 把Java程序打包成jar文件包并执行
1.首先要确认自己写的程序有没有报错. 2.第一次我写的是Web Project到现在,我一直没有执行成功,所以最好创建的是java Project 打包步骤: 1.在项目上,右键,选择Export. ...
- phpcms v9 模板调用代码大全
另:每个栏目会对应当前所选模型的三个模板文件: 这些模板文件所在位置:phpcms/templates/default/content/ 目录下,如果想修改模板文件,只需要到此目录下找到对应的模板文 ...
- CSS的display属性,显示或隐藏元素
<html> <head> <script type="text/javascript"> function removeElement() { ...
- IOS 手势事件的冲突
关于手操作需要强调几点: UIImageView默认是不支持交互的,也就是userInteractionEnabled=NO ,因此要接收触摸事件(手势识别),必须设置userInteractionE ...
- json-lib 之jsonConfig具体应用
一,setCycleDetectionStrategy 防止自包含 public static void testCycleObject() { CycleObject object ...
- 错误提示: An App ID with identifier "*****" is not avaliable. Please enter a different string.
百度了很多,但大多的解决办法就是 更改BundleID,的确管用,,但是有的情况下,你需要跟同事合作,公用同一个BundleID, 我是这样处理的:工具栏中打开Window—project删除所有工程 ...
- 获取网络状态ios(2G、3G、4G、Wifi)
+(NSString *)getNetWorkStates{UIApplication *app = [UIApplication sharedApplication];NSArray *childr ...
- 【转】TableLayout(表格布局)
转自:http://www.cnblogs.com/zhangs1986/archive/2013/01/17/2864536.html TableLayout(表格布局) 表格布局模型以行列的形式管 ...
- 我的工具箱之MySql Front 5.3
下载地址:http://pan.baidu.com/s/1i4sJpNB 这款软件用来连接MySql,作为前端使用. 它功能全面,方便快捷,如果说有缺点的话,sql窗口中不能执行选择的部分有遗憾. 2 ...