POJ 2676 Sudoku 数独游戏
2011-03-02 18:32
381 查看
DFS.
/*Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7686 Accepted: 3757 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 Southeastern Europe 2005*/ #include "stdlib.h" #include "stdio.h" #include "string.h" #define CELL_NUM 9 #define SUDOKU_SUCCESS 0 #define SUDOKU_FAIL -1 #define INFINITE 0XFF typedef struct _SUDOKU_QUEUE_ST_ { int iIndexX; int iIndexY; int iValidNum; }SUDOKU_QUEUE_ST; #define SUDOKU_VALID 1 #define SUDOKU_INVALID 0 char gacRowValid[CELL_NUM][CELL_NUM+1]; char gacColumnValid[CELL_NUM][CELL_NUM+1]; char gacSubsquareValid[CELL_NUM][CELL_NUM+1]; int gaiSudoku[CELL_NUM][CELL_NUM]; SUDOKU_QUEUE_ST gastSudokuQueue[CELL_NUM*CELL_NUM]; int SudokuResolve(int viIndex) { int iLoop; int iIndexX; int iIndexY; int iReturnVal; if (INFINITE == gastSudokuQueue[viIndex].iValidNum ||CELL_NUM*CELL_NUM == viIndex) { return SUDOKU_SUCCESS; } iIndexX = gastSudokuQueue[viIndex].iIndexX; iIndexY = gastSudokuQueue[viIndex].iIndexY; for (iLoop = 1; iLoop <= CELL_NUM; iLoop++) { if (SUDOKU_VALID == gacRowValid[iIndexX][iLoop] &&SUDOKU_VALID == gacColumnValid[iIndexY][iLoop] &&SUDOKU_VALID == gacSubsquareValid[(iIndexX/3)*3+iIndexY/3][iLoop]) { gaiSudoku[iIndexX][iIndexY] = iLoop; gacRowValid[iIndexX][iLoop] = SUDOKU_INVALID; gacColumnValid[iIndexY][iLoop] = SUDOKU_INVALID; gacSubsquareValid[(iIndexX/3)*3+iIndexY/3][iLoop] = SUDOKU_INVALID; iReturnVal = SudokuResolve(viIndex+1); if(SUDOKU_SUCCESS != iReturnVal) { gaiSudoku[iIndexX][iIndexY] = 0; gacRowValid[iIndexX][iLoop] = SUDOKU_VALID; gacColumnValid[iIndexY][iLoop] = SUDOKU_VALID; gacSubsquareValid[(iIndexX/3)*3+iIndexY/3][iLoop] = SUDOKU_VALID; } else { return SUDOKU_SUCCESS; } } } return SUDOKU_FAIL; } int SudokuCmp(const void *a,const void *b) { if (((SUDOKU_QUEUE_ST *)a)->iValidNum > ((SUDOKU_QUEUE_ST *)b)->iValidNum) { return 1; } else { return -1; } } void SudokuPreProc() { int iLoop; int iLoopX; int iLoopY; int iTemp; /*init*/ memset(gacRowValid,SUDOKU_VALID,CELL_NUM*(CELL_NUM+1)); memset(gacColumnValid,SUDOKU_VALID,CELL_NUM*(CELL_NUM+1)); memset(gacSubsquareValid,SUDOKU_VALID,CELL_NUM*(CELL_NUM+1)); for (iLoopX = 0; iLoopX < CELL_NUM; iLoopX++) { for (iLoopY = 0; iLoopY < CELL_NUM; iLoopY++) { if (0 != gaiSudoku[iLoopX][iLoopY]) { gacRowValid[iLoopX][gaiSudoku[iLoopX][iLoopY]] = SUDOKU_INVALID; gacColumnValid[iLoopY][gaiSudoku[iLoopX][iLoopY]] = SUDOKU_INVALID; gacSubsquareValid[(iLoopX/3)*3+iLoopY/3][gaiSudoku[iLoopX][iLoopY]] = SUDOKU_INVALID; } } } for (iLoopX = 0; iLoopX < CELL_NUM; iLoopX++) { for (iLoopY = 0; iLoopY < CELL_NUM; iLoopY++) { gastSudokuQueue[iLoopX*9+iLoopY].iIndexX = iLoopX; gastSudokuQueue[iLoopX*9+iLoopY].iIndexY = iLoopY; if (0 != gaiSudoku[iLoopX][iLoopY]) { gastSudokuQueue[iLoopX*9+iLoopY].iValidNum = INFINITE; continue; } gastSudokuQueue[iLoopX*9+iLoopY].iValidNum = 0; for (iLoop = 1; iLoop <= CELL_NUM; iLoop++) { if (SUDOKU_VALID == gacRowValid[iLoopX][iLoop] &&SUDOKU_VALID == gacColumnValid[iLoopY][iLoop] &&SUDOKU_VALID == gacSubsquareValid[(iLoopX/3)*3+iLoopY/3][iLoop]) { iTemp = iLoop; gastSudokuQueue[iLoopX*9+iLoopY].iValidNum++; } } if(1 == gastSudokuQueue[iLoopX*9+iLoopY].iValidNum) { gaiSudoku[iLoopX][iLoopY] = iTemp; gacRowValid[iLoopX][iTemp] = SUDOKU_INVALID; gacColumnValid[iLoopY][iTemp] = SUDOKU_INVALID; gastSudokuQueue[iLoopX*9+iLoopY].iValidNum = INFINITE; gacSubsquareValid[(iLoopX/3)*3+iLoopY/3][iTemp] = SUDOKU_INVALID; } } } qsort(gastSudokuQueue,CELL_NUM*CELL_NUM,sizeof(SUDOKU_QUEUE_ST),SudokuCmp); return; } int main(void) { int iCaseNum; int iLoopX; int iLoopY; char acBuffer[CELL_NUM+1]; scanf("%d",&iCaseNum); while(iCaseNum--) { for (iLoopX = 0; iLoopX < CELL_NUM; iLoopX++) { scanf("%s",acBuffer); for (iLoopY = 0; iLoopY < CELL_NUM; iLoopY++) { gaiSudoku[iLoopX][iLoopY] = acBuffer[iLoopY] - '0'; } } SudokuPreProc(); SudokuResolve(0); for (iLoopX = 0; iLoopX < CELL_NUM; iLoopX++) { for (iLoopY = 0; iLoopY < CELL_NUM; iLoopY++) { printf("%d",gaiSudoku[iLoopX][iLoopY]); } printf("/n"); } } return 0; } /*========================END OF FILE=======================================*/
相关文章推荐
- poj 2676 Sudoku(dfs)
- 【POJ】2676 Sudoku 【数独】精确覆盖
- POJ 2676 Sudoku
- poj(2676)——Sudoku
- 数独 Sudoku poj 2676
- poj-2676 Sudoku
- POJ 2676.Sudoku
- POJ 2676 Sudoku [暴搜]
- poj 2676 sudoku
- POJ 2676 Sudoku
- POJ 2676 Sudoku1 (DFS)
- POJ 2676—Sudoku
- 数独 Sudoku poj 2676
- poj 2676 Sudoku (基础DFS)
- poj(2676)——Sudoku
- POJ 2676 Sudoku【DancingLinks,数独】
- poj 2676 Sudoku
- POJ 2676 Sudoku
- POJ 2676 Sudoku(经典DFS)
- POJ 2676 Sudoku