• 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
  • 相关阅读:
    计算机毕设JAVA——学习考试管理系统(基于SpringBoot+Vue前后端分离的项目)
    ElasticSearch索引数据备份与恢复
    电容的电路应用
    小游戏在提升用户留存方面的作用
    html5期末大作业——HTML+CSS公益关爱残疾人( 6个页面)
    使用nacos实现简单的动态化线程池
    Uniapp 应用开机自启插件 Ba-Autoboot
    技术内幕 | StarRocks Pipeline 执行框架(下)
    蚓链助新零售企业快速实现数字化转型
    ZedGraph如何去掉外边框?并设置背景颜色
  • 原文地址:https://blog.csdn.net/hejx0412/article/details/126287587