首页 > 学院 > 开发设计 > 正文

POj 2676 数独 (dfs)

2019-11-08 02:49:07
字体:
来源:转载
供稿:网友

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

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843854396127就是数独;

爆搜。。。

关键是懂得搜索姿势还有标记方法。。。 book_row book_col记录的是第几行的哪个数有没有用过。。 Book[][][]前两维记录的是分块,后面记录的是数字,初始化:if(val) book_row[i][val] = book_col[j][val] = book[i/3][j/3][val] = 1;这样就必须保证i从0开始,0/3,1/3,2/3 = 0,但是如果

从1开始 1/3,2/3 = 0.3/3 = 1,就达不到标记的情况了。。。另外像这种二位搜索,一般都是这样。。。

if(flag)return ;  

if(y == 9) x++, y = 0;  

if(x == 9) { flag = 1;return ; } 

if(maze[x][y]) dfs(x, y+1);

#include<iostream>#include<cstdio>#include<cstring>using namespace std;char str[15][15];int maze[15][15], book_row[10][10], book_col[10][10], book[5][5][10];int flag;void dfs(int x, int y){    if(flag) return ;    if(y == 9) x++, y = 0;    if(x == 9) { flag = 1; return ; }    if(maze[x][y]) dfs(x, y+1);    else    {        for(int i = 1; i <= 9; i++)        {            if(!book_row[x][i] && !book_col[y][i] && !book[x/3][y/3][i])            {                maze[x][y] = i;                book_row[x][i] = book_col[y][i] = book[x/3][y/3][i] = 1;                dfs(x, y+1);                if(flag) return;                maze[x][y] = book_row[x][i] = book_col[y][i] = book[x/3][y/3][i] = 0;            }        }    }}int main(void){    //freopen("in.txt", "r", stdin);    int t;    cin >> t;    while(t--)    {        flag = 0;        memset(book_row, 0, sizeof(book_row));        memset(book_col, 0, sizeof(book_col));        memset(book, 0, sizeof(book));        for(int i = 0; i < 9; i++)            {                scanf("%s", str[i]);                for(int j = 0; j < 9; j++)                {                    int val = str[i][j]-'0';                    maze[i][j] = val;                    if(val) book_row[i][val] = book_col[j][val] = book[i/3][j/3][val] = 1;                }            }        dfs(0, 0);        for(int i = 0; i < 9; i++)            for(int j = 0; j < 9; j++)            {                printf("%d", maze[i][j]);                if(j == 8) printf("/n");            }    }    return 0;}

我一开始姿势不对。。。 这样写的,仔细想想,这样不能回溯所有的。。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int book_row[10][10], book_col[10][10], book[10][10][10], maze[10][10], flag;char str[15][15];void dfs(int x){    if(flag) return;    if(x == 10)    {        for(int i = 1; i <= 9; i++)        {            for(int j = 1; j <= 9; j++)            {                printf("%d", maze[i][j]);            }            printf("/n");        }        flag = 1;    }    for(int y = 1; y <= 9; y++)        for(int i = 1; i <= 9; i++)        {            if(!book_row[x][i] && !book_col[y][i] && !book[x/3][y/3][i])            {                book_row[x][i] = book_col[y][i] = book[x/3][y/3][i] = 1;                maze[x][y] = i;                dfs(x+1);                book_row[x][i] = book_col[y][i] = book[x/3][y/3][i] = 0;            }        }}int main(){    int t;    scanf("%d", &t);    while(t--)    {        memset(book_row, 0, sizeof(book_row));        memset(book_col, 0, sizeof(book_col));        memset(book, 0, sizeof(book));        flag = 0;        for(int i = 1; i <= 9; i++)        {            scanf("%s", str[i]+1);            for(int j = 1; j <= 9; j++)                maze[i][j] = str[i][j] - '0';        }        dfs(1);    }    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表