【搜索】靶形数独

倒着搜,卡一下时就能过。


/*							*\
 	By
 			Wu yihao

\*							*/


#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using std::max;

long map[15][15];
const long score[9][9] = {
{6,6,6,6,6,6,6,6,6},
{6,7,7,7,7,7,7,7,6},
{6,7,8,8,8,8,8,7,6},
{6,7,8,9,9,9,8,7,6},
{6,7,8,9,10,9,8,7,6},
{6,7,8,9,9,9,8,7,6},
{6,7,8,8,8,8,8,7,6},
{6,7,7,7,7,7,7,7,6},
{6,6,6,6,6,6,6,6,6}};

long calc()
{
	long cnt = 0;
	for (long i=0;i<9;i++)
		for (long j=0;j<9;j++)
			cnt += map[i][j]*score[i][j];
	return cnt;
}

long ans = -1;
bool h[15][15];
bool v[15][15];
bool b[15][15][15];
long tt = 0;

void dfs(long x,long y)
{
	// --------------------------------- crack on time
	tt ++;
	if (tt > 15000000)
	{
		printf("%ld",ans);
		exit(0);
	}

	if (x == -1)
	{
		long tmp = calc();
		ans = max(ans,tmp);
		return;
	}
	// --------------------------------- newx & newy
	long nx; long ny;
	if (y == 0)
	{ nx = x - 1; ny = 8; }
	else
	{ nx = x; ny = y-1; }
	

	// --------------------------------- filled ones
	if (map[x][y] > 0)
	{
		dfs(nx,ny);
		return;
	}

	// --------------------------------- filled it
	for (long i=1;i<10;i++)
	{
		if (!h[x][i] && !v[y][i] && !b[x/3][y/3][i])
		{
			h[x][i] = v[y][i] = b[x/3][y/3][i] = true;
			map[x][y] = i;
			dfs(nx,ny);
			map[x][y] = -1;
			h[x][i] = v[y][i] = b[x/3][y/3][i] = false;
		}
	}
}

int main()
{
	freopen("sudoku.in","r",stdin);
	freopen("sudoku.out","w",stdout);

	for (long i=0;i<9;i++)
	{
		for (long j=0;j<9;j++)
		{
			do map[i][j] = getchar() - '0';
			while (!isdigit(map[i][j]+'0'));
			if (map[i][j] == 0)
				map[i][j] = -1;
			else
			{
				h[i][map[i][j]] = 
				v[j][map[i][j]] = 
				b[i/3][j/3][map[i][j]] = true;
			}
		}
	}

	dfs(8,8);

	printf("%ld",ans);

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值