记录编号 174052 评测结果 AAAAAAAAAAAAAAAAAATA
题目名称 [NOIP 2009]靶形数独 最终得分 95
用户昵称 Gravatardevil 是否通过 未通过
代码语言 C++ 运行时间 2.527 s
提交时间 2015-07-31 10:09:31 内存使用 0.32 MiB
显示代码纯文本
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <ctime>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const int maxn=2000010;
const int maxm=90010;
const int MAX_INT=0x7fffffff;
const int max_int=1061109567;
const int mod=12345;

int sorce[15][15]={
    {6,6,6,6, 6,6,6,6,6},
    {6,7,7,7, 7,7,7,7,6},
    {6,7,8,8, 8,8,8,7,6},
    {6,7,8,9, 9,9,8,7,6},
    {6,7,8,9,10,9,8,7,6},
    {6,7,8,9, 9,9,8,7,6},
    {6,7,8,8, 8,8,8,7,6},
    {6,7,7,7, 7,7,7,7,6},
    {6,6,6,6, 6,6,6,6,6}
};

int num[15][15];int ans=0;
bool vis_x[15][10],vis_y[15][10],vis_e[10][10];

void check_point()
{
    int s=0;
    for(int i=0;i<9;i++)
    {
        for(int j=0;j<9;j++)
          s+=num[i][j]*sorce[i][j];
    }
    if(s>ans) ans=s;
}

void dfs(int x,int y)
{
    if(y==-1) {x--;y=8;}
    if(x==-1) {check_point();return;}
    
    if(num[x][y]!=0) dfs(x,y-1);
    else
    {
        int t=x/3*3+y/3;
        for(int i=1;i<=9;i++)
        {
            if(!vis_x[x][i]&&!vis_y[y][i]&&!vis_e[t][i])
            {
                vis_x[x][i]=vis_y[y][i]=vis_e[t][i]=true;
                num[x][y]=i;
                dfs(x,y-1);
                vis_x[x][i]=vis_y[y][i]=vis_e[t][i]=false;
                num[x][y]=0;
            }
        }
    }
}

int main()
{
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    freopen("sudoku.in","r",stdin);
    freopen("sudoku.out","w",stdout);
    clock_t st=clock();
    for(int i=0;i<9;i++)
    {
        for(int j=0;j<9;j++)
        {
            scanf("%d",&num[i][j]);
            vis_x[i][num[i][j]]=true;
            vis_y[j][num[i][j]]=true;
            vis_e[i/3*3+j/3][num[i][j]]=true;
        }
    }
    dfs(8,8);
    if(ans==0) printf("-1\n");
    else printf("%d\n",ans);
    clock_t et=clock();
    //printf("\nTime used : %.4lf Ms\n",double(et-st)/CLOCKS_PER_SEC);
    return 0;
}