阅读647 返回首页    go 技术社区[云栖]


UVA之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

【分析】

根据字典序的定义,我们可以从上到下,从左到右一位一位的求,先求满足第一个元素最小,再求满足第二个元素最小,

以此类推。本题中,我们只需从左到右,从上到下依次给所有的空格填上最小可能的字母即可。

在填写某个空格时,从‘A’到'Z',要跟这个空格的上面,下面,左边,右边的元素分别比较,找到一个字母。

【代码】

/*********************************
*   日期:2014-04-10
*   作者:SJF0115
*   题号: Fill the Square
*   来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=show_problem&problem=2515
*   结果:AC
*   来源:UVA
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
using namespace std;

#define N 10 + 1
char matrix[N][N];

void FillTheSquare(int n){
    if(matrix == NULL || n <= 0){
        return;
    }
    int i,j;
    for(i = 0;i < n;i++){
        for(j = 0;j < n;j++){
            //.代表需要填写的地方
            if(matrix[i][j] == '.'){
                //试着从A-E开始填写是否成功
                for(char c = 'A';c <= 'Z';c++){
                    bool ok = true;
                    //上面元素
                    if(i > 0 && matrix[i-1][j] == c){
                        ok = false;
                    }
                    //下面元素
                    if(i < n-1 && matrix[i+1][j] == c){
                        ok = false;
                    }
                    //左边元素
                    if(j > 0 && matrix[i][j-1] == c){
                        ok = false;
                    }
                    //右边元素
                    if(j < n-1 && matrix[i][j+1] == c){
                        ok = false;
                    }
                    //该元素与周边元素没有冲突,填写元素,停止尝试
                    if(ok){
                        matrix[i][j] = c;
                        break;
                    }
                }//for
            }//if
        }//for
    }//for
}

int main(){
    int T,i,j,k,n;
    while(scanf("%d",&T) != EOF){
        for(k = 1;k <= T;k++){
            scanf("%d",&n);
            for(i = 0;i < n;i++){
                scanf("%s",matrix[i]);
            }
            //FillTheSquare
            FillTheSquare(n);
            //输出
            printf("Case %d:\n",k);
            for(i = 0;i < n;i++){
                printf("%s\n",matrix[i]);
            }
        }
    }
    return 0;
}





 



最后更新:2017-04-03 12:56:09

  上一篇:go android 添加快捷方式并且图标上添加数字
  下一篇:go ListView异步加载优化