Sudoku POJ - 2676 状压剪枝

22 篇文章 0 订阅

一、内容

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

二、思路

  • 创建3个数组c,r,grid分别表示每行每列每个小宫格的二进制状态,一共有9个数字所以有9位,该位上为1代表这个位置可以填数字,为0代表不可以填数字。
  • 用num数组记录每个二进制状态上面有多少个1,help数组记录某个状态下1所在第几位如000001000这个状态1所在的位置是3,那么它代表的数字就是4(因为是从右边开始,第一个是1)。
  • 在进行dfs搜索的时候,首先找出某个位置的状态,该状态的1的数量最少,那么可以填的数字自然就是最少的了,从最开始就减少了搜索的数量, 那么可以搜索的分支自然就减少了。
  • 然后再从该状态出发,如100100010这个状态可以填的数字就是3个,分别是2,6,9,就只需要搜索3个状态即可。
  • lowbit(): lowbit是取得二进制上的最后一个1, lowbit(x) = x & -x。 这个不懂的就百度下。

三、代码

#include <cstdio>
const int N =  9;
char g[10][10];
int c[N], r[N], grid[N], num[1 << N], t, help[1 << N]; //num记录当前状态1的个数 
void init() {
 	for (int i = 0; i < N; i++) {
 		c[i] = r[i] = grid[i] = (1 << N) - 1;	
	} 
}
int get(int x, int y) {
	//代表3个状态都可以 
	return c[y] & r[x] & grid[x / 3 * 3 + y / 3];
}
bool dfs(int cnt) {
	if (cnt == 0) {
		//代表没有可以填的了 直接输出
		for (int i = 0; i < N; i++) {
			printf("%s\n", g[i]);
		}
		return true; 
	}
	//找出1最少的位置
	int min = 81;
	int x, y; //记录位置 
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (g[i][j] == '0') {
				int t = num[get(i, j)];
				if (t < min) {
					x = i, y = j;
					min = t;
				} 
			}
		}
	} 
	//从这个位置进行出发
	for (int i = get(x, y); i; i -= i & -i) {
		 int t = i & -i;
		 //从是1的位置开始枚举
		 g[x][y] = help[t] + '1'; 
		 r[x] -= t;
		 c[y] -= t;
		 grid[x / 3 * 3 + y / 3] -= t;
		 if (dfs(cnt - 1)) return true;
		 //回溯 
		 g[x][y] = '0';
		 r[x] += t;
		 c[y] += t;
		 grid[x / 3 * 3 + y / 3] += t;		 
	} 
	return false;
}
int main() {
	//初始化num 数组 
	for (int i = 0; i < 1 << N; i++) {
		for (int j = i; j; j -= j & -j) {
			num[i]++; 
		}
	} 
	//初始化help数组 记录当前1是第个几位置上的1 
	for (int i = 0; i < N; i++) {
		help[1 << i] = i;
	}
 	scanf("%d", &t);
 	while (t--) {
	 	//初始化c,r,grid数组
		init(); 
		for (int i = 0; i < N; i++) {
	 		scanf("%s", g[i]);
		}
		int cnt = 81;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (g[i][j] != '0') {
					//将此二进制位上标记位0
					int t = 1 << (g[i][j] - '1');
					c[j] -= t;
					r[i] -= t;
					grid[i / 3 * 3 + j / 3] -= t;
					cnt--; //代表可填的位置 
				}
			}
		}
		dfs(cnt);
	}
	return 0;
} 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值