uva 11520 Fill the Square(枚举)

uva 11520 Fill the Square

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                       Output for Sample Input  

2

3

...

...

...

3

...

A..

...


Case 1:

ABA

BAB

ABA

Case 2:

BAB

ABA

BAB

题目大意:给出一张图,‘.‘的地方要用大写字母去填,要求说每个位置的上下左右如果存在的话所填的字母不能和当前位置相同。输出从上到下,从左到右字典序最小的方案。

解题思路:暴力枚举。

#include<stdio.h>
#include<string.h>
char gra[15][15];
int main() {
	int T, Case = 1;
	scanf("%d%*c", &T);
	while (T--) {
		memset(gra, 0, sizeof(gra));
		int n;
		scanf("%d", &n);
		getchar();
		char temp;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				scanf("%c", &gra[i][j]);
			}
			getchar();
		}
		for (int i = 1; i <= n; i++) {
			for (int  j = 1; j <= n; j++) {
				if (gra[i][j] == '.') {
					for (int k = 0; k < 26; k++) { //字典序
						int ch = k + 'A';
						if (ch == gra[i - 1][j]) continue;//冲突检测
						if (ch == gra[i + 1][j]) continue;
						if (ch == gra[i][j - 1]) continue;
						if (ch == gra[i][j + 1]) continue;
						gra[i][j] = ch;
						break;
					}
				}
				else continue;
			}
		}
		printf("Case %d:\n", Case++);
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				printf("%c", gra[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
}
时间: 2024-10-29 19:08:45

uva 11520 Fill the Square(枚举)的相关文章

[2016-03-19][UVA][11520][Fill the Square]

时间:2016-03-19 14:52:10 星期六 题目编号:[2016-03-19][UVA][11520][Fill the Square] 题目大意:给定n<=10 的不完全矩阵,求填充完整矩阵,使得相邻的字母不同,并且字典序最小,输出最终的矩阵 方法:从左到右,从上到下,每个空格枚举'A'-'Z'的所有情况,满足就跳出 #include <cstdio> using namespace std; #define FOR(x,y,z) for(int (x)=(y);(x)<

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

题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2515 题意 n*n矩阵中填入大写字母,n <= 10,要求从上到下从左到右字母序最小,相邻格子字母不同 思路 如刘书思路,状态比较小,不会导致矛盾. 感想 1. 状态较小 代码 #include <algorithm> #include <cassert>

UVa 11520 - Fill the Square

题目:给你一个n*n的格子,有些里面有大写字母,用大写字母填满格子,相邻的格子中字母不相同, 并且使得从上到下,从左到右的字母字典序最小. 分析:构造.将格子从上到下,从左到右编号,然后按编号填充,避免冲突即可,这样一定最小. (如果,该方案不是最小,那么之前一定会选择更小的方案,而不是本方案) 说明:╮(╯▽╰)╭. #include <cstdlib> #include <cstring> #include <cstdio> char maps[12][12]; i

UVA - 11520 - Fill the Square(贪心)

题意:给定一个n * n(1 <= n <= 10)的网格,有些已经填上了一些大写字母,需要补充完所有的字母,使每两两相邻的格子中的字母不同,且从上至下,从左至右,组成一个字符串后字典序最小. 由于组成字符串后的长度都为n * n,故字典序越往前的字母决定的优先级越大,所以贪心即可,从上到下,从左到右的补充格子里的字母,尽可能的使当前可以补充的字母尽量小. #include<cstdio> #include<cstring> #include<cctype>

UVa 10603 Fill [暴力枚举、路径搜索]

10603 Fill There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The rst and the second jug are initially empty, while the third is completely lled with water. It is allowed to pour water f

UVA 10603 Fill(正确代码虽然很搓,网上许多代码都不能AC)

在做用户查找时 因为要把查找的结果动态加载和显示,所以,那些html元素要由Ajax动态生成.用户打开查找界面时,有系统推荐的用户,而当用户按条件查找后,查找的结果动态加载和显示.所以考虑到用js来搞. 这个for循环就是移除已有的表单.然后根据Ajax请求过来的数据,动态生成新的表单对象.一定要注意j变量从大往小循环,否则,删除div元素后会引起serchResultLenth=serchResult.children.length;长度的变化(这个问题摸索了好久,才搞定,切记) for(va

uva 1560 - Extended Lights Out(枚举 | 高斯消元)

题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5?6的矩阵,每个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,如果按了这个位置的开关,将会导致周围包括自己位置的灯状态变换,求一个按开关位置,保证所有灯都灭掉. 解题思路: 枚举,枚举第一行的状态,然后递推出后面四行的状态. 高斯消元,对于每个位置对定变量,这样列出30个方程求解. C++ 枚举 #include <cstdio> #include <cstring> #include &

uva 565 - Pizza Anyone?(暴力枚举 + 二进制)

题目:uva 565 - Pizza Anyone?(暴力枚举 + 二进制) 题目大意:题目是说有一个人要帮他的朋友们定批萨,然后每个朋友都有自己的口味要求,问能不能定一个批萨然后满足每个朋友的至少一个要求. 能就输出所定批萨里面加的东西,,输出要求按字典序: 不能就输出:No pizza can satisfy these requests. 解题思路:这题里面有16种材料,每种材料只有取与不取的可能,这样就有 2^16 种( 0 - 2^16 - 1),枚举出每种情况然后在分别看是否能满足每

uva 10603 Fill (BFS)

uva 10603 Fill There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The first and the second jug are initially empty, while the third is completely filled with water. It is allowed to pour