hdu 5547 Sudoku【dfs】

Sudoku

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 612    Accepted Submission(s): 223


Problem Description
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 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 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 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 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


一道比较简单的数独题目,这里简化了问题,变成了一个4*4的图,然后小图变成了2*2的图,这里问题就简洁了很多。

地图辣么小,直接枚举每一个点就行了。

从左上角开始,一直枚举到右下角,然后输出了就行了。

每次遇到一个“*”我们就枚举他变成1,2,3,4然后判断是否合法,如果合法就进行下一个点,否则回溯。

这里我们强调一下如何判断是否合法的代码:

int panduan(int row,int col)//x,y
{
    for(int i=0;i<4;i++)//列判断,不能有相同的数字
    {
        if(a[row][i]==a[row][col]&&i!=col)
        return 0;
    }
    for(int i=0;i<4;i++)//行判断,不能有相同的数字
    {
        if(a[i][col]==a[row][col]&&i!=row)
        return 0;
    }
    int mrow=row;
    int mcol=col;
    if(row%2==1)row-=1;//找到小矩阵的左上角//这里也可以对行/2再*2对列/2再*2也能找到小矩阵的左上角
    if(col%2==1)col-=1;
    for(int i=row;i<=row+1;i++)//小矩阵的判断,不能有相同的数字
    {
        for(int j=col;j<=col+1;j++)
        {
            {
                if(a[i][j]==a[mrow][mcol]&&i!=mrow&&j!=mcol)return 0;
            }
        }
    }
    return 1;
}

然后是如何回溯dfs:

void dfs(int cur)
{
    //printf("yes\n");
    if(cur==4*4)//如果遍历了所有点,就输出最终图形
    {
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                printf("%c",a[i][j]);
            }
            printf("\n");
        }
        return ;
    }
    int row=cur/4;
    int col=cur%4;//行列的计算方法~
    if(a[row][col]=='*')//如果是*枚举
    {
        for(int j=1;j<=4;j++)//枚举四个数字
        {
            a[row][col]=j+'0';
            if(panduan(row,col)==1)//如果当前这个点合法
            {
                dfs(cur+1);//进行下一步
            }
            a[row][col]='*';//记得要取消标记。深搜尝试问题,涉及到回溯的问题。
        }
    }
    else//如果不是*,那么跳过,进入下一步
    {
        dfs(cur+1);
    }
}

然后上完整的AC代码:

 

#include<stdio.h>
#include<string.h>
using namespace std;
char a[100][100];
int panduan(int row,int col)
{
    for(int i=0;i<4;i++)
    {
        if(a[row][i]==a[row][col]&&i!=col)
        return 0;
    }
    for(int i=0;i<4;i++)
    {
        if(a[i][col]==a[row][col]&&i!=row)
        return 0;
    }
    int mrow=row;
    int mcol=col;
    if(row%2==1)row-=1;
    if(col%2==1)col-=1;
    for(int i=row;i<=row+1;i++)
    {
        for(int j=col;j<=col+1;j++)
        {
            {
                if(a[i][j]==a[mrow][mcol]&&i!=mrow&&j!=mcol)return 0;
            }
        }
    }
    return 1;
}
void dfs(int cur)
{
    //printf("yes\n");
    if(cur==4*4)
    {
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                printf("%c",a[i][j]);
            }
            printf("\n");
        }
        return ;
    }
    int row=cur/4;
    int col=cur%4;
    if(a[row][col]=='*')
    {
        for(int j=1;j<=4;j++)
        {
            a[row][col]=j+'0';
            if(panduan(row,col)==1)
            {
                dfs(cur+1);
            }
            a[row][col]='*';
        }
    }
    else
    {
        dfs(cur+1);
    }
}
int main()
{
    int kase=0;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<4;i++)
        {
            scanf("%s",a[i]);
        }
        printf("Case #%d:\n",++kase);
        dfs(0);
    }
    return 0;
}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值