Programming Puzzle 8-8-08 (spoiler)

Wyverex wyverex.cypher at gmail.com
Fri Aug 8 12:21:42 PDT 2008


//Heres a backtracking solution


import tango.io.Stdout;

char[][] boards =
["000075400000000008080190000300001060000000034000068170204000603900000020530200000",
"300000000050703008000028070700000043000000000003904105400300800100040000968000200",
"302609005500730000000000900000940000000000109000057060008500006000000003019082040",
"530000008007000030200006901000500200090370004000981000300040560000090000000007080",
"008310900095000160000000005000400000000080049006072000000001030000240607001008200",
"000400970000051600042000010030000000070508064000070000700030000300090000005864009",
"060500000720000000000000320000050637000004500000230180180009000603070000004006003",
"274000030000000005000600041900306000100280000006054000000000002007000583000095700",
"570000069000003800090000000801600000000030600702000050000060501000702000006091032",
"005200000400300700600000010800020100040800500000095000083040070090006080500902000",
"400500600200000000000020000002004380000030000790000504000060490070093810500100030",
"000790000001000000040050080000800027009003000000020403000040600004907100600501790",
"060010000403700008520640000002000000009438005000006300004301200000200000005070000",
"130400000705300000600020000000000027000900400000000085860500003059103000002004060",
"020001048400000037071006020500000000000010802000809500090030400000040000000902060",
"000000020006410035180020000008130406020000300600000000790005000004000008001300002",
"040000200000007090000006010870020004901000028060030100006800041000070050005900000",
"000030009048900000200470100125000080000080710000500000000090054061000003000050070",
"000000060306000000000000805000605071005000300100870042900200014201080000000703000",
"900000586008070004401000300002010900804005100000007000003008702000000000600040009",
"000032970070045010000800000001060000000000000029000840500620007004000009100009036",
"950003008800002000031000000060350090010007050000060010008000307000206009007000004",
"000000000300027801100083000005001000001370060007004002200060070004000000900030650",
"030207001000180670001030050000500900190004008000600020300700000000005080000020006",
"600000004020507000000000031010900060000350109800000002240108000067090000003000006",
"600095000000061802000000100500016000004000200002008036000002450040050000003400070",
"201070800460000090080010040000050030030980051000006000004097000500000000090020700",
"090000030100000800000312700050640007000730240080500000026000010000004300000050060",
"000560300100000800024000000009000000080720006610800000007206000400080037000104090",
"090035406001000000007000089070940050100200000006800700008004030000600040605000000",
"050060040006247091000190000000600900200000084000300005031000008000000006004000250"];


void main()
{
   foreach(org; boards)
   {
     auto sol = solve( org.dup, 0 );

     for(int x = 0; x <81; x+=9)
       Stdout.formatln("{}  {}  {}", org[x..x+9], (x == 4*9 ? "=": " "), 
sol[x..x+9]);
     Stdout.newline;
   }
}

char[] solve( char[] board, int index )
{
   if( index >= 81 )
     return board;

   if( board[index] != '0' )
     return solve( board, index + 1 );

   for( int i = 1; i < 10; i++ )
   {
     board[index] = i + '0';

     if( !test( board, index ) ) continue;

     auto ans = solve( board, index + 1 );
     if( ans !is null )
       return ans;
   }

   board[index] = '0';
   return null;
}


bool test( char[] board, int index )
{
   //tests if the new number is legal
   int x = index % 9;
   int y = index / 9;

   //test what row the number is in
   {
     int[10] find;
     for(int i = y*9; i < y*9+9; i++)
     {
       int n = board[i] - '0';
       if(n == 0) continue;
       if(find[n] == 1) return false;
       find[n] = 1;
     }
   }

   //test the column
   {
     int[10] find;
     for( int i = 0; i < 9; i++ )
     {
       int n = board[i*9 + x] - '0';
       if(n == 0) continue;
       if(find[n] == 1) return false;
       find[n] = 1;
     }
   }

   //now to test the local block
   {
     int bx = x / 3;
     int by = y / 3;
     int[10] find;
     for( int yy = by * 3; yy < (by * 3) + 3; ++yy )
     {
       for( int xx = bx*3; xx < (bx * 3) + 3; ++xx )
       {
         int i = yy*9+xx;
         int n = board[i] - '0';
         if(n == 0) continue;
         if(find[n] == 1) return false;
         find[n] = 1;
       }
     }
   }

   return true;
}


More information about the Digitalmars-d-learn mailing list