• POJ2676数独游戏题解


    第三次才AC我好菜

    一道“简单”的问题。

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Sample Output

    143628579
    572139468
    986754231
    391542786
    468917352
    725863914
    237481695
    619275843
    854396127
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    简单来讲就是给T个数独给我们,让我们求解。

    思路

    根其他深搜一样还是要有变量来存每一行,每一列,每一宫(每一个大格)
    是否有数字一到九。
    剩下的代码就很好写,只是长了一点。
    最后附上求宫公式,点想(x,y)在第:

    3 * ((i - 1) / 3) + (j - 1) / 3 + 1

    宫。
    在这里插入图片描述

    AC代码

    #include 
    #include 
    
    using namespace std;
    
    int a[15][15];
    int row[100][100], col[100][100], gird[100][100];
    
    bool dfs(int x, int y) {
    	if (x == 10) return 1;
    	bool flag = 0;
    	cout << 1; //防抄袭
    	if (a[x][y]) {
    		if (y == 9)  {
    			flag = dfs(x + 1, 1);
    		} else {
    			flag = dfs(x, y + 1);
    		}
    		return flag?1:0;
    	} else {
    		int k = 3 * ((x - 1) / 3) + 1 + ((y - 1) / 3); 
    		for (int i = 1; i <= 9; i++) {
    			if (!row[x][i] && !col[y][i] && !gird[k][i]) {
    				a[x][y] = i;
    				row[x][i] = 1;
    				col[y][i] = 1;
    				gird[k][i] = 1;
    				if (y == 9) {
    					flag = dfs(x + 1, 1);
    				} else {
    					flag = dfs(x, y + 1);
    				}
    				if (!flag) {
    					a[x][y] = 0;
    					row[x][i] = 0;
    					col[y][i] = 0;
    					gird[k][i] = 0;
    				} else return 1; 
    			}
    		}
    	}
    	return 0;
    }
    
    int main() {
    	int T;
    	cin >> T;
    	while (T--) {
    		memset(row, 0, sizeof(row));
    		memset(col, 0, sizeof(col));
    		memset(gird, 0, sizeof(gird));
    		for (int i = 1; i <= 9; i++) {
    			for (int j = 1; j <= 9; j++) {
    				char tmp;
    				cin >> tmp;  //用scanf("%1d", &a[i][j]) 也可以(邪门方法 )
    				a[i][j] = tmp - '0';
    				if (a[i][j]) {
    					int k = 3 * ((i - 1) / 3) + (j - 1) / 3 + 1;
    					row[i][a[i][j]] = 1;
    					col[j][a[i][j]] = 1;
    					gird[k][a[i][j]] = 1;
    				}
    			}
    		}
    		dfs(1, 1);
    		for (int i = 1; i <= 9; i++) {
    			for (int j = 1; j <= 9; j++) {
    				cout << a[i][j];
    			}
    			cout << endl;
    		}
    		cout << endl;
    	}
    	return 0; //华丽结束
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
  • 相关阅读:
    面向对象的三大特性之多态
    C++桌面应用开发(Qt学习)——对话框(2)常用标准对话框
    java double类型 向上取整,向下取整,四舍五入
    Linux下IIC子系统和触摸屏驱动
    Cys(Npys)-(Arg)₉,H2N-C(Npys)-RRRRRRRRR-OH
    【ACM】简单题(4)
    力扣336.回文对 ——字符串哈希
    K8s网络存储,NFS,PV,PVC,StorageClass等详解
    方法参数调用-两种情况
    【头歌实验】四、Python分支结构
  • 原文地址:https://blog.csdn.net/hejx0412/article/details/126287587