CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » August Mini Challenge 2009 (Contest VII) » Sudoku Solver
0
Your rating: None

CodeChef submission 79969 (C++ 4.0.0-8)

CodeChef submission 79969 (C++ 4.0.0-8) plaintext list. Status: TLE, problem NSUDOKU, contest AUGMINI. By pr0ton (Pratik Tandel), 2009-08-24 14:59:56.
  1.  
  2. #include<queue>
  3. #include<fstream>
  4. #include<cstdlib>
  5.  
  6.  
  7.  
  8.  
  9.  
  10. #define oo (int)13e7
  11. #define sl(n) scanf("%lld",&n)
  12. #define sf(n) scanf("%lf",&n)
  13. #define fill(a,v) memset(a, v, sizeof a)
  14. #define ull unsigned long long
  15. #define ll long long
  16. #define bitcount __builtin_popcount
  17. #define all(x) x.begin(), x.end()
  18. #define pb( z ) push_back( z )
  19. #define gcd __gcd
  20. using namespace std;
  21.  
  22. const int mx = 901;
  23. int a[mx][mx];
  24. int n, k;
  25. bool row[mx][mx], col[mx][mx];
  26. int bxid[mx][mx];
  27. bool box[mx][mx];
  28. int n2, cnt[mx];//, cnt2[mx];
  29. // put at x, y value d
  30. inline void put( int x, int y,int d )
  31. {
  32. a[x][y] = d;
  33. int b = bxid[ x ][ y ];
  34. box[ b ][ d ] = 1;
  35. row[ x ][ d ] = 1;
  36. col [ y ][ d ] = 1;
  37. --cnt[ d ];
  38. //cnt2[ d ]--;
  39. }
  40.  
  41.  
  42. /*
  43. const int mxip = (int)4.5e6;
  44. char ip[ mxip ];
  45. int ipsz = 0;
  46. *//*
  47. const int mxop = (int)2.8e6;
  48. char op[ mxop ];
  49. int opsz = 0;*/
  50. #define pchar(c) putchar_unlocked(c)//op[opsz++] = c
  51. #define getcx() getchar_unlocked()
  52. inline void s( int &n )
  53. {
  54. n=0;
  55. int ch=getcx();
  56. while( ch < '0' || ch > '9' ) ch=getcx();
  57. while( ch >= '0' && ch <= '9' )
  58. n = (n<<3)+(n<<1) + ch-'0', ch=getcx();
  59. }
  60.  
  61. //fast output to buffer, and then print using fwrite
  62. inline void writeNum( int x )
  63. {
  64. int tmp;
  65. if( x >= 100 )
  66. {
  67. tmp = x / 100;
  68. pchar( '0' + tmp );
  69. x -= tmp * 100;
  70. tmp = x/10;
  71. pchar( '0' + tmp );
  72. x -= tmp * 10;
  73. pchar( '0' + x );
  74. }
  75. else if( x >= 10 )
  76. {
  77. tmp = x/10;
  78. pchar( '0' + tmp );
  79. x -= tmp * 10;
  80. pchar( '0' + x );
  81. }
  82. else
  83. {
  84. pchar( '0' + x );
  85. }
  86. }
  87.  
  88.  
  89.  
  90. static unsigned int g_seed;
  91. //init
  92. inline void fast_srand( int seed )
  93. {
  94. g_seed = seed;
  95. }
  96. //fast random generator between 0 and 32767
  97. inline int fastrand()
  98. {
  99. g_seed = (214013*g_seed+2531011);
  100. return (g_seed>>16)&0x7FFF;
  101. }
  102. int konst = 1;
  103. int mod[ 32768 ]; //precomputed mod n2
  104. int main()
  105. {
  106. //fread( ip, mxip, 1, stdin );
  107. int start = clock(); //timer
  108. fast_srand( clock() );
  109. s( n ); s( k );
  110. n2 = n*n;
  111. #ifdef ONLINE_JUDGE
  112. #else
  113. printf("%d\n", n);
  114. #endif
  115. //assign box ids to each cell
  116. for(int i=1; i <= n2; i++)
  117. for(int j=1; j <= n2; j++)
  118. bxid[i][j] = n* ( (i-1)/n ) + (j-1)/n;
  119. //read input
  120. for(int i=0; i < k; i++)
  121. {
  122. int x, y, v;
  123. s(x); s(y); s(v);
  124. put( x, y, v );
  125. }
  126. for(int i=1; i < 32768; ++i)
  127. {
  128. mod[i] = mod[i-1] + 1 ;
  129. if( mod[i] == n2 )
  130. mod[i] = 0;
  131. mod[i-1]++;
  132. }
  133. mod[32767]++;
  134. const double delta = 2.5 + min( 0.31, ( 0.25 * n2 / 900 ) );
  135. int mtries = 5.21e8 / ( n2*n2 );
  136. int ch2 = 7.7e7 / ( n2*n2 );
  137. for(int i=1; i <= n2; i++)
  138. {
  139. for(int j=1; j <= n2; j++)
  140. if( a[i][j] == 0 )
  141. {
  142. int zx = mod[ fastrand() ] ;
  143. {
  144. int tries = mtries;
  145. char bcost = 3; int bbit = zx;
  146. while( tries--)
  147. {
  148. zx = mod[ fastrand() ] ;
  149. char ccost = 0;
  150. if( row[i][zx] ) ccost++;
  151. if( col[j][zx] ) ccost++;
  152. if( box[ bxid[i][j] ] [ zx ] ) ccost++;
  153. if( ccost < bcost )
  154. {
  155. bcost = ccost;
  156. bbit = zx;
  157. if( bcost == 0 ) break;
  158. }
  159. else if( bcost == ccost )
  160. {
  161. if( cnt[ bbit ] > cnt[ zx ] )
  162. bbit = zx;
  163. }
  164. }
  165. zx = bbit;
  166. }
  167. put( i, j, zx );
  168. }
  169. int cur = clock();
  170. double secs = ( start - cur + 0.0 ) / CLOCKS_PER_SEC;
  171. if( secs > delta )
  172. mtries = ch2;
  173. }
  174. for(int i=1; i <= n2; i++)
  175. for(int j=1; j <= n2; j++)
  176. {
  177. writeNum( a[i][j] );
  178. //if( j < n2 )
  179. pchar( ' ' );
  180. //else
  181. // pchar( '\n' );
  182. }
  183. //fwrite (op , 1 , opsz , stdout );
  184. return 0;
  185. }
  186.  


Comments

  • Login or Register to post a comment.

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com