洛谷 P1074 [NOIP2009 提高组] 靶形数独(DFS+剪枝)

题目链接:P1074 [NOIP2009 提高组] 靶形数独

题解:dfs+剪枝,行列间关系不好模拟处理,我们可以把所有为0的点(即待搜索放数的点)都存到一个数组s里面,第一维和第二维存下标,第三维存在第几宫的信息,用数字gong[i][j],表示第i宫是否存在数字j,hang[i][j]和lie[i][j]也一样,然后直接dfs从前往后搜索枚举数组s每一个放数字几通过数组gong,hang,lie就可以知道是否合法,记得更新和回溯状态,但是我们发现这样子并不能通过所有点,我们发现层数越多越容易TLE,而一行中填过的数字越多,需要填的数越少,dfs层数就越少,我们可以先予处理下数组s,我们记录每行有多少给待搜索的点,让待搜索点较少的行排在前面即可,参考自大佬 学无止境

题解:

#include<iostream>
#include<stack>
#include<list>
#include<set>
#include<vector>
#include<algorithm>
#include<math.h>
#include<numeric>
#include<map>
#include<cstring>
#include<queue>
#include<iomanip>
#include<cmath>
#include<queue>
#include <bitset>
#include<unordered_map>
	#ifndef local
	#define endl '\n'
#endif */
#define mkp make_pair
using namespace std;
using std::bitset;
typedef long long ll;
typedef long double ld;
const int inf=0x3f3f3f3f;
const ll MAXN=2e6+10;
const ll N=2e5+100;
const ll mod=1e9+7;
const ll hash_p1=1610612741;
const ll hash_p2=805306457;
const ll hash_p3=402653189;
//-----------------------------------------------------------------------------------------------------------------*/
// ll head[MAXN],net[MAXN],to[MAXN],edge[MAXN]/*流量*/,cost[MAXN]//费用;
/* 
void add(ll u,ll v,ll w,ll s){
	to[++cnt]=v;net[cnt]=head[u];edge[cnt]=w;cost[cnt]=s;head[u]=cnt;
	to[++cnt]=u;net[cnt]=head[v];edge[cnt]=0;cost[cnt]=-s;head[v]=cnt;
}
struct elemt{
	int p,v;
};
-----------------------------------
求[1,MAXN]组合式和逆元 
ll mi(ll a,ll b){
	ll res=1;
	while(b){
		if(b%2){
			res=res*a%mod;
		}	
		a=a*a%mod;
		b/=2;
	}
	return res;
}
ll fac[MAXN+10],inv[MAXN+10]
ll C(int m,int n){//组合式C(m,n); 
	if(!n){
		return 1;
	}
	return fac[m]*(inv[n]*inv[m-n]%mod)%mod;
}
fac[0]=1;inv[0]=1;
for(ll i=1;i<=MAXN;i++){
	fac[i]=(fac[i-1]*i)%mod;
	inv[i]=mi(fac[i],mod-2);
}
---------------------------------
 unordered_map<int,int>mp;
//优先队列默认小顶堆 , greater<int> --小顶堆  less<int> --大顶堆  
priority_queue<elemt,vector<elemt>,comp>q;
struct comp{
	public:
		bool operator()(elemt v1,elemt v2){
			return v1.v<v2.v;
		}
};
	set<int>::iterator it=st.begin();
*/
//emplace_back()  等于push_back(),但效率更高,传输pair时emplace_back(i,j)==push_back({i,j}) 
// vector<vector<int>>edge; 二维虚拟储存坐标 
//-----------------------------------------------------------------------------------------------------------------*/
  //map<int,bool>mp[N]; 
  //emplace_back()
 int which(int i,int j){
    if(i<=3)
    {
        if(j<=3)        return 1;
        else if(j<=6)   return 2;
        else            return 3;
    }
    else if(i<=6)
    {
        if(j<=3)        return 4;
        else if(j<=6)    return 5;
        else            return 6;
    }
    else
    {
        if(j<=3)        return 7;
        else if(j<=6)   return 8;
        else            return 9;
    }
}
int point(int i,int j){
    if(i==1||j==1||i==9||j==9)   return 6;
    if(i==2||j==2||i==8||j==8)   return 7;
    if(i==3||j==3||i==7||j==7)   return 8;
    if(i==4||j==4||i==6||j==6)   return 9;
    return 10;
}
int a[10][10];
struct line{
	int p,num;
};
bool comp(line v1,line v2){
	return v1.num<v2.num;
}
line l[10];//
int hang[10][10];//第i行有没有出现j
int lie[10][10];//同上(列)
int gong[10][10];//同上(宫)
int s[100][4];//存要填的数的x(0),y(1)坐标 ,所在宫(2)
int cnt=0;//要填的点的数量
int dfs(int x){//正在搜s[x]
	if(x>cnt){
		return 0;
	}
	int res=-inf;
	for(int i=1;i<=9;i++){//枚举s[x]放的数
		if(!gong[s[x][2]][i]&&!lie[s[x][1]][i]&&!hang[s[x][0]][i]){//判断能否填入i
			int tmp=i*point(s[x][0],s[x][1]);
			gong[s[x][2]][i]=lie[s[x][1]][i]=hang[s[x][0]][i]=1;//更新状态
			tmp+=dfs(x+1);
			res=max(res,tmp);
			gong[s[x][2]][i]=lie[s[x][1]][i]=hang[s[x][0]][i]=0;//回溯状态
		}
	}
	return res;//res<0时无解
}
int main(){
/*cout<<setiosflags(ios::fixed)<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(不含整数部分)*/
/*cout<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(含整数部分)*/
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//同步流
	ll res=0;//原本就有的值
	for(int i=1;i<=9;i++){
		l[i].p=i;
		for(int j=1;j<=9;j++){
			cin>>a[i][j];
			if(a[i][j]==0){
				l[i].num++;
			}
		}
	}
	sort(l+1,l+10,comp);//按num排序--剪枝,先搜0数量少的行
	for(int i=1;i<=9;i++){
		for(int j=1;j<=9;j++){
			int d=a[l[i].p][j];
			if(d==0){
				s[++cnt][0]=l[i].p;s[cnt][1]=j;s[cnt][2]=which(l[i].p,j);//按0数量少的行开始放入搜索区
			}
			else{
				res+=d*point(l[i].p,j);hang[l[i].p][d]++;lie[j][d]++;gong[which(l[i].p,j)][d]++;//原本分值
			}
		}
	}
	res+=dfs(1);
	if(res<0){
		cout<<-1<<endl;
	}
	else{
		cout<<res<<endl;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值