POJ 2676 深搜+剪枝
Sudoku
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 23467 | Accepted: 10979 | 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
解题思路:直接深搜会搜索很多错误的情况,在搜索时,将这些情况排除
#include <stdio.h> #include <iostream> #include <string> #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; int n; int map_hang[10][10]; int map_lie[10][10]; int map_gezi[4][4][10]; int shudu[10][10]; bool hang_lie_gezi(int x, int y, int v) { return map_lie[y][v] == 0 && map_hang[x][v] == 0 && map_gezi[(x+2)/3][(y+2)/3][v] == 0; }//用于检测v值是否可以填在该位置,可以滤过0 0 bool empty(int x, int y) { return !shudu[x][y]; }//用于检测坐标(X,Y)是否有数 void add(int x, int y, int v) { shudu[x][y] = v; map_hang[x][v]=1; //此处应为=1,++是为了debug map_lie[y][v]=1; //此处应为=1,++是为了debug map_gezi[(x + 2) / 3][(y + 2) / 3][v]=1; } void reback(int lx, int ly, int lv) { shudu[lx][ly] = 0; map_hang[lx][lv]=0; //此处应为=0,--是为了debug map_lie[ly][lv]=0; //此处应为=0,--是为了debug map_gezi[(lx + 2) / 3][(ly + 2) / 3][lv]=0; } bool dfs(int x, int y) { if (x==10 || y == 10) { return 1; } int flag = 0; if (shudu[x][y]) { if (y == 9) flag = dfs(x + 1, 1); else flag = dfs(x, y + 1); return flag; } else { for (int v = 1; v <= 9; v++) { if (hang_lie_gezi(x, y, v)) { add(x, y, v); if (y == 9) flag = dfs(x + 1, 1); else flag = dfs(x, y + 1); if (flag == 0) { reback(x, y, v); } else { return 1; } } } } return 0; } string s; int main() { fio; cin >> n; while (n--) { memset(shudu, 0, sizeof shudu); memset(map_hang, 0, sizeof map_hang); memset(map_gezi, 0, sizeof map_gezi); memset(map_lie, 0, sizeof map_lie); for (int i = 1; i < 10; i++) { cin >> s; for (int j = 1; j < 10; j++) { shudu[i][j] = s[j-1]-'0'; } } for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { if (shudu[i][j]) { add(i, j, shudu[i][j]); } } } dfs(1, 1); for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { cout << shudu[i][j]; } cout << endl; } } return 0; }