NOIP2009 靶形数独

爆搜没什么好说的。。。

剪枝思路:

一开始将每个点可能取的值的数量统计出,排序,从小到大搜

然后贪心可行性(就是剩下的地方都填9,得分10)


不过在vj上测85。。。日。。加了卡时,2E7次之内跳出,总算过了。。。


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;

const int maxn = 10;
const int dx[8] = {0,0,1,1,1,-1,-1,-1};
const int dy[8] = {1,-1,1,0,-1,1,0,-1};

int ans,cnt,sc[maxn][maxn],p[maxn][maxn],a[maxn][maxn][maxn],tot[maxn][maxn];
int SBVIJOS = 0;

struct P{
	int x,y;
	bool operator < (const P &b) const {return tot[x][y] < tot[b.x][b.y];}
}b[maxn*maxn];

queue <P> q;

void Modify(int x,int y,int now,int Add)
{
	int l1,l2,r1,r2;
	if (x <= 3) l1 = 1,l2 = 3;
	else if (x <= 6) l1 = 4,l2 = 6;
	else l1 = 7,l2 = 9;
	if (y <= 3) r1 = 1,r2 = 3;
	else if (y <= 6) r1 = 4,r2 = 6;
	else r1 = 7,r2 = 9;
	for (int i = l1; i <= l2; i++)
		for (int j = r1; j <= r2; j++)
			a[i][j][now] += Add;
	for (int i = 1; i <= 9; i++) a[x][i][now] += Add,a[i][y][now] += Add;
}

void dfs(int now,int sum)
{
	++SBVIJOS;
	if (SBVIJOS >= 2E7) return;
	if (sum + (cnt - now)*90 <= ans) return;
	if (now == cnt) {ans = sum; return;}
	int x = b[now].x,y = b[now].y;
	for (int i = 1; i <= 9; i++)
		if (!a[x][y][i]) {
			Modify(x,y,i,1);
			dfs(now + 1,sum + i*sc[x][y]);
			Modify(x,y,i,-1);
		}
}

int main()
{
	#ifdef YZY
		   freopen("yzy.txt","r",stdin);
	#endif
	
	sc[5][5] = 10; q.push((P){5,5});
	while (!q.empty()) {
		P k = q.front(); q.pop();
		for (int i = 0; i < 8; i++) {
			int xx = k.x + dx[i];
			int yy = k.y + dy[i];
			if (sc[xx][yy] || xx < 1 || yy < 1 || xx > 9 || yy > 9) continue;
			sc[xx][yy] = sc[k.x][k.y] - 1;
			q.push((P){xx,yy});
		}
	}
	
	for (int i = 1; i <= 9; i++)
		for (int j = 1; j <= 9; j++) {
			scanf("%d",&p[i][j]);
			if (p[i][j]) Modify(i,j,p[i][j],1),ans += p[i][j]*sc[i][j];
		}
		
	for (int i = 1; i <= 9; i++)
		for (int j = 1; j <= 9; j++)
			if (!p[i][j]) {
				b[cnt++] = (P){i,j};
				for (int l = 1; l <= 9; l++)
					if (!a[i][j][l]) ++tot[i][j];
			}
	int orz = ans;
	sort(b,b + cnt);
	dfs(0,ans);
	if (ans > orz) cout << ans;
	else cout << -1;
	
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值