您的位置:首页 > 其它

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;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: