Sicily 1162. Sudoku
来源:互联网 发布:51单片机模块化编程pdf 编辑:程序博客网 时间:2024/05/05 13:02
1162. Sudoku
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB , 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
#include <iostream>#include <vector>#include <string.h>#include <cstring>#include <stdio.h>#include <algorithm>using namespace std;//数独题,深搜,这是剪枝的char ans[10][10];//用来储存最终答案bool num_in_row[10][10], num_in_col[10][10], num_in_blo[10][10];//这里的数组[i][j]表示在第i行/列/块里面已经有了j这个数字(有的时候为true)bool is_ok;//是否找到了答案int blank_num;//空白的数目struct Blank { int pos_row, pos_col, pos_blo, possibility;//其中的possibility就是表示可能的数字的个数}blank[85];int find_block(int x, int y) {//返回属于的块编号 int block[10][10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 0, 4, 4, 4, 5, 5, 5, 6, 6, 6, 0, 4, 4, 4, 5, 5, 5, 6, 6, 6, 0, 4, 4, 4, 5, 5, 5, 6, 6, 6, 0, 7, 7, 7, 8, 8, 8, 9, 9, 9, 0, 7, 7, 7, 8, 8, 8, 9, 9, 9, 0, 7, 7, 7, 8, 8, 8, 9, 9, 9 }; return block[x][y];}void dfs(int blank_now) {//blank_now指当前要填的空的编号 if (is_ok == true)//找到答案了就返回 return; if (blank_now == blank_num) {//如果blank_now超过了总的空白数,也就是说空白都填完了那就返回 is_ok = true; return; } for (int possible = 1; possible <= 9; possible++) {//在一个空上有9种可能 if (!num_in_row[blank[blank_now].pos_row][possible] && !num_in_col[blank[blank_now].pos_col][possible] && !num_in_blo[blank[blank_now].pos_blo][possible]) { ans[blank[blank_now].pos_row][blank[blank_now].pos_col] = possible + '0';//先填入答案中,就算不对,后来填的也可以覆盖 num_in_row[blank[blank_now].pos_row][possible] = true;//并更新这个空白的限制信息 num_in_col[blank[blank_now].pos_col][possible] = true; num_in_blo[blank[blank_now].pos_blo][possible] = true; dfs(blank_now + 1);//深搜 if (is_ok)//找到答案直接返回完事 return; num_in_row[blank[blank_now].pos_row][possible] = false;//程序运行到这说明前面的假设没找到答案,因此还原这个空白的限制信息 num_in_col[blank[blank_now].pos_col][possible] = false; num_in_blo[blank[blank_now].pos_blo][possible] = false; } }}void set_blank(int k, int i, int j) { blank[k].possibility = 0; blank[k].pos_row = i; blank[k].pos_col = j; blank[k].pos_blo = find_block(i, j);}void calculate(int k) {//这里是计算可能的数的个数 for (int temp = 1; temp <= 9; temp++) { if (!num_in_row[blank[k].pos_row][temp] && !num_in_col[blank[k].pos_col][temp] && !num_in_blo[blank[k].pos_blo][temp]) { blank[k].possibility++; } }}bool cmp(const Blank &a, const Blank &b) {//按照从小到大的顺序排序 return a.possibility < b.possibility;}int main() { int case_num, i, j; scanf("%d", &case_num); while (case_num--) { blank_num = 0; is_ok = false; memset(num_in_row, false, sizeof(num_in_row)); memset(num_in_col, false, sizeof(num_in_col)); memset(num_in_blo, false, sizeof(num_in_blo)); for (i = 1; i <= 9; i++) { scanf("%s", ans[i] + 1); } for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { if (ans[i][j] != '0') {//不是空白就更新限制信息 num_in_row[i][ans[i][j] - '0'] = true;//更新限制信息 num_in_col[j][ans[i][j] - '0'] = true; num_in_blo[find_block(i, j)][ans[i][j] - '0'] = true; } else { set_blank(blank_num, i, j); blank_num++; } } } for (i = 0; i < blank_num; i++) {//计算possibility calculate(i); } sort(blank, blank + blank_num, cmp);//排序,也就是剪枝 dfs(0); for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { printf("%c", ans[i][j]); } printf("\n"); } } return 0;}
- Sicily 1162. Sudoku
- sicily 1162. Sudoku
- Sicily 1162 Sudoku
- sicily 1317. Sudoku
- Sicily 1317. Sudoku
- sicily 1317. Sudoku
- Sicily 1002. 数独游戏(sudoku)
- soj 1162. Sudoku[Special judge] 1317. Sudoku
- Sudoku
- sudoku
- Sudoku
- sudoku
- Sudoku
- Sudoku
- sudoku
- sudoku
- Sudoku
- Sudoku
- 杭电 acm Box of Bricks
- Android 蓝牙 BR/EDR 的关于串口通信的学习
- 回调函数透彻理解Java
- 杭电 acm Balloon Comes!
- 而且,她从不对他任何要求!在他面前非常乖巧非常的惹人喜欢!
- Sicily 1162. Sudoku
- hdoj-1875-畅通工程再续
- 顾倾尘喝了一口贝染泡的茶,在这个初夏的夜里,还是暖心暖肺!
- 杭电 acm AC Me
- 她将主动权全部交给他,她在他面前,四年如一日,依然是非常听话!
- 这个男人总是不动声色之间想要撕碎她伪装的面具,可是,贝染在心里从来没有恨过他提出离婚这件事情!
- Number Sequence
- Sicily 1024. Magic Island
- 只是,贝染这个女人……她很乖,乖到不用他操心!