zoukankan      html  css  js  c++  java
  • POJ2676:Sudoku(DFS) java程序员

    Sudoku
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 10849   Accepted: 5414   Special 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.

    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

    MYCode:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    bool row[9][10];
    bool col[9][10];
    bool mat[9][10];
    int map[9][9];
    int ans[9][9];
    bool reach;
    void init()
    {
        memset(row,0,sizeof(row));
        memset(col,0,sizeof(col));
        memset(mat,0,sizeof(mat));
        memset(ans,0,sizeof(ans));
        reach=false;
    }
    void dfs(int x,int y)
    {
        //cout<<"move to"<<x<<" "<<y<<endl;
        if(x==9)
        {
            //cout<<"reach"<<endl;
            reach=true;
            return;
        }
        if(reach==true)
        return;
        if(map[x][y])
        {
            if(y==8)
            dfs(x+1,0);//note
            else
            dfs(x,y+1);
        }
        else
        {
            int i;
            for(i=1;i<=9;i++)
            {
                int id=(x/3)*3+y/3;
                if((!row[x][i])&&(!col[y][i])&&(!mat[id][i]))
                {
                    //cout<<x<<" "<<y<<": "<<i<<endl;
                    ans[x][y]=i;
                    row[x][i]=1;
                    col[y][i]=1;
                    mat[id][i]=1;
                    if(y==8)
                    dfs(x+1,0);
                    else
                    dfs(x,y+1);
                    row[x][i]=0;
                    col[y][i]=0;
                    mat[id][i]=0;
                }
                if(reach)//note
                break;
                //ans[x][y]='0';
            }
        }
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int i,j;
            init();
            char ch;
            for(i=0;i<9;i++)
            {
                for(j=0;j<9;j++)
                {
                    //scanf("%d",&map[i][j]);
                    //cin>>map[i][j];
                    cin>>ch;
                    ans[i][j]=map[i][j]=ch-'0';
                    if(map[i][j]!=0)
                    {
                        int val=map[i][j];
                        row[i][val]=1;
                        col[j][val]=1;
                        int id=(i/3)*3+j/3;
                        mat[id][val]=1;
                    }
                }
            }
            dfs(0,0);
            /*if(reach==false)
            {
                printf("fail\n");
                continue;
            }*/
            for(i=0;i<9;i++)
            {
                for(j=0;j<9;j++)
                {
                    cout<<ans[i][j];
                }
                printf("\n");
            }
        }
    }
    //454MS
    DFS暴力搜索, 注意如果找到答案就要立即返回,不再搜索.
     
  • 相关阅读:
    【学车笔记】皮卡科目二考前笔记
    【Java学习笔记】继承和多态
    【Java学习笔记】Java中定义宏
    【读书笔记】《世界上最伟大的推销员》
    《大话设计模式》重印公告
    《大话设计模式》第29章OOTV杯超级模式大赛—模式总结(四)
    《大话设计模式》第29章OOTV杯超级模式大赛—模式总结(六)
    《大话设计模式》第29章OOTV杯超级模式大赛—模式总结(五)
    阅读不懂,图书之过——《大话设计模式》创作历程
    岁月凶残,敬请珍惜——得知早已不能过五四节时之随想
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215874.html
Copyright © 2011-2022 走看看