zoukankan      html  css  js  c++  java
  • 数独 还能优化

    #include <iostream>
    using namespace std;
    
    
    int X[10][10][10];
    int flag[10][10];
    int sol[10][10];
    int N=81;
    int hasValueFlag[10][10];
    int check(int n)
    {
        int baseRow,baseCol;
        int row=n/9+1;
        int col=n%9+1;
        int i;
        int j;
        for (i=1;i<10;i++)
        {
            for (j=1;j<10;j++)
            {
                hasValueFlag[i][j]=0;
            }
        }
        for (i=1;i<row;i++)
        {
            for (j=1;j<10;j++)
            {
                hasValueFlag[i][j]=1;
            }
        }
        for (j=1;j<=col;j++)
        {
            hasValueFlag[row][j]=1;
        }
        for (i=1;i<10;i++)
        {
            for (j=1;j<10;j++)
            {
                if (flag[i][j])
                {
                    hasValueFlag[i][j]=1;
                }            
            }
        }
        for (i=1;i<10;i++)
        {
            if (i!=col&&hasValueFlag[row][i])
            {
                if (sol[row][col]==sol[row][i])
                {
                    return 0;
                }
            }
        }
        for (i=1;i<10;i++)
        {
            if (i!=row&&hasValueFlag[i][col])
            {
                if (sol[row][col]==sol[i][col])
                {
                    return 0;
                }
            }
        }
        baseRow=(row-1)/3+1;
        baseCol=(col-1)/3+1;
        for (i=(baseRow-1)*3+1;i<(baseRow-1)*3+4;i++)
        {
            for (j=(baseCol-1)*3+1;j<(baseCol-1)*3+4;j++)
            {
                if (i!=row&&j!=col&&hasValueFlag[i][j])
                {
                    if (sol[i][j]==sol[row][col])
                    {
                        return 0;
                    }
                }
            }
        }
        return 1;
    }
    void bt(int n)//n from 0
    {
        int baseRow,baseCol;
        if (n==N)
        {
            int i,j;
            for (i=1;i<10;i++)
            {
                for (j=1;j<10;j++)
                {
                    cout<<sol[i][j]<<" ";
                }
                cout<<endl;
            }
            getchar();
        }
        else
        {
            int row=n/9+1;
            int col=n%9+1;
            if (flag[row][col])
            {
                if (check(n))
                {
                    bt(n+1);
                }            
            }
            else
            {
                int i;
                for (i=1;i<10;i++)
                {
                    if (X[row][col][i])
                    {
                        sol[row][col]=i;
                        if (check(n))
                        {
                            bt(n+1);
                        }
                    }
                }
            }
        }
    }
    void execlueCell(int row,int col)
    {
        int i,j;
        int baseRow,baseCol;
        if (flag[row][col])
        {
            return;
        }
        for (i=1;i<10;i++)
        {
            if (i!=col&&flag[row][i])
            {
                int t=sol[row][i];
                X[row][col][t]=0;
            }
        }
        for (i=1;i<10;i++)
        {
            if (i!=row&&flag[i][col])
            {
                int t=sol[i][col];
                X[row][col][t]=0;
            }
        }
        baseRow=(row-1)/3+1;
        baseCol=(col-1)/3+1;
        for (i=(baseRow-1)*3+1;i<(baseRow-1)*3+4;i++)
        {
            for (j=(baseCol-1)*3+1;j<(baseCol-1)*3+4;j++)
            {
                if (i!=row&&j!=col&&flag[i][j])
                {
                    int t=sol[i][j];
                    X[row][col][t]=0;
                }
            }
        }
    }
    void execlue()
    {
        int i,j;
        memset(X,1,sizeof(X));
        for (i=1;i<10;i++)
        {
            for (j=1;j<10;j++)
            {
                execlueCell(i,j);
            }
        }
    }
    int main()
    {
        int i;
        int j;
        int t;
        cout<<"input num
    ";
        for (i=1;i<10;i++)
        {
            for (j=1;j<10;j++)
            {
                cin>>t;
                sol[i][j]=t;
                if(t!=0)
                {
                    flag[i][j]=1;
                }
            }
        }
        cout<<"computing...
    ";
        execlue();
        bt(0);
        return 0;
    }
  • 相关阅读:
    linux试题
    linux常用脚本
    nagios
    lvs/nginx/haproxy 负载均衡优缺点分析讲解
    一次SSLPeerUnverifiedException,SSLHandshakeException问题的分析
    [转]【安卓笔记】AsyncTask源码剖析
    linux下查看进程占用端口和端口占用进程命令
    which framework or library is best to use WebRTC
    [转]svn diff 替代工具
    [转]使用Subversion进行版本控制
  • 原文地址:https://www.cnblogs.com/mengqingzhong/p/3493324.html
Copyright © 2011-2022 走看看