zoukankan      html  css  js  c++  java
  • 南阳oj 题目722 数独

    数独

    时间限制:1000 ms | 内存限制:65535 KB
    难度:4

    描述
    数独是一种运用纸、笔进行演算的逻辑游戏。玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个3*3宫内的数字均含1-9,不重复。 每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为基础,任何无解或多解的题目都是不合格的。
    有一天hrdv碰到了一道号称是世界上最难的数独的题目,作为一名合格的程序员,哪能随随便便向困难低头,于是他决定编个程序来解决它。。
    这里写图片描述
    输入
    第一行有一个数n(0< n <100),表示有n组测试数据,每组测试数据是由一个9*9的九宫格构成,0表示对应的格子为空
    输出
    输出一个9*9的九宫格,为这个数独的答案
    样例输入
    1
    0 0 5 3 0 0 0 0 0
    8 0 0 0 0 0 0 2 0
    0 7 0 0 1 0 5 0 0
    4 0 0 0 0 5 3 0 0
    0 1 0 0 7 0 0 0 6
    0 0 3 2 0 0 0 8 0
    0 6 0 5 0 0 0 0 9
    0 0 4 0 0 0 0 3 0
    0 0 0 0 0 9 7 0 0
    样例输出
    1 4 5 3 2 7 6 9 8
    8 3 9 6 5 4 1 2 7
    6 7 2 9 1 8 5 4 3
    4 9 6 1 8 5 3 7 2
    2 1 8 4 7 3 9 5 6
    7 5 3 2 9 6 4 8 1
    3 6 7 5 4 2 8 1 9
    9 8 4 7 6 1 2 3 5
    5 2 1 8 3 9 7 6 4

    无脑递归,注意递归条件就好

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    struct sdf
    {
        int x,y;
    }transfer[300];
    int rampant[10][10];//对每行进行存储
    int wale[10][10];//对每列进行存储
    int release[10][10];//对每个长度为3的方格
    int matrix[10][10];//整个九宫格
    int state[3][3]={{0,1,2},{3,4,5},{6,7,8}};//保存宫的信息
    int n,m,sum,sta;
    void dfs(int tot)
    {
        if(sta==1)//结束 递归
            return ;
        else if(tot==sum)
        {
            for(int i=0;i<9;i++)
            {
                for(int j=0;j<8;j++)
                    printf("%d ",matrix[i][j]);
                printf("%d
    ",matrix[i][8]);
            }
            sta=1;
            return ;
        }
        for(int i=1;i<=9;i++)
        {
            if(rampant[transfer[tot].x][i]==0&&wale[transfer[tot].y][i]==0&&
            release[state[transfer[tot].x/3][transfer[tot].y/3]][i]==0)
            {
                rampant[transfer[tot].x][i]=1;
                wale[transfer[tot].y][i]=1;
                release[state[transfer[tot].x/3][transfer[tot].y/3]][i]=1;
                matrix[transfer[tot].x][transfer[tot].y]=i;
                dfs(tot+1);
               // matrix[transfer[tot].x][transfer[tot].y]=0;
                rampant[transfer[tot].x][i]=0;
                wale[transfer[tot].y][i]=0;
                release[state[transfer[tot].x/3][transfer[tot].y/3]][i]=0;
            }
        }
    }
    int main()
    {
        int k;
        scanf("%d",&k);
        while(k--)
        {
            sum=0;
            sta=0;
            memset(rampant,0,sizeof(rampant));
            memset(wale,0,sizeof(wale));
            memset(release,0,sizeof(release));
            for(int i=0;i<9;i++)
            {
                for(int j=0;j<9;j++)
                {
                    scanf("%d",&matrix[i][j]);
                    if(matrix[i][j]==0)//存储地图上的要填数坐标
                    {
                        transfer[sum].x=i;
                        transfer[sum].y=j;
                        sum++;
                    }
                    else//对已经有数的格子进行保存标记
                    {
                        rampant[i][matrix[i][j]]=1;
                        wale[j][matrix[i][j]]=1;
                        release[state[i/3][j/3]][matrix[i][j]]=1;
                    }
                }
            }
            dfs(0);
        }
        return 0;
    }
    
  • 相关阅读:
    hadoop hdfs基本命令的java编码实现
    三维空间旋转和Three.JS中的实现
    Talk about VR
    Keras bug in model.predict
    How to compile tensorflow on CentOS
    熵(Entropy),交叉熵(Cross-Entropy),KL-松散度(KL Divergence)
    Two kinds of item classification model architecture
    Three failed attempts of handling non-sequential data
    How to setup Tensorflow inception-v3 model on Windows
    (译)三维空间中的几种坐标系
  • 原文地址:https://www.cnblogs.com/nanfenggu/p/7900110.html
Copyright © 2011-2022 走看看