Luogu p1074 target Sudoku [Search/pruning] By cellur925

Source: Internet
Author: User

Question Portal

It is obviously a search. However, there is no pruning at the beginning, and the brute force search starts from,NaturallyT has 6 points.

#include<cstdio>#include<algorithm>using namespace std;const int group[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,};const int sco[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 ans,num[50][50];bool gong[50][50],hang[50][50],lie[50][50];void review(){    int val=0;    for(int i=1;i<=9;i++)        for(int j=1;j<=9;j++)            val+=sco[i][j]*num[i][j];//  printf("%d\n",val);    ans=max(ans,val);}bool check(int x,int y,int w){    if(gong[group[x][y]][w]||hang[x][w]||lie[y][w]) return 0;    return 1;}void dfs(int x,int y){    if(x==10)    {        review();        return ;    }    int nx=x,ny=y+1;    if(ny==10) nx++,ny=1;    if(num[x][y]) dfs(nx,ny);    else    {        for(int i=1;i<=9;i++)        {            if(!check(x,y,i)) continue;            gong[group[x][y]][i]=1;            hang[x][i]=1;            lie[y][i]=1;            num[x][y]=i;            dfs(nx,ny);            gong[group[x][y]][i]=0;            hang[x][i]=0;            lie[y][i]=0;            num[x][y]=0;        }    }}int main(){    for(int i=1;i<=9;i++)        for(int j=1;j<=9;j++)        {            int x=0;            scanf("%d",&x);            if(!x) continue;            num[i][j]=x;            hang[i][x]=1;            lie[j][x]=1;            gong[group[i][j]][x]=1;        }    dfs(1,1);       printf("%d",ans==0 ? -1 : ans);    return 0;}

Consider pruning. From the perspective of human intelligenceAlthough I have never playedWe must start from a region with more options, because fewer decisions are available for us. So here we can also use this idea for reference. Every time we make statistics on the number of columns in each row, we have already filled in the number of records and obtained an optimal coordinate from which we can start searching. This algorithm ensures that we do not enter a number for each search, which makes the complexity much better.

# Include <cstdio> # include <algorithm> using namespace STD; const int group [10] [10] =,, 5, 5, 5, 6, 6, 0, 4, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 7, 8, 8, 9, 9, 7, 7, 8, 8, 8, 9, 9,}; const int SCO [10] [10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,, 6, 7, 8, 9, 9, 8, 7, 6, 9, 10, 9, 8, 7, 6, 7, 8, 9, 9, 8, 7, 6, 7, 8, 8, 8, 8, 8,, int CN, ANS, num [50] [50]; int cnt_hang [50], cnt_lie [50]; bool Gong [50] [50], hang [50] [50], lie [50] [50]; void Review () {int val = 0; For (INT I = 1; I <= 9; I ++) for (Int J = 1; j <= 9; j ++) val + = SCO [I] [J] * num [I] [J]; // printf ("% d \ n", Val); ans = max (ANS, val);} bool check (int x, int y, int W) {If (Gong [GRO Up [x] [Y] [W] | hang [x] [W] | lie [y] [W]) return 0; return 1 ;} void DFS (int x, int y, int CNT) {If (CNT = 81) {Review (); Return ;}for (INT I = 1; I <= 9; I ++) {If (! Check (x, y, I) continue; Gong [Group [x] [Y] [I] = 1; hang [x] [I] = 1; lie [y] [I] = 1; num [x] [Y] = I; cnt_hang [x] ++; cnt_lie [y] ++; int qwq =-1, qaq =-1, BX = 0, by = 0; For (Int J = 1; j <= 9; j ++) if (cnt_hang [J]> qwq & cnt_hang [J] <9) qwq = cnt_hang [J], BX = J; For (Int J = 1; j <= 9; j ++) if (cnt_lie [J]> Qaq &&(! Num [BX] [J]) Qaq = cnt_lie [J], by = J; DFS (BX, by, CNT + 1 ); gong [Group [x] [y] [I] = 0; hang [x] [I] = 0; lie [y] [I] = 0; num [x] [Y] = 0; cnt_hang [x] --; cnt_lie [y] -- ;}} int main () {for (INT I = 1; I <= 9; I ++) for (Int J = 1; j <= 9; j ++) {int x = 0; scanf ("% d ", & X); If (! X) continue; num [I] [J] = x; cnt_hang [I] ++; cnt_lie [J] ++; hang [I] [x] = 1; lie [J] [x] = 1; Gong [Group [I] [J] [x] = 1; CN ++;} int qwq =-1, qaq =-1, BX = 0, by = 0; For (INT I = 1; I <= 9; I ++) if (cnt_hang [I]> qwq & cnt_hang [I] <9) qwq = cnt_hang [I], BX = I; for (INT I = 1; I <= 9; I ++) if (cnt_lie [I]> Qaq &&(! Num [BX] [I]) // you can find a coordinate Qaq = cnt_lie [I], by = I; DFS (BX, by, CN) that is not filled in ); printf ("% d", ANS = 0? -1: ANS); Return 0 ;}
Warning

I started writing my own brute-force statement twice:

① No input/output (???) Sure enough, t is hopeless.

② Because I'm sure the palace and the value are both played out by the array table, and started to open the array very large, \ (50*50 \), however, the part of the table we typed is not a separate row and will be understood as a continuous segment after compilation. Therefore, we need to limit the array size so that we can just fill in the number.

Luogu p1074 target Sudoku [Search/pruning] By cellur925

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.