HDU 5547 Sudoku(dfs+模拟+状态判断)

97 篇文章 0 订阅
21 篇文章 0 订阅

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 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four  2×2 2×2 pieces, every piece contains 1 to 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(1T100) T(1≤T≤100) T T test cases follow. Each test case starts with an empty line followed by 4 lines. Each line consist of 4 characters. Each character represents the number in the corresponding cell (one of '1', '2', '3', '4'). '*' 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). Then output 4 lines with 4 characters each. indicate the recovered board.
Sample Input
3
****
2341
4123
3214
*243
*312
*421
*134
*41*
**3*
2*41
4*2*
Sample Output
Case #1:
1432
2341
4123
3214
Case #2:
1243
4312
3421
2134
Case #3:
3412
1234
2341
4123


题解:

给你一个4x4的矩阵,*号表示可以填的位置,要求满足数独规则并且四个角上2x2的地方要唯一,也就是只有1234,深搜+横列数列加上四个方阵判断一下就好了

代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
#define ll long long
#define INF 100861111
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
using namespace std;
int a[5][5];
int h[5][5];
int l[5][5],ans,b[5][5];
char s[5][5];
int tag;
void dfs(int x,int y,int dp)
{
    int i,j,k;
    if(tag)
        return;
    for(i=1;i<=4;i++)
    {
        if(h[x][i]||l[y][i])
            continue;
        if(x>=0&&x<=1&&y>=0&&y<=1)
        {
            if(b[1][i])
                continue;
            b[1][i]=1;
        }
        else if(x>=0&&x<=1&&y>=2&&y<=3)
        {
            if(b[2][i])
                continue;
            b[2][i]=1;
        }
        else if(x>=2&&x<=3&&y>=0&&y<=1)
        {
            if(b[3][i])
                continue;
            b[3][i]=1;
        }
        else if(x>=2&&x<=3&&y>=2&&y<=3)
        {
            if(b[4][i])
                continue;
            b[4][i]=1;
        }
        a[x][y]=i;
        h[x][i]=1;
        l[y][i]=1;
        for(j=0;j<4;j++)
        {
            for(k=0;k<4;k++)
            {
                if(a[j][k]==0)
                {
                    dfs(j,k,dp+1);
                    goto deep;
                }
            }
        }
        deep:;
        if(dp>=ans)
        {
            tag=1;
            return;
        }
        if(tag)
            return;
        if(x>=0&&x<=1&&y>=0&&y<=1)
        {
            b[1][i]=0;
        }
        else if(x>=0&&x<=1&&y>=2&&y<=3)
        {
            b[2][i]=0;
        }
        else if(x>=2&&x<=3&&y>=0&&y<=1)
        {
            b[3][i]=0;
        }
        else if(x>=2&&x<=3&&y>=2&&y<=3)
        {
            b[4][i]=0;
        }
        h[x][i]=0;
        l[y][i]=0;
        a[x][y]=0;
    }
}
int main()
{
    int i,j,test,q;
    scanf("%d",&test);
    for(q=1;q<=test;q++)
    {
        memset(h,0,sizeof(h));
        memset(l,0,sizeof(l));
        memset(b,0,sizeof(b));
        ans=0;
        for(i=0;i<4;i++)
        {
            scanf("%s",s[i]);
            for(j=0;j<4;j++)
            {
                if(s[i][j]=='*')
                {
                    ans++;
                    a[i][j]=0;
                    continue;
                }
                h[i][s[i][j]-'0']=1;
                l[j][s[i][j]-'0']=1;
                if(i>=0&&i<=1&&j>=0&&j<=1)
                    b[1][s[i][j]-'0']=1;
                else if(i>=0&&i<=1&&j>=2&&j<=3)
                    b[2][s[i][j]-'0']=1;
                else if(i>=2&&i<=3&&j>=0&&j<=1)
                    b[3][s[i][j]-'0']=1;
                else if(i>=2&&i<=3&&j>=2&&j<=3)
                    b[4][s[i][j]-'0']=1;
                a[i][j]=s[i][j]-'0';
            }
        }
        tag=0;
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                if(s[i][j]=='*')
                {
                    dfs(i,j,1);
                    goto loop;
                }
            }
        }
        loop:;
        printf("Case #%d:\n",q);
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
                printf("%d",a[i][j]);
            printf("\n");
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值