UVA11520 - Fill the Square

Problem A
Fill the Square
Input: Standard Input

Output: Standard Output

 

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


Problemsetter: Sohel Hafiz

题目大意:在给定一个N*N的网格中,一些网格中已经填了大写字母,你的任务是把剩下的网格用大写字母填满,要求每个网格内的大写字母与和它有公共边的网格内的字母不同,且从左到右把所有格子连起来形成的字符串字典序最小。

题解:直接枚举即可。。。

View Code
 1 #include<stdio.h>

 2 #include<string.h>

 3 #define MAXN 15

 4 int main(void)

 5 {

 6     char map[MAXN][MAXN];

 7     int i,j,n,t,p;

 8     char ch;

 9     scanf("%d",&t);

10     p=1;

11     while(t--)

12     {

13         printf("Case %d:\n",p++);

14         scanf("%d",&n);

15         for(i=1; i<=n; i++)

16         {

17             scanf("%s",map[i]);

18             getchar();

19         }

20            for(i=1; i<=n; i++)

21             for(j=0; j<n; j++)

22                 if(map[i][j]=='.')

23                 {

24                     for(ch='A'; ch<='Z'; ch++)

25                     {

26                         if(i-1>=1&&map[i-1][j]==ch) continue;

27                         if(i+1<=n&&map[i+1][j]==ch) continue;

28                         if(j-1>=0&&map[i][j-1]==ch) continue;

29                         if(j+1<n&&map[i][j+1]==ch) continue;

30                         map[i][j]=ch;

31                         break;

32                     }

33                 }

34         for(i=1; i<=n; i++)

35                 printf("%s\n",map[i]);

36 

37     }

38     return 0;

39 }

 

 

你可能感兴趣的:(uva)