NOIP2009 Target Sudoku (search)

Source: Internet
Author: User

Previously did a hdu similar Sudoku problem, the problem I did not add any optimization on the past, and then the problem is not optimized can be 50~80 points, to 80 points must be optimized

A bitwise operation can be used to save each row of each column each nine Gongne already used, for example, userow[i] = 011101111 means that line I and 1, 52 numbers are not used, then we enumerate each unknown lattice (i,j), he can not fill the number is userow[i]| USECOL[J]|USEBOX[I/3*3+J/3], in fact, this does not optimize what time, the following is the most NB optimization

We can count the known number of each line at the input, so we can sort the non-filled grid from small to large in each row, so that the search tree that the enumeration point can extend is smaller than the original.

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define RE (1<    &LT;9)-1) using namespace std;int sum_a[10];struct t{int x, y;        BOOL operator < (T t) const//known quantity more placed in front {if (sum_a[x] = = Sum_a[t.x]) return x < t.x;    return sum_a[x] > Sum_a[t.x];           }}unknown[100];//Save the position of the blank lattice int st[10][10];int p[10][10];//weight array void init () {for (int i = 0; i < 9; i++) {    P[0][i] = P[8][i] = p[i][0] = p[i][8] = 6;    } for (int i = 1; i < 8; i++) {p[1][i] = p[7][i] = p[i][1] = p[i][7] = 7;    } for (int i = 2; i < 7; i++) {p[2][i] = p[6][i] = p[i][2] = p[i][6] = 8;    } for (int i = 3; i < 6; i++) {p[3][i] = p[5][i] = p[i][3] = p[i][5] = 9; } P[4][4] = 10;} int ans = -1;int Cnt,sorce;int userow[10];//Each row uses the number of int usecol[10];//Each column uses the number of int usebox[10];//each nine to use the number of void Dfs (int cur        {if (cur > cnt) {ans = max (Sorce,ans);    Return  }  int x = unknown[cur].x;    int y = unknown[cur].y;    int temp = (USEROW[X]|USECOL[Y]|USEBOX[X/3*3+Y/3]); if (temp = = RE) return;//is not available for the number for (int i = 1; I <= 9; i++) if (! (        (Temp>> (i-1)) &1)) {St[x][y] = i;        USEBOX[X/3*3+Y/3] |= (1<< (i-1));        Userow[x] |= (1<< (i-1));        Usecol[y] |= (1<< (i-1));        Sorce + = St[x][y]*p[x][y];        DFS (CUR+1);        Sorce-= St[x][y]*p[x][y];        St[x][y] = 0;        USEBOX[X/3*3+Y/3] ^= (1<< (i-1));        Userow[x] ^= (1<< (i-1));    Usecol[y] ^= (1<< (i-1));    }}int Main () {//freopen ("sudoku.in", "R", stdin);    Freopen ("Sudoku.out", "w", stdout);    Init ();            for (int i = 0; i < 9; i++) {for (int j = 0; J < 9; J + +) {scanf ("%d", &st[i][j]);                if (st[i][j]! = 0) {Sorce + = St[i][j]*p[i][j];                Userow[i] |= (1<< (st[i][j]-1)); USECOL[J] |= (1<< (st[I][J]-1));                USEBOX[I/3*3+J/3] |= (1<< (st[i][j]-1));        ++sum_a[i];//Count I-line-known}}} for (int i = 0; i < 9; i++) for (int j = 0; J < 9; J + +)                {if (!st[i][j]) {++cnt;                unknown[cnt].x = i;            Unknown[cnt].y = j;    }} sort (unknown+1,unknown+cnt+1);    DFS (1); printf ("%d\n", ans);}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

NOIP2009 Target Sudoku (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.