UESTC 1222 Sudoku

Sudoku

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller.

Actually, Yi Sima was playing it different. First of all, he tried to generate a  4×4 4×4 board with every row contains  1 1 to  4 4, every column contains  1 1 to  4 4. Also he made sure that if we cut the board into four  2×2 2×2 pieces, every piece contains  1 1 to  4 4.

Then, he removed several numbers from the board and gave it to another guy to recover it. As other counselors are not as smart as Yi Sima, Yi Sima always made sure that the board only has one way to recover.

Actually, you are seeing this because you've passed through to the Three-Kingdom Age. You can recover the board to make Yi Sima happy and be promoted. Go and do it!!!

Input

The first line of the input gives the number of test cases,  T T( 1T100 1≤T≤100).  T T test cases follow. Each test case starts with an empty line followed by  4 4 lines. Each line consist of  4 4characters. Each character represents the number in the corresponding cell (one of 1234). * represents that number was removed by Yi Sima.

It's guaranteed that there will be exactly one way to recover the board.

Output

For each test case, output one line containing Case #x:, where  x x is the test case number (starting from  1 1). Then output  4 4 lines with  4 4 characters each. indicate the recovered board.

Sample input and output

Sample Input Sample Output
3

****
2341
4123
3214

*243
*312
*421
*134

*41*
**3*
2*41
4*2*
Case #1:
1432
2341
4123
3214
Case #2:
1243
4312
3421
2134
Case #3:
3412
1234
2341
4123

Source

The 2015 China Collegiate Programming Contest


思路:x[i][j]标记第i行是否已有j存在,y [i][j]标记第i列是否已有j存在,行列都是从0开始
k[i][j]则标记第i分块是否已有j存在(分块有左上、右上、左下、右下,分别对应0,1,2,3)
因为题目已经说明答案唯一,那么从一开始就必定有某些位置是确定的,只需要统计该位置可填数的数量(1-4)为1则可以直接确定该数
以下代码没有用队列优化,就不要嫌弃了哈哈哈.....

AC代码:
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<stdio.h>
#include<math.h>
#include<stack>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define mem(a,num) memset(a,num,sizeof(a))
#define inf(a,n) fill(a,a+n,0x3f3f3f3f)

int get(char a)
{
    if(a>'0'&&a<='9')return a-'0';
    return 0;
}

char f(int a)
{
    return char(a+'0');
}

int kk(int x,int y)//获取当前坐标所属分块标号
{
    if(x<2){
        if(y<2)return 0;
        return 1;
    }
    if(y<2)return 2;
    return 3;
}

int main()
{
    ll T,t;
    char a[4][4];
    bool x[4][5],y[4][5],k[4][5];
    scanf("%lld",&T);
    t=T;
    while(T--)
    {
        mem(x,0);
        mem(y,0);
        mem(k,0);
        int count=0;
        for(int i=0;i<4;++i)
        for(int j=0;j<4;++j) cin>>a[i][j];
            //scanf("%c",&a[i][j]);
        for(int i=0;i<4;++i)
        for(int j=0;j<4;++j)
        {
            int tmp = get(a[i][j]);
            x[i][tmp]=1;
            y[j][tmp]=1;
            k[kk(i,j)][tmp]=1;
            if(a[i][j]=='*')count++;
        }
        while(count>0){
            for(int i=0;i<4;++i)
            for(int j=0;j<4;++j)
            {
                if(a[i][j]=='*'){
                    int tmp=0,flag=0;
                    for(int l=1;l<5;++l)
                        if(!x[i][l]&&!y[j][l]&&!k[kk(i,j)][l]){
                            tmp++;
                            flag=l;
                        }
                    if(tmp==1){
                        a[i][j]=f(flag);
                        x[i][flag]=1;
                        y[j][flag]=1;
                        k[kk(i,j)][flag]=1;
                        count--;
                    }
                }
            }
        }

        printf("Case #%lld:\n",t-T);
        for(int i=0;i<4;++i)
        {
            for(int j=0;j<4;++j) cout<<a[i][j];
            cout<<endl;
        }
    }
    return 0;
}














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

算法之旅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值