CodeChef submission 129840 (C++ 4.0.0-8) plaintext list. Status: TLE, problem J1, contest NOV09. By akhilravidas (Akhil Ravidas), 2009-11-11 00:03:43.
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <string> #include <cstdio> #include <cctype> #include <vector> #include <cassert> #include <iomanip> #include <string.h> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; typedef pair<int,int> PI; #define _DEBUG 1 #define DEBUG if(_DEBUG) #define dbg(x) DEBUG{cerr << #x << " -> " << (x) << "\t";} #define dbge(x) DEBUG{cerr << #x << " -> " << (x) << endl;} #define CLR(X) memset(X,0,sizeof(X)) int Igrid[9][9]; // Encoding / Decoding functions inline int Encode(int N, int r, int c) { return N*81 + r*9 + c; } inline int GetNum(int N) { return N/81; } inline int GetRow(int N) { return (N/9)%9; } inline int GetCol(int N) { return N%9; } inline int GetSq(int N) { return ((GetRow(N)/3)*3) + (GetCol(N)/3); } inline int GetGridpt(int N) { return GetRow(N)*9 + GetCol(N); } inline int GetNumRo(int N) { return GetNum(N)*9 + GetRow(N); } inline int GetNumCol(int N) { return GetNum(N)*9 + GetCol(N); } inline int GetNumSq(int N) { return GetNum(N)*9 + GetSq(N); } namespace ExactCover { const int inf = 1<<30,MX=1024; struct Node { Node * Left , *Right , *Up , *Down ; Node * Column; int Size; int ID;// RowNo string DBG; int RR , CC; } Root , *RootPtr = &Root, Matrix[MX][MX] , *RoStart[MX]; void Cover( Node * Col ) { Col -> Right -> Left = Col -> Left; Col -> Left -> Right = Col -> Right; for ( Node * nextRo = Col -> Down;nextRo != Col ; nextRo = nextRo -> Down ) { for ( Node * nxtRight = nextRo -> Right; nxtRight != nextRo; nxtRight = nxtRight -> Right ) { nxtRight -> Down -> Up = nxtRight -> Up; nxtRight -> Up -> Down = nxtRight -> Down; nxtRight -> Column -> Size --; } } } void UnCover( Node * Col ) { for ( Node * nextRo = Col -> Up;nextRo != Col ; nextRo = nextRo -> Up ) { for ( Node * nxtRight = nextRo -> Left; nxtRight != nextRo; nxtRight = nxtRight -> Left ) { nxtRight -> Column -> Size ++; nxtRight -> Down -> Up = nxtRight; nxtRight -> Up -> Down = nxtRight; } } Col -> Right -> Left = Col; Col -> Left -> Right = Col; } int depth,Stk[MX],Top; bool Found; int R,C,Filled;// Common for all bool Input[MX][MX]; void Save() { //dbg(Top); int s; for ( int i=0;i<Top;i++ ) {s=Stk[i];Igrid[GetRow(s)][GetCol(s)] = GetNum(s)+1;}//dbge(Stk[i]); } void Search () { // dbge(depth); if ( RootPtr -> Right == RootPtr || depth == 81 - Filled ) { Save(); Found = true; return; } int best = inf; Node * nextCol=RootPtr , *bestCol ; while(1) { nextCol = nextCol -> Right ; if ( nextCol == RootPtr ) break; if ( best > nextCol-> Size ) best = nextCol -> Size , bestCol = nextCol; } Cover ( bestCol ); for ( Node * nextRo = bestCol -> Down ; nextRo != bestCol; nextRo = nextRo -> Down ) { Stk[Top++] = nextRo -> ID; for ( Node * nxtRight = nextRo -> Right; nxtRight != nextRo; nxtRight = nxtRight -> Right ) Cover ( nxtRight -> Column ); depth ++; Search(); depth --; if ( Found ) return; for ( Node * nxtRight = nextRo -> Left; nxtRight != nextRo; nxtRight = nxtRight -> Left ) UnCover ( nxtRight -> Column ); Top --; } UnCover ( bestCol ); } int nextCol ( int n ){ return n<C-1?n+1:0;} int nextRo( int n ) { return n<R-1?n+1:0;} int prevCol ( int n ) { return n?n-1:C-1;} int prevRo ( int n ) { return n?n-1:R-1;} void Construct() { int r,c; for ( int i=0;i<R;i++ ) for ( int j=0;j<C;j++ ) if ( Input[i][j] ) { Matrix[i][j].RR = i , Matrix[i][j].CC = j; // Construct a node for i,j r=i,c=j; do { c=nextCol(c); } while ( Input[i][c] == 0 ); Matrix[i][j].Right = &Matrix[i][c]; r=i,c=j; do { c=prevCol(c); } while ( Input[i][c] == 0 ); Matrix[i][j].Left = &Matrix[i][c]; r=i,c=j; do { r=nextRo(r); } while ( Input[r][j] == 0 ); Matrix[i][j].Down = &Matrix[r][j]; r=i,c=j; do { r=prevRo(r); } while ( Input[r][j] == 0 ); Matrix[i][j].Up = &Matrix[r][j]; Matrix[i][j].Column = &Matrix[R-1][j]; Matrix[i][j].Column -> Size++; Matrix[i][j].ID = i; RoStart[i] = &Matrix[i][j]; } Root.Right = &Matrix[R-1][0]; Root.Left = &Matrix[R-1][C-1]; Matrix[R-1][0].Left = &Root; Matrix[R-1][C-1].Right = &Root; } /* void init() { scanf("%d %d",&R,&C); for ( int i=0;i<R;i++ ) for ( int j=0;j<C;j++ ) scanf("%d",&Input[i][j]); for ( int i=0;i<C;i++ ) Input[R][i] = 1; R ++; } */ void Place ( int num , int r , int c ) { int En = Encode ( num , r , c ); Node * PresRow = ExactCover::RoStart[En]; Cover ( PresRow -> Column ); for ( Node * nxtRight = PresRow -> Right; nxtRight != PresRow ; nxtRight = nxtRight -> Right ) Cover ( nxtRight -> Column ); Filled ++; Stk[Top++] = En; } }; const int offset1 = 81 , offset2 = offset1+81, offset3 = offset2+81, offset4 = offset3+81, offset5 = offset4+9; void init() { // No of rows -> int R = 81 * 9,num,C; for ( int i=0;i<9;i++ ) for ( int j=0;j<9;j++ ) for ( int d=0;d<9;d++ ) { // place d at i,j num = Encode(d,i,j); ExactCover::Input[num][GetGridpt(num)] = 1;// one per cell ExactCover::Input[num][offset1 + GetNumRo(num)] = 1; ExactCover::Input[num][offset2 + GetNumCol(num)] = 1; ExactCover::Input[num][offset3 + GetNumSq(num)] = 1; // d1 if ( i == j ) ExactCover::Input[num][offset4 + d] = 1; if ( i == 8-j ) ExactCover::Input[num][offset5 + d] = 1; } C = offset5 + 9; for ( int i=0;i<C;i++ ) ExactCover::Input[R][i] = 1; R ++; ExactCover::R = R , ExactCover :: C = C; } void FillValues() { ExactCover::Filled = 0; for ( int i=0;i<9;i++ ) for ( int j=0;j<9;j++ ) if ( Igrid[i][j] ) ExactCover::Place ( Igrid[i][j]-1 , i , j ); } char _grid[9][9]; void solve() { ExactCover::Top = ExactCover:: Found = ExactCover:: Filled = 0; ExactCover::Construct(); FillValues(); ExactCover::Search(); } int main() { int kase_; init(); //freopen ( "inp" , "r" , stdin ); while(kase_--) { for ( int i=0;i<9;i++ ) for ( int j=0;j<9;j++ ) if ( _grid[i][j] >= '0' ) Igrid[i][j] = _grid[i][j] - '0'; else Igrid[i][j] = 0; // HumanSolve(); solve(); } return 0; }
Comments

