poj 2676(dfs求解數獨問題,對行列和格子分別加bool數組優化搜索)

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

Source


 

之前做過的 八皇后問題非常類似。需要用row, col, gezi三個輔助數組來優化每行沒列每小格的9個數字是否出現(bool數組即可)。

第一次編譯後,樣例沒有過,是因爲在讀入的時候,blank一定要最後再++,否則會錯位。。。

修正後,1A。 現在簡單的搜索題,我已經基本上可以完全做到1A,其實dfs搜索,只要注意遞歸終止條件+恢復現場這兩點,基本上不會WA。

提交記錄:

1、Accepted!

/*Source Code

Problem: 2676		User: 775700879
Memory: 724K		Time: 907MS
Language: G++		Result: Accepted
Source Code*/
#include 
#include 
#include 
using namespace std;
bool row[9][10];
bool col[9][10]; 
bool gezi[9][10];
int data[9][9];
int blank;
struct Node {
    int x, y;
};
Node node[100];
int getgezi(int x, int y) {
    int result = 0;
    result += x / 3 * 3;
    result += y / 3;
    return result;
}
bool check(int position, int num) {
    int x = node[position].x;
    int y = node[position].y;
    if (row[x][num] == true) return false;
    if (col[y][num] == true) return false;
    if (gezi[getgezi(x, y)][num] == true) return false;
    return true;
}
bool dfs(int num, int blank) {
    if (num == blank) return true;
    int i;
    for (i = 1; i <= 9; i++) {
        if (data[node[num].x][node[num].y] == 0 && check(num, i)) {
            data[node[num].x][node[num].y] = i;
            row[node[num].x][i] = true;
            col[node[num].y][i] = true;
            gezi[getgezi(node[num].x, node[num].y)][i] = true;        
            if(dfs(num+1, blank)) return true;
            row[node[num].x][i] = false;
            col[node[num].y][i] = false;
            gezi[getgezi(node[num].x, node[num].y)][i] = false;        
            data[node[num].x][node[num].y] = 0;
        }
    }
    return false; 
}
int main() {
    int t, i, j;
    int r, c;
    char ch;
    cin >> t;
    while (t--) {
        blank = 0;
        memset(row, 0, sizeof(row));
        memset(col, 0, sizeof(row));
        memset(gezi, 0, sizeof(row));
        for (i = 0; i < 9; i++) {
            for (j = 0; j < 9; j++) {
                cin >> ch;
                data[i][j] = ch - '0';
                row[i][data[i][j]] = true;
                col[j][data[i][j]] = true;
                gezi[getgezi(i, j)][data[i][j]] = true;
                if (data[i][j] == 0) {node[blank].x = i; node[blank].y = j; blank++;}
            }
        }
        dfs(0, blank);
        for (i = 0; i < 9; i++) {
            for (j = 0; j < 9; j++) {
                cout << data[i][j];
            }
            cout << endl;
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章