码迷,mamicode.com
首页 > 其他好文 > 详细

NOIP2009 靶形数独

时间:2016-05-11 07:27:39      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

爆搜没什么好说的。。。

剪枝思路:

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

然后贪心可行性(就是剩下的地方都填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;
}


NOIP2009 靶形数独

标签:

原文地址:http://blog.csdn.net/crzbulabula/article/details/51367965

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!