P1074 [NOIP2009 提高组] 靶形数独

https://www.luogu.com.cn/problem/P1074

-> 八皇后 ->判断 位置 -》hang lie gong
->已知可以算 tmp_have

->==0 ->记录点的信息 s[i][0--3] 可以预处理->cnt

-》dfs -》优化 -》从少的dfs -》记录rou[i].sum,rou[i].rank

/*
7 0 0 9 0 0 0 0 1 
1 0 0 0 0 5 9 0 0 
0 0 0 2 0 0 0 8 0 
0 0 5 0 2 0 0 0 3 
0 0 0 0 0 0 6 4 8 
4 1 3 0 0 0 0 0 0 
0 0 7 0 0 2 0 9 0 
2 0 1 0 6 0 8 0 4 
0 8 0 5 0 4 0 1 2

2829

0 0 0 7 0 2 4 5 3 
9 0 0 0 0 8 0 0 0 
7 4 0 0 0 5 0 1 0 
1 9 5 0 8 0 0 0 0 
0 7 0 0 0 0 0 2 5 
0 3 0 5 7 9 1 0 8 
0 0 0 6 0 1 0 0 0 
0 6 0 9 0 0 0 0 1 
0 0 0 0 0 0 0 0 6

2852
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------debug\n");
using namespace std;

int a[11][11];
int hang[11][11],lie[11][11],gong[11][11];
int s[100][10],cnt,ans=-1;
struct node
{
    int rank,sum;
    bool operator< (const node &a)const{
        return sum<a.sum;
    }
}rou[11];

int which(int x,int y)
{
    if(x<=3){
        if(y<=3) return 1;
        if(y<=6) return 2;
        if(y<=9) return 3;
    }
    if(x<=6){
        if(y<=3) return 4;
        if(y<=6) return 5;
        if(y<=9) return 6;
    }
    if(x<=9){
        if(y<=3) return 7;
        if(y<=6) return 8;
        if(y<=9) return 9;
    }
}
int point(int x,int y)
{
    if(x==1||x==9||y==1||y==9) return 6;
    if(x==2||x==8||y==2||y==8) return 7;
    if(x==3||x==7||y==3||y==7) return 8;
    if(x==4||x==6||y==4||y==6) return 9;
    return 10;  
}
void dfs(int dep,int score)
{
    if(dep==cnt+1){
        ans=max(score,ans);
        return;
    }
    for(int i=1;i<=9;i++)
    {
        if(hang[ s[dep][0] ][i]==0&&lie[ s[dep][1] ][i]==0&&gong[ s[dep][3] ][i]==0)
        {
            hang[ s[dep][0] ][i]=lie[ s[dep][1] ][i]=gong[ s[dep][3] ][i]=1;
            dfs(dep+1,score+s[dep][2]*i);
            hang[ s[dep][0] ][i]=lie[ s[dep][1] ][i]=gong[ s[dep][3] ][i]=0;
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    
    int tmp_have=0;    for(int i=1;i<=9;i++) rou[i].rank=i;
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=9;j++)
        {
            cin>>a[i][j];
            if(a[i][j]>0)
            {
                hang[i][a[i][j]]=lie[j][a[i][j]]=gong[which(i,j)][a[i][j]]=1; 
                tmp_have+=point(i,j)*a[i][j];
            }
            else
            {
                //s[++cnt][0]=i,s[cnt][1]=j,s[cnt][2]=point(i,j),s[cnt][3]=gong(i,j);
                rou[i].sum++;
            }
        }
    }
    sort(rou+1,rou+1+9);
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=9;j++)
        {
            if(a[rou[i].rank][j]==0)
            {
                s[++cnt][0]=rou[i].rank,s[cnt][1]=j,s[cnt][2]=point(rou[i].rank,j),s[cnt][3]=which(rou[i].rank,j);
            }
        }
    }
    dfs(1,tmp_have);
    
    
    cout<<ans<<endl;
    
    
    
    return 0;
}
View Code

 

posted @ 2023-07-09 14:58  JMXZ  阅读(14)  评论(0)    收藏  举报