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

首頁 > 學院 > 開發設計 > 正文

POJ 2676 Sudoku1 (DFS)

2019-11-11 03:27:29
字體:
來源:轉載
供稿:網友

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.

img

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

題意

給出一個16*16矩陣的部分格,其中0為空格,要求填充這些空格。

使矩陣滿足橫豎和九個3*3的方格內的數字都包含1~9這9個數字。

思路

很基礎的DFS,先存儲當前每行每列和每塊的數字狀態,然后從左上角開始搜索,遇到零的時候向該點填充一個當前行、當前列、當前塊都不存在的一個數字,若無法填充,則結束此層DFS,若可行,繼續搜索下一層,直到搜索到右下角,標記已經找到答案,此時結束所有DFS,注意在結束的過程中要保留當前所填充的矩陣。

AC 代碼

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int mapp[9][9]; //存儲當前狀態bool isx[9][10],isy[9][10],iss[3][3][10],flag; //行、列、塊判斷void dfs(int y){ if(y>=9) //最后一個數是0的情況下若遞歸到這里,則說明全部填充完畢 { flag=true; return; } for(int i=y; i<9; i++) //從第y行開始,遍歷剩下的點 for(int j=0; j<9; j++) { if(mapp[i][j]==0) //一個需要判斷的點 { bool isc=false; //這一點是否可以被填充 for(int k=1; k<=9; k++) //1-9 if(!isx[j][k]&&!isy[i][k]&&!iss[i/3][j/3][k]) //三種情況都滿足 { isc=true; //假設可以填充 mapp[i][j]=k; //填充的數是k isx[j][k]=isy[i][k]=iss[i/3][j/3][k]=true; //標記該位置 dfs(j!=8?y:y+1); if(flag)return; //找到答案返回,放在這里可以防止在層層結束遞歸的時候狀態被還原 else isc=false; //下層遞歸失敗,標記該點不能填充 mapp[i][j]=0; //還原狀態 isx[j][k]=isy[i][k]=iss[i/3][j/3][k]=false; } if(!isc)return; } else if(i==8&&j==8) //最后一個點不是0的情況 { flag=true; return; } }}int main(){ int n; scanf("%d",&n); for(int ni=0; ni<n; ni++) { memset(isx,false,sizeof(isx)); memset(isy,false,sizeof(isy)); memset(iss,false,sizeof(iss)); flag=false; for(int i=0; i<9; i++) for(int j=0; j<9; j++) { scanf("%1d",&mapp[i][j]); if(mapp[i][j]) { isx[j][mapp[i][j]]=true; isy[i][mapp[i][j]]=true; iss[i/3][j/3][mapp[i][j]]=true; } } dfs(0); for(int i=0; i<9; i++) for(int j=0; j<9; j++) printf(j!=8?"%d":"%d/n",mapp[i][j]); } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 三都| 天祝| 隆尧县| 安岳县| 卢湾区| 兴义市| 奎屯市| 毕节市| 资中县| 米易县| 剑阁县| 冀州市| 鹤峰县| 泊头市| 深泽县| 隆林| 吉林省| 保山市| 道真| 阿克陶县| 廊坊市| 禹州市| 宜章县| 博客| 翁源县| 马龙县| 金门县| 资兴市| 彰化市| 元江| 大方县| 明光市| 龙胜| 永善县| 定州市| 咸阳市| 湛江市| 灵璧县| 赤峰市| 松桃| 名山县|