POJ 3076 Sudoku DLX coefficient alone

Source: Internet
Author: User

16*16 Sudoku, similar to the POJ 3074.

POJ 3076//sep9#include <cstdio> #include <cstdlib> #define Int_max 2147483647using namespace Std;const INT col_num=16*16*4;const int row_num=16*16*16+10; const int HEAD=0;CONST int Max=row_num*4+col_num+10;const int Delta[]={1,16*16+1,16*16*2+1,16*16*3+1};int cnt[col_num +10],st[col_num+10];int left[max],right[max],up[max],down[max];int row[max],col[max];int K,M;//k:node ' s idx M:row ' s Numberstruct ans{int r,c,k;} Ans[max];void init () {left[head]=col_num;right[head]=1;up[head]=down[head]=head;for (int i=1;i<=col_num;++i) { left[i]=i-1;right[i]= (i+1)% (col_num+1); up[i]=down[i]=i;cnt[i]=0;col[i]=i;row[i]=0;} m=0; K=col_num;} int make_col_head (int c) {++k;++cnt[c];col[k]=c;row[k]=m;left[k]=right[k]=k;up[k]=c;down[k]=down[c];up[down[k]]=k ;d Own[up[k]]=k;return K;} void Addcol (int ids,int c) {++k;++cnt[c];col[k]=c;row[k]=m;left[k]=ids;right[k]=right[ids];left[right[k]]=k;right[ Left[k]]=k;up[k]=c;down[k]=down[c];up[down[k]]=k;down[up[k]]=k;} void addrow (int i,int j,int k) {++m;ans[m].r=i;ans[M].c=j;ans[m].k=k;int Ids=make_col_head (16*i+j+delta[0]); Addcol (ids,16*i+k+delta[1]); Addcol (ids,16*j+k+delta[2 ]); Addcol (ids,16* (I/4*4+J/4) +k+delta[3]);} void remove (int c) {left[right[c]]=left[c];right[left[c]]=right[c];for (int i=down[c];i!=c;i=down[i]) for (int j=right [I];j!=i;j=right[j]) {up[down[j]]=up[j];d own[up[j]]=down[j];--cnt[col[j];}} void Resume (int c) {for (Int. I=up[c];i!=c;i=up[i]) for (int j=left[i];j!=i;j=left[j]) {down[up[j]]=j;up[down[j]]=j;++ CNT[COL[J]];} Left[right[c]]=c;right[left[c]]=c;} bool Dfs (int k) {if (right[head]==head) {char s[300];for (int i=0;i<k;++i) S[ans[st[i]].r*16+ans[st[i]].c]=ans[st[i] ].k+ ' A '; for (int i=0;i<16;++i) {for (int j=0;j<16;++j) Putchar (S[i*16+j]);p UTS (""); return true;} int s=int_max,c=0;for (int i=right[head];i!=head;i=right[i]) {if (cnt[i]<s) {s=cnt[i];c=i;}} Remove (c); for (int i=down[c];i!=c;i=down[i]) {st[k]=row[i];for (int j=right[i];j!=i;j=right[j]) remove (col[j]); DFS (k+1)) return true;for (int j=left[i];j!=i;j=left[j]) resume (col[j]);} Resume (c); RetuRN false;} int main () {char s[20];while (scanf ("%s", s) ==1) {init (); for (int. j=0;j<16;++j) if (s[j]== '-') {for (int k=0;k<16;++k ) AddRow (0,j,k);} Elseaddrow (0,j,s[j]-' A '), for (int i=1;i<16;++i) {scanf ("%s", s), and for (int. j=0;j<16;++j) if (s[j]== '-') {for (int k=0 ; k<16;++k) AddRow (i,j,k);} Elseaddrow (i,j,s[j]-' A ');} DFS (0);p UTS ("");} return 0;}



POJ 3076 Sudoku DLX coefficient alone

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.