Target-shaped Sudoku [greedy + Deep Search]

Source: Internet
Author: User

Problem description
Xiaocheng and Xiaohua are both good students who love mathematics. Recently, they fell in love with the sudoku game, and they wanted to use the sudoku for a better match. But the common Sudoku is too simple for them, so they asked Dr. Z for advice, and Dr. Z took out the "target-shaped Sudoku" he recently invented ", as the question of the two children comparison.
The square of the target-shaped Sudoku is the same as that of the ordinary Sudoku. There are 9 small jiugong grids (separated by thick black lines) in the 9-grid wide × 9-grid-tall big jiugongge ). Some numbers are known in the big nine cells. Based on these numbers, use logical reasoning to enter numbers ranging from 1 to 9 in other spaces. Each number cannot appear in each small cell, and each number cannot appear in each row or column. However, the target-type Sudoku is different from the ordinary Sudoku, that is, each square has a score, and, like a target, the closer the distance from the center, the higher the score. ()

 

The specific score distribution is as follows: the innermost frame (yellow area) is 10 points, and a circle (Red Area) outside the yellow area is 9 points for each grid, in a circle (blue area), each grid is 8 points. In a circle (brown area) outside the blue area, each grid is 7 points, and the outermost area is a circle (white area) each grid has 6 points, as shown in. The requirement of the competition is: each person must complete a given Sudoku (each given Sudoku may have a different filling method) and strive for a higher total score. The total score is the sum of the score on each square and the product of the number entered on the corresponding lattice when the Sudoku is completed ., In the following target Sudoku game, the total score is 2829. The game rules determine the outcome based on the total score.

 

Thanks to the competition, xiaocheng found you who are good at programming and asked you to help him find the highest score for the given target-type Sudoku.
Input Format
There are 9 rows in total, and each row has 9 integers (each number is within the range of 0-9), indicating an unfilled Sudoku Square. The unfilled space is represented by "0. Each number is separated by a space.
Output Format
Output the highest score of the target-type Sudoku. If the number is independent, an integer-1 is output.
Sample Input
[Example 1]
7 0 0 9 0 0 0 1
1 0 0 0 0 5 9 0 0
0 0 0 2 0 0 8 0
0 0 5 0 2 0 0 3
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

[Example 2]
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 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 1
0 0 0 0 0 0 0 6
Sample output
[Output Example 1]
2829
[Output Example 2]
2852

To answer this question, learn Data independence first

Code
#include<stdio.h>#include<cstdlib>#include<algorithm>using namespace std;struct node {    int id,sum;}sud[10]; bool operator<(const node &x,const node &y) {    return x.sum>y.sum;}int mp[10][10];bool line[10][10],list[10][10],gon[10][10];int score[]={6,7,8,9,10};int ans,sum;int mark(int x,int y) {    return score[min(min(x,8-x),min(y,8-y))];}int Get(int x,int y) {    if(x<3) {        if(y<3) return 1;        else if(y<6) return 2;        else return 3;     }    else if(x<6) {        if(y<3) return 4;        else if(y<6) return 5;        else return 6;    }    else if(x<9) {        if(y<3) return 7;        else if(y<6) return 8;        else return 9;    }}void dfs(int x,int y,int k){    if(k>=9) {        ans=max(ans,sum);        return ;    }    if(y>=9) dfs(sud[k+1].id,0,k+1);    if(mp[x][y]>0) dfs(x,y+1,k);    else     {        for(int i=1;i<=9;++i)        {            int wh=Get(x,y);            if(!line[x][i] && !list[y][i] && !gon[wh][i])            {                line[x][i]=list[y][i]=gon[wh][i]=1;                mp[x][y]=i;                sum+=i*mark(x,y);                dfs(x,y+1,k);                line[x][i]=list[y][i]=gon[wh][i]=0;                mp[x][y]=0;                sum-=i*mark(x,y);            }            }    }}int main(){    for(int i=0 ; i<9 ; ++i)    {        sud[i].id=i;        for(int j=0 ; j<9 ; ++j) {            int x;            scanf("%d",&x);            if(x) {                sud[i].sum++;                mp[i][j]=x;                sum+=x*mark(i,j);                line[i][x]=1;                list[j][x]=1;                    gon[Get(i,j)][x]=1;            }        }    }    sort(sud,sud+9);    dfs(sud[0].id,0,0);    if(ans) printf("%d",ans);    else printf("-1");    return 0;}

 

Target-shaped Sudoku [greedy + Deep Search]

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.