zoukankan      html  css  js  c++  java
  • POJ 2676 Sudoku(暴搜)



    Sudoku
    Time Limit: 2000MSMemory Limit: 65536K
    Total Submissions: 11672Accepted: 5801Special Judge

    Description

    Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 
    POJ 2676 Sudoku(暴搜) - qhn999 - 码代码的猿猿

    Input

    The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

    Output

    For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

    Sample Input

    1
    103000509
    002109400
    000704000
    300502006
    060000050
    700803004
    000401000
    009205800
    804000107

    Sample Output

    143628579
    572139468
    986754231
    391542786
    468917352
    725863914
    237481695
    619275843
    854396127

    Source




    #include <iostream>
    #include <cstring>
    #include <cstdio>

    using namespace std;

    int visr[11][11];
    int visc[11][11];
    int visv[4][4][11];
    int mp[10][10];
    char mmp[10][10];

    int OK=0;

    void dfs(int x,int y)
    {
        if(OK) return ;
        if(x>9&&!OK)//不要忘了最后一格
        {
            for(int i=1;i<=9;i++)
            {
                for(int j=1;j<=9;j++)
                {
                    printf("%d",mp[j]);
                }
                putchar(10);
            }
            OK=1;
            return ;
        }
        int tx,ty;

        if(mp[x][y]!=0)
        {
            ty=y+1;
            tx=x;
            if(ty==10)
            {
                tx++;
                ty=1;
            }
            dfs(tx,ty);
        }
        else if(mp[x][y]==0)
        {
            for(int i=1;i<=9;i++)
            {
                if(visr[x]==0&&visc[y]==0&&visv[(x-1)/3+1][(y-1)/3+1]==0)
                {
                    visr[x]=1;
                    visc[y]=1;
                    visv[(x-1)/3+1][(y-1)/3+1]=1;
                    tx=x;ty=y+1;
                    if(ty==10)
                    {
                        ty=1;
                        tx++;
                    }
                    mp[x][y]=i;
                    dfs(tx,ty);
                    mp[x][y]=0;
                    visr[x]=0;
                    visc[y]=0;
                    visv[(x-1)/3+1][(y-1)/3+1]=0;
                }
            }
        }

    }

    int main()
    {
        int T;
        scanf("%d",&T);
    while(T--)
    {
        memset(visr,0,sizeof(visr));
        memset(visc,0,sizeof(visc));
        memset(visv,0,sizeof(visv));
        memset(mp,0,sizeof(mp));
        memset(mmp,'0',sizeof(mmp));

        for(int i=1;i<=9;i++)
        {
            scanf("%s",mmp);
        }

        for(int i=1;i<=9;i++)
        {
            for(int j=1;j<=9;j++)
            {
                char c=mmp[j-1];
                mp[j]=c-'0';
                if(mp[j]!=0)
                {
                    visr[mp[j]]=1;
                    visc[j][mp[j]]=1;
                    visv[(i-1)/3+1][(j-1)/3+1][mp[j]]=1;
               }
            }
        }

        OK=0;
        dfs(1,1);
    }

        return 0;
    }

  • 相关阅读:
    在Tomcat运行JSP的一个问题
    英语时态的性趣学法
    温哥华蝉联全球最宜居城市榜首 Vancouver still world's most liveable city: survey
    【转】解压缩版tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
    五个常用MySQL图形化管理工具
    windows下将解压缩版的tomcat设置为自动运行的系统服务
    Java初学者Java的学习路径(全集)
    [ZT]森田学习体会
    7种错误冲奶粉法 宝宝的健康会打折
    数据库系统原理
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350983.html
Copyright © 2011-2022 走看看