国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

POj 2676 數(shù)獨(dú) (dfs)

2019-11-08 02:49:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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就是數(shù)獨(dú);

爆搜。。。

關(guān)鍵是懂得搜索姿勢(shì)還有標(biāo)記方法。。。 book_row book_col記錄的是第幾行的哪個(gè)數(shù)有沒(méi)有用過(guò)。。 Book[][][]前兩維記錄的是分塊,后面記錄的是數(shù)字,初始化:if(val) book_row[i][val] = book_col[j][val] = book[i/3][j/3][val] = 1;這樣就必須保證i從0開(kāi)始,0/3,1/3,2/3 = 0,但是如果

從1開(kāi)始 1/3,2/3 = 0.3/3 = 1,就達(dá)不到標(biāo)記的情況了。。。另外像這種二位搜索,一般都是這樣。。。

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;}

我一開(kāi)始姿勢(shì)不對(duì)。。。 這樣寫(xiě)的,仔細(xì)想想,這樣不能回溯所有的。。

#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;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 抚州市| 桓台县| 蒙城县| 镇宁| 黔南| 筠连县| 周宁县| 滦南县| 平果县| 富蕴县| 高淳县| 新郑市| 常宁市| 安顺市| 宝山区| 津南区| 武川县| 莱芜市| 高邮市| 双辽市| 乐昌市| 大港区| 宜章县| 平凉市| 商丘市| 石林| 景泰县| 松原市| 仙桃市| 云林县| 承德县| 朔州市| 静乐县| 牙克石市| 龙岩市| 龙里县| 隆林| 杭州市| 洛川县| 陵川县| 台南市|