搜索-洛谷P1074 靶形数独

https://daniu.luogu.org/problem/show?pid=1074
还真是启发式搜索,这个不是A*,纯粹的启发式搜索;
我钞蹄姐的;
……………….
基本上就是把题解翻译了一遍,理所当然1A;
想起当年的斗地主,基本上就是抄的;
唉,繁琐的暴搜题目我还是太萎啊;
题解:
https://daniu.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P1074

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#define Ll long long
using namespace std;
int v[10][10]{
{0,0,0,0,0,0,0,0,0,0},
{0,6,6,6,6,6,6,6,6,6},
{0,6,7,7,7,7,7,7,7,6},
{0,6,7,8,8,8,8,8,7,6},
{0,6,7,8,9,9,9,8,7,6},
{0,6,7,8,9,10,9,8,7,6},
{0,6,7,8,9,9,9,8,7,6},
{0,6,7,8,8,8,8,8,7,6},
{0,6,7,7,7,7,7,7,7,6},
{0,6,6,6,6,6,6,6,6,6},
};
int g[10][10]{
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,2,2,2,3,3,3},
{0,1,1,1,2,2,2,3,3,3},
{0,1,1,1,2,2,2,3,3,3},
{0,4,4,4,5,5,5,6,6,6},
{0,4,4,4,5,5,5,6,6,6},
{0,4,4,4,5,5,5,6,6,6},
{0,7,7,7,8,8,8,9,9,9},
{0,7,7,7,8,8,8,9,9,9},
{0,7,7,7,8,8,8,9,9,9},
};
bool h[10][10],l[10][10],G[10][10],c[90];
int map[10][10],x[90],y[90];
int n,ans;
void find(){
    int sum=0;
    for(int i=1;i<=9;i++)
    for(int j=1;j<=9;j++)
    sum+=map[i][j]*v[i][j];
    ans=max(ans,sum);
}
void dfs(int k){
    if(k>n){find();return;}
    int w=1e9,p;
    for(int i=1;i<=n;i++)
    if(!c[i]){
        int ww=0;
        for(int j=1;j<=9;j++)
            if(!h[x[i]][j]&&!l[y[i]][j]&&!G[g[x[i]][y[i]]][j])
                if(++ww==w)break;
        if(ww<w){w=ww;p=i;}
    }
    int xx=x[p],yy=y[p];
    c[p]=1;
    for(int i=1;i<=9;i++)
        if(!h[xx][i]&&!l[yy][i]&&!G[g[xx][yy]][i]){
            map[xx][yy]=i;
            h[xx][i]=1;
            l[yy][i]=1;
            G[g[xx][yy]][i]=1;
            dfs(k+1);
            h[xx][i]=0;
            l[yy][i]=0;
            G[g[xx][yy]][i]=0;
        }
    c[p]=0;
}
int main()
{
    for(int i=1;i<=9;i++)
    for(int j=1;j<=9;j++){
        scanf("%d",&map[i][j]);
        if(map[i][j]){
            h[i][map[i][j]]=1;
            l[j][map[i][j]]=1;
            G[g[i][j]][map[i][j]]=1;
        }else{
            x[++n]=i;
            y[n]=j;
        }
    }
    ans=-1;
    dfs(1);
    printf("%d",ans);
}
posted @ 2017-04-01 14:15  largecube233  阅读(197)  评论(0编辑  收藏  举报