【POJ】2676 Sudoku 【数独】精确覆盖

传送门:【POJ】2676 Sudoku 【数独】


题目分析:耶~第一道数独写粗来啦~~~~

数独转化为精确覆盖问题看这篇文章就好啦,我也不多费口舌了大笑

算法实践——舞蹈链(Dancing Links)算法求解数独


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define REV( i , a , b ) for ( int i = a - 1 ; i >= b ; -- i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define REC( i , A , o ) for ( int i = A[o] ; i != o ; i = A[i] )
#define CLR( a , x ) memset ( a , x , sizeof a )

const int MAXN = 325 ;
const int MAXM = 730 ;
const int MAXNODE = 3000 ;
const int INF = 0x3f3f3f3f ;

struct DLX {
	int U[MAXNODE] , D[MAXNODE] , L[MAXNODE] , R[MAXNODE] ;
	int row[MAXNODE] , col[MAXNODE] ;
	int S[MAXN] , H[MAXM] ;
	int deep , ans[MAXN] ;
	int n , m ;
	int size ;
	
	int G[10][10] ;
	
	void init () {
		CLR ( H , -1 ) ;
		FOR ( i , 0 , n ) {
			S[i] = 0 ;
			U[i] = i ;
			D[i] = i ;
			L[i] = i - 1 ;
			R[i] = i + 1 ;
		}
		L[0] = n ;
		R[n] = 0 ;
		size = n ;
	}
	
	void link ( int r , int c ) {
		++ size ;
		++ S[c] ;
		row[size] = r ;
		col[size] = c ;
		U[size] = U[c] ;
		D[size] = c ;
		D[U[c]] = size ;
		U[c] = size ;
		if ( ~H[r] ) {
			L[size] = L[H[r]] ;
			R[size] = H[r] ;
			L[R[size]] = size ;
			R[L[size]] = size ;
		}
		else
			H[r] = L[size] = R[size] = size ;
	}
	
	void remove ( int c ) {
		L[R[c]] = L[c] ;
		R[L[c]] = R[c] ;
		REC ( i , D , c )
			REC ( j , R , i ) {
				U[D[j]] = U[j] ;
				D[U[j]] = D[j] ;
				-- S[col[j]] ;
			}
	}
	
	void resume ( int c ) {
		REC ( i , U , c )
			REC ( j , L , i ) {
				++ S[col[j]] ;
				D[U[j]] = j ;
				U[D[j]] = j ;
			}
		R[L[c]] = c ;
		L[R[c]] = c ;
	}
	
	int dance ( int d ) {
		if ( R[0] == 0 ) {
			deep = d ;
			return 1 ;
		}
		int c = R[0] ;
		REC ( i , R , 0 )
			if ( S[c] > S[i] )
				c = i ;
		remove ( c ) ;
		REC ( i , D , c ) {
			ans[d] = row[i] ;
			REC ( j , R , i )
				remove ( col[j] ) ;
			if ( dance ( d + 1 ) )
				return 1 ;
			REC ( j , L , i )
				resume ( col[j] ) ;
		}
		resume ( c ) ;
		return 0 ;
	}
	
	void solve () {
		n = 9 * 9 * 4 ;
		init () ;
		REP ( i , 0 , 9 )
			REP ( j , 0 , 9 )
				scanf ( "%1d" , &G[i][j] ) ;
		REP ( i , 0 , 9 )
			REP ( j , 0 , 9 )
				if ( G[i][j] ) {
					int r = ( i * 9 + j ) * 9 + G[i][j] ;
					int c1 = i * 9 + j + 1 ;
					int c2 = 81 + i * 9 + G[i][j] ;
					int c3 = 162 + j * 9 + G[i][j] ;
					int c4 = 243 + ( ( i / 3 ) * 3 + ( j / 3 ) ) * 9 + G[i][j] ;
					link ( r , c1 ) ;
					link ( r , c2 ) ;
					link ( r , c3 ) ;
					link ( r , c4 ) ;
					S[c1] = S[c2] = S[c3] = S[c4] = -1 ;
				}
		REP ( i , 0 , 9 )
			REP ( j , 0 , 9 )
				if ( !G[i][j] )
					FOR ( k , 1 , 9 ) {
						int r = ( i * 9 + j ) * 9 + k ;
						int c1 = i * 9 + j + 1 ;
						int c2 = 81 + i * 9 + k ;
						int c3 = 162 + j * 9 + k ;
						int c4 = 243 + ( ( i / 3 ) * 3 + ( j / 3 ) ) * 9 + k ;
						if ( ~S[c1] && ~S[c2] && ~S[c3] && ~S[c4] ) {
							link ( r , c1 ) ;
							link ( r , c2 ) ;
							link ( r , c3 ) ;
							link ( r , c4 ) ;
						}
					}
		dance ( 0 ) ;
		REP ( i , 0 , deep ) {
			int key = ( ans[i] - 1 ) % 9 + 1 ;
			int x = ( ans[i] - 1 ) / 9 / 9 + 1 ;
			int y = ( ans[i] - 1 ) / 9 % 9 + 1 ;
			G[x][y] = key ;
		}
		FOR ( i , 1 , 9 ) {
			FOR ( j , 1 , 9 )
				printf ( "%d" , G[i][j] ) ;
			printf ( "\n" ) ;
		}
	}	
} dlx ;

int main () {
	int T ;
	scanf ( "%d" , &T ) ;
	while ( T -- )
		dlx.solve () ;
	return 0 ;
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值