UVa 11520 Fill the Square 填充正方形
2013-08-03 17:31
435 查看
在一个 n * n 网格中填了一些大写字母,你的任务是把剩下的格子中也填满大写字母,使得任意两个相邻格子(即有公共边的格子)中的字母不同。如果有多重填法,则要求按照从上到下,从左到右的顺序把所有格子连接起来得到的字符串的字典序应该尽量小。
直接暴力走起就OK。因为,需要填的格子最多就是 A、B、C、D、E 这五个字母。所以直接暴力也就 O(n2)因为要保证字符串的字典序最小,所以就从第一行第一列开始,一行一行的暴就搞定了。其他的就不说了,简单的水题。
附AC代码:
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <cstdarg>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <list>
#include <vector>
#include <map>
#define LL long long
#define M(a) memset(a, 0, sizeof(a))
using namespace std;
void Clean(int count, ...)
{
va_list arg_ptr;
va_start (arg_ptr, count);
for (int i = 0; i < count; i++)
M(va_arg(arg_ptr, int*));
va_end(arg_ptr);
}
char buf[19][19];
char Deal(int a, int b)
{
for (char tmp = 'A'; tmp <= 'Z'; tmp++)
{
if (buf[a - 1][b] == tmp) continue;
else if (buf[a + 1][b] == tmp) continue;
else if (buf[a][b - 1] == tmp) continue;
else if (buf[a][b + 1] == tmp) continue;
else return tmp;
}
}
int main()
{
int T, n, cnt = 1;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
Clean(1, buf);
char *input = &buf[1][1];
for (int i = 1; i <= n; i++)
{
input = &buf[i][1];
scanf("%s", input);
}
for(int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (buf[i][j] == '.')
buf[i][j] = Deal(i, j);
}
}
printf("Case %d:\n", cnt++);
for(int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
printf("%c", buf[i][j]);
puts("");
}
}
return 0;
}
相关文章推荐
- UVa 11520 - Fill the Square (填充正方形)
- UVa 11520 - Fill the Square (填充正方形)
- UVa 11520 Fill the Square 填充正方形
- 例题1.14 填充正方形 Fill the Square UVA - 11520 搜索
- uva - 11520 - Fill the Square(贪心、字典序)
- UVa 11520 - Fill the Square(构造法)
- UVa 11520 Fill the Square (贪心&字典序)
- UVA11520 Fill the Square 枚举
- uva 11520 Fill the Square(DFS)
- UVA - 11520 - Fill the Square(贪心)
- UVa 11520 Fill the Square (水题,暴力)
- uva 11520 - Fill the Square(贪心+枚举)
- UVA 11520 填充正方形
- uva 11520 Fill the Square(枚举)
- uva 11520 - Fill the Square
- UVA 11520 - Fill the Square (贪心)
- UVA - 11520 Fill the Square 水题
- UVA11520 Fill the Square 枚举
- UVA - 11520 Fill the Square 暴力
- 例题1.14 填充正方形 UVa11520