poj Sudoku(数独) DFS

Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13665   Accepted: 6767   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讲解:游戏都玩过,但就是基本上见过没做出来过,哈哈、、、挺有意思的代码,题意都知道,关键就是如何进行搜索,其实就是不断地进行枚举,如果满足所有的条件就跳出来;AC代码:
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 const int N = 11;
 7 int Map[N][N], flag;
 8 char MAp[N][N];
 9 int check(int ans,int key)
10 {
11     int x = (key-1)/9+1;
12     int y =  key-(x-1)*9;
13     for(int i=1; i<=9; i++)//行是否冲突
14        if(Map[x][i] == ans)
15             return 0;
16     for(int i=1; i<=9; i++) //列是否冲突
17        if(Map[i][y] == ans)
18            return 0;
19            if(x<=3)x = 1; //x所在小九格起点
20            if(x>3 && x<7)x=4;
21            if(x>=7) x=7;
22            if(y<=3) y = 1; //y所在小九格起点
23            if(y>3 && y<7) y=4;
24            if(y>=7) y=7;
25     for(int i=0; i<3; i++) //小的九格是否冲突
26        for(int j=0; j<3; j++)
27             if(Map[x+i][y+j] == ans)
28                     return 0;
29   return 1;//以上条件都不符合,返回1;
30 }
31 int dfs(int key)
32 {
33     int x = (key-1)/9+1;
34     int y =  key-(x-1)*9;
35     if(key>81)
36     {
37         flag = 1;
38         return 0;
39     }
40     if(Map[x][y]!=0)//不是0,直接跳过去
41     {
42         dfs(key+1);
43     }
44     else //否者,在这个位置枚举九个数,进行递归搜索
45     {
46     for(int k=1; k<=9; k++)
47         if(check(k,key))
48             {
49                 Map[x][y] = k;
50                 dfs(key+1);
51                 if(flag == 1)  return 0;
52                 Map[x][y] = 0;
53             }
54     }
55     return 0;
56 }
57 int main()
58 {
59     int T,i,j;
60 //    freopen("in2.txt","r",stdin);
61 //    freopen("out2.txt","w",stdout);
62     scanf("%d",&T);
63     while(T--)
64     {
65         memset(Map,0,sizeof(Map));
66        for(i=1; i<=9; i++)
67           for(j=1; j<=9; j++)
68           {
69              cin>>MAp[i][j];
70              Map[i][j] = MAp[i][j]-‘0‘;
71           }
72              flag = 0;
73               dfs(1);
74        for(i=1; i<=9; i++)
75        {
76           for(j=1; j<=9; j++)
77              printf("%d",Map[i][j]);
78              printf("\n");
79        }
80     }
81     return 0;
82 }

poj Sudoku(数独) DFS

时间: 2024-10-20 11:16:38

poj Sudoku(数独) DFS的相关文章

POJ 2676 Sudoku (数独 DFS)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   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

POJ Sudoku 数独填数 DFS

题目链接:Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18105   Accepted: 8772   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 Fig

POJ 2676 数独(DFS)

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

POJ 2676 Sudoku (数独)

经典搜索问题,主要是时间上的优化,我用了三个辅助数组记录信息 row[i][k] = 1表示第i行数字k已经被使用,col[j][k] = 1表第j列数字k已经被使用,blo[i][k]表示第i个小九宫格中数字k已经被使用 还有很重要的一个优化(没有优化的话可能会超时,或者非常慢,像POJ讨论区里有很多说正着搜超时,倒着搜0ms,这的确是一个可以用的方法,但是有一定的随机性),每次填数字时,先扫描一遍整个矩阵,找出可选数字情况最少的那个0所在的地方,优先填这里,这样会使得搜索树尽可能的"瘦&qu

POJ 3076 数独DLX

Sudoku Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 4203   Accepted: 2051 Description A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital l

POJ2676 Sudoku [数独]

好题,也很实用,犯了几个错误 1.在枚举赋值的时候,思维有个错误:当当前的赋值不能填完这个数独,应该是继续下一个循环,而不是return false 终止枚举 2.Generic Programing写错了,,,本来那个memset想写成Generic Programing的,,,然后,永远只有第一组结果对 不说了,泪哈,,, #include <cstdio> #include <cstring> #include <iostream> #include <cs

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

[POJ 1011]Sticks(DFS剪枝)

Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were original

poj 2918 Tudoku 数独dfs

题意: 解数独游戏. 分析: 这道数独的数独直接dfs就能过. 代码: //poj 2918 //sep9 #include<iostream> using namespace std; char s[12][12]; int board[12][12]; int CheckSquare[12][12]; int CheckRow[12][12]; int CheckColumn[12][12]; int ok; int GetSquareId(int i,int j) { int r=i/3