NOIP2009靶形数独——位运算优化DFS

题目在此

一道位运算优化DFS题目(大神们各种Dancing Link ORZ)

然而听说这题可以直接从右下角开始爆搜(YoungTree:强行把一道爆搜题做成位运算  我:Excuse me?)

其实和爆搜填数独的方法差不多,但是可以用位运算(即1和0)表示一个格子填了没有以及先填哪个格子、填什么数,大大简化了运算(毕竟不用循环找格子填)

一个小技巧:从空余格子最少的一行(或列——取决于你是逐行填还是逐列填)开始填

代码如下:

#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;

const int score[10][10]={{0,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 },
                         {0,6 ,6 ,6 ,6 ,6 ,6 ,6 ,6 ,6 },
						 {0,6 ,7 ,7 ,7 ,7 ,7 ,7 ,7 ,6 },
						 {0,6 ,7 ,8 ,8 ,8 ,8 ,8 ,7 ,6 },
						 {0,6 ,7 ,8 ,9 ,9 ,9 ,8 ,7 ,6 },
						 {0,6 ,7 ,8 ,9 ,10,9 ,8 ,7 ,6 },
						 {0,6 ,7 ,8 ,9 ,9 ,9 ,8 ,7 ,6 },
						 {0,6 ,7 ,8 ,8 ,8 ,8 ,8 ,7 ,6 },
						 {0,6 ,7 ,7 ,7 ,7 ,7 ,7 ,7 ,6 },
						 {0,6 ,6 ,6 ,6 ,6 ,6 ,6 ,6 ,6 }};
int emp[10],colu[10],rowu[10],coll[10],searchorder[10],map[10][10],latu[4][4];
int ans,i,j,p,sum,poss;
double log2(int a)
{
	return log(a)/log(2)+0.5;
}
int init()
{
	memset(colu,0,sizeof(colu));
	memset(rowu,0,sizeof(rowu));
	memset(emp,0,sizeof(emp));
	memset(coll,0,sizeof(coll));
	memset(searchorder,0,sizeof(searchorder));
	memset(map,0,sizeof(map));
	memset(latu,0,sizeof(latu));
	sum=0;
	int i,j;
	for (i=1;i<=9;i++)
	  for(j=1;j<=9;j++)
	  {
	  	scanf("%d",&map[i][j]);
	  	if (map[i][j])
	  	{
	  		sum+=map[i][j]*score[i][j];
	  		emp[i]|=1<<(j-1);
	  		p=1<<(map[i][j]-1);
	  		if ((colu[i]&p)||(rowu[j]&p)||(latu[(i-1)/3][(j-1)/3]&p))//已经出现重复就不用填了 
	  		{
	  			printf("-1");
	  			return 0;
	  		}
	  		colu[i]|=p;
	  		rowu[j]|=p;
	  		latu[(i-1)/3][(j-1)/3]|=p;
	  	}
	  	else
	  	coll[i]++;
	  }
	for (i=1;i<=9;i++)
	searchorder[i]=i;//搜索顺序:从已填数多的一行开始填 
	for (i=1;i<=8;i++)
	for (j=i+1;j<=9;j++)
	if (coll[searchorder[i]]>coll[searchorder[j]])
	{
		searchorder[0]=searchorder[i];
		searchorder[i]=searchorder[j];
		searchorder[j]=searchorder[0];
	}
	for(i=1;i<=9;i++)
	if (coll[searchorder[i]]) break;
	return i;
}
void dfs(int k,int sum)
{
	if (k==10) ans=max(ans,sum);
	else
	{
		int x,first,row,able,pos,col;
		col=searchorder[k];
		x=511-emp[col];
		first=x&(-x);
		emp[col]|=first;
		row=(int)log2(first)+1;//找格子 
	    pos=511-(colu[col]|rowu[row]|latu[(col-1)/3][(row-1)/3]);
	    while (pos)//填格子 
	    {
	    	able=pos&(-pos);
	    	pos-=able;
	    	map[col][row]=(int)log2(able)+1;
	    	colu[col]|=able;
	    	rowu[row]|=able;
	    	latu[(col-1)/3][(row-1)/3]|=able;
	    	if (x==first)
	    	dfs(k+1,sum+map[col][row]*score[col][row]);
	    	else
	    	dfs(k,sum+map[col][row]*score[col][row]);
			colu[col]-=able;
	    	rowu[row]-=able;
	    	latu[(col-1)/3][(row-1)/3]-=able;
	    }
	    emp[col]-=first;
	}
}
int main()
{
	ans=0;
	poss=init();
	if (!poss)
	{
		printf("-1");
		return 0;
	}
	dfs(poss,sum);
	if (!ans)
	printf("-1");
	else
	printf("%d",ans);
	return 0;
} 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值