Sudoku POJ - 3074(DFS)

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

就是搜索了。但是实现起来不好实现吧,关键是剪枝,和常数优化了。

代码:

import java.util.Arrays;
import java.util.Scanner;

public class Main 
{
    static int cnt[]=new int[512];
    static int num[]=new int[512];
    static char str[][]=new char[10][10];
    static int row[]=new int[9];
    static int col[]=new int[9];
    static int grid[]=new int[9];
    static int g(int x,int y)
    {
        return ((x/3)*3)+y/3;
    }
    static void filp(int x,int y,int z)
    {
        row[x]^=1<<z;
        col[y]^=1<<z;
        grid[g(x,y)]^=1<<z;
    }
    static boolean dfs(int now)
    {
        if(now==0)return true;
        int temp=10,x=0,y=0;
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++)
            {
                if(str[i][j]!='0')continue;
                int val=row[i]&col[j]&grid[g(i,j)];
                if(val==0)return false;
                if(cnt[val]<temp)
                {
                    temp=cnt[val];
                     x=i;
                     y=j;
                }//从填的数量最少的开始填
            }
        int val=row[x]&col[y]&grid[g(x,y)];
        for(;val>0;val-=val&-val)
        {
            int z=num[val&-val];
            str[x][y]=(char)('1'+z);
            filp(x,y,z);
            if(dfs(now-1))return true;
            filp(x,y,z);
            str[x][y]='0';
        }
        return false;
    }
    public static void main(String[] args) 
    {
        for(int i=0;i<(1<<9);i++)
            for(int j=i;j>0;j-=j&-j)cnt[i]++;
        for(int i=0;i<9;i++)
            num[1<<i]=i;
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            for(int i=0;i<9;i++)
            {
                String s=sc.next();
                for(int j=0;j<9;j++)
                    str[i][j]=s.charAt(j);
            }
            for(int i=0;i<9;i++)
                row[i]=col[i]=grid[i]=(1<<9)-1;
            int tot=0;
            for(int i=0;i<9;i++)
                for(int j=0;j<9;j++)
                    if(str[i][j]!='0')filp(i,j,str[i][j]-'1');
                    else
                        tot++;
            dfs(tot);
            for(int i=0;i<9;i++)
            {
                for(int j=0;j<9;j++)
                    System.out.print(str[i][j]);
                System.out.println();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/coldfresh/article/details/80273817
今日推荐