UVA之Fill the Square

22 篇文章 7 订阅

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
*   来源:http://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;
}





 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值