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 79964 (C++ 4.0.0-8)

CodeChef submission 79964 (C++ 4.0.0-8) plaintext list. Status: AC, problem NSUDOKU, contest AUGMINI. By triplem (Stephen Merriman), 2009-08-24 14:58:34.
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int grid[900][900];
  5. bool preset[900][900];
  6. int cc[3][900][900];
  7. int L,N;
  8. int numstates;
  9. char output[50000000];
  10. char input[50000000];
  11. long long sizeatend;
  12. int starttime;
  13. int outpos=0, inpos=0, numcharsin;
  14. void writeint(int a) {
  15. if (a==0) {
  16. output[outpos++]='0';
  17. return;
  18. }
  19. char tmp[10];
  20. int tp = 0;
  21. while (a) {tmp[tp++]=a%10;a/=10;}
  22. while (tp--) {
  23. output[outpos++]=tmp[tp]+'0';
  24. }
  25. }
  26. int getint() {
  27. while (input[inpos]<'0' || input[inpos]>'9') inpos++;
  28. int ret = 0;
  29. while (inpos<numcharsin && input[inpos]>='0' && input[inpos]<='9') ret = 10*ret+input[inpos++]-'0';
  30. return ret;
  31. }
  32. typedef struct State {
  33. long long parts[15];
  34. State() {for (int i=0; i<15; i++) parts[i]=0;}
  35. void update(int val) {
  36. // printf("before: %I64d\n",parts[val/61]);
  37. // printf("Adding 1LL<<%d\n",val);
  38. parts[val/61]|=1LL<<(val%61);
  39. // printf("after: %I64d\n",parts[val/61]);
  40. }
  41. void reset() {
  42. for (int i=0; i<15; i++) parts[i]=0;
  43. }
  44. void print() {
  45. for (int i=0; i<numstates; i++)
  46. for (int j=0; j<61; j++) printf("%d",(parts[i]&(1LL<<j))?1:0);
  47. printf("\n");
  48. }
  49. };
  50. int getscore() {
  51. int score = 0;
  52. static bool found[3][900][900];
  53. memset(found,0,3*900*900*sizeof(bool));
  54. for (int i=0; i<L; i++)
  55. for (int j=0; j<L; j++) {
  56. found[0][i][grid[i][j]-1]=true;
  57. found[1][j][grid[i][j]-1]=true;
  58. found[2][(i/N)*N+(j/N)][grid[i][j]-1]=true;
  59. }
  60. for (int i=0; i<L; i++)
  61. for (int k=0; k<L; k++)
  62. for (int j=0; j<3; j++) if (!found[j][i][k]) score++;
  63. return score;
  64.  
  65. }
  66. State states[3][900];
  67. void update(int x, int y) {
  68. cc[0][x][grid[x][y]-1]++;
  69. states[0][x].update(grid[x][y]-1);
  70. cc[1][y][grid[x][y]-1]++;
  71. states[1][y].update(grid[x][y]-1);
  72. cc[2][N*(x/N)+y/N][grid[x][y]-1]++;
  73. states[2][N*(x/N)+y/N].update(grid[x][y]-1);
  74. }
  75. int getbest(int x, int y, int type) {
  76. // printf("At %d,%d\n",x,y);
  77. State *one = &states[0][x];
  78. State *two = &states[1][y];
  79. State *three = &states[2][N*(x/N)+y/N];
  80. long long stophere = (1LL<<61)-1;
  81. for (int i=0; i<numstates; i++) {
  82. long long stop = (i==numstates-1?sizeatend:stophere);
  83. long long tmp = one->parts[i]|two->parts[i]|three->parts[i];
  84. if (tmp==stop) continue;
  85. for (int j=0; j<61 && 61*i+j<L; j++) {
  86. if (tmp&1) {tmp/=2;continue;}
  87. return 61*i+j+1;
  88. tmp/=2;
  89. }
  90. }
  91. for (int i=0; i<numstates; i++) {
  92. long long stop = (i==numstates-1?sizeatend:stophere);
  93. long long tmp1 = one->parts[i]|two->parts[i];
  94. long long tmp2 = one->parts[i]|three->parts[i];
  95. long long tmp3 = two->parts[i]|three->parts[i];
  96. if (tmp1==stop && tmp2==stop && tmp3==stop) continue;
  97. long long tmp;
  98. if (tmp1!=stop) tmp=tmp1;
  99. else if (tmp2!=stop) tmp=tmp2;
  100. else tmp = tmp3;
  101. for (int j=0; j<61 && 61*i+j<L; j++) {
  102. if (tmp&1) {tmp/=2;continue;}
  103. return 61*i+j+1;
  104. tmp/=2;
  105. }
  106. }
  107. if (type==100) {
  108. for (int i=0; i<numstates; i++) {
  109. long long stop = (i==numstates-1?sizeatend:stophere);
  110. long long tmp1 = one->parts[i];
  111. long long tmp2 = two->parts[i];
  112. long long tmp3 = three->parts[i];
  113. if (tmp1==stop && tmp2==stop && tmp3==stop) continue;
  114. long long tmp;
  115. if (tmp1!=stop) tmp=tmp1;
  116. else if (tmp2!=stop) tmp=tmp2;
  117. else tmp = tmp3;
  118. for (int j=0; j<61 && 61*i+j<L; j++) {
  119. if (tmp&1) {tmp/=2;continue;}
  120. return 61*i+j+1;
  121. tmp/=2;
  122. }
  123. }
  124. //fprintf(stderr,"\n\n\n\n\n\n");
  125. return rand()%L+1;
  126. }
  127. return 0;
  128. }
  129. int perm[810000];
  130. void print() {
  131. for (int i=0; i<L; i++) {
  132. for (int j=0; j<L; j++) {
  133. printf("%d ",grid[i][j]);
  134. }
  135. printf("\n");
  136. }
  137. printf("\n");
  138. }
  139. int getdelta(int x, int y1, int y2, bool doit) {
  140. if (grid[x][y1]==grid[x][y2]) return 0;
  141. // suppose we switch (x,y1) and (x,y2)
  142. int delta = 0;
  143. // row x is unchanged
  144. // column y1
  145. // we increase the grid[x][y2] count, decrease the grid[x][y1] coun
  146. if (cc[1][y1][grid[x][y2]-1]==0) {delta++; if (doit) cc[1][y1][grid[x][y2]-1]++; }
  147. if (cc[1][y1][grid[x][y1]-1]==1) {delta--; if (doit) cc[1][y1][grid[x][y1]-1]--; }
  148. // column y2
  149. if (cc[1][y2][grid[x][y2]-1]==1) {delta--; if (doit) cc[1][y2][grid[x][y2]-1]--; }
  150. if (cc[1][y2][grid[x][y1]-1]==0) {delta++; if (doit) cc[1][y2][grid[x][y1]-1]++; }
  151. if (y1/N!=y2/N) {
  152. if (cc[2][N*(x/N)+y1/N][grid[x][y2]-1]==0) {delta++;if (doit) cc[2][N*(x/N)+y1/N][grid[x][y2]-1]++; }
  153. if (cc[2][N*(x/N)+y1/N][grid[x][y1]-1]==1) {delta--;if (doit) cc[2][N*(x/N)+y1/N][grid[x][y1]-1]--; }
  154. if (cc[2][N*(x/N)+y2/N][grid[x][y2]-1]==1) {delta--;if (doit) cc[2][N*(x/N)+y2/N][grid[x][y2]-1]--; }
  155. if (cc[2][N*(x/N)+y2/N][grid[x][y1]-1]==0) {delta++;if (doit) cc[2][N*(x/N)+y2/N][grid[x][y1]-1]++; }
  156. }
  157. if (doit) {int tmp = grid[x][y1]; grid[x][y1] = grid[x][y2]; grid[x][y2] = tmp; }
  158. return delta;
  159. }
  160. int getdelta2(int x, int y1, int y2, bool doit) {
  161. if (grid[y1][x]==grid[y2][x]) return 0;
  162. // suppose we switch (x,y1) and (x,y2)
  163. int delta = 0;
  164. // row x is unchanged
  165. // column y1
  166. // we increase the grid[y2][x] count, decrease the grid[y1][x] coun
  167. if (cc[0][y1][grid[y2][x]-1]==0) {delta++; if (doit) cc[0][y1][grid[y2][x]-1]++; }
  168. if (cc[0][y1][grid[y1][x]-1]==1) {delta--; if (doit) cc[0][y1][grid[y1][x]-1]--; }
  169. // column y2
  170. if (cc[0][y2][grid[y2][x]-1]==1) {delta--; if (doit) cc[1][y2][grid[y2][x]-1]--; }
  171. if (cc[0][y2][grid[y1][x]-1]==0) {delta++; if (doit) cc[1][y2][grid[y1][x]-1]++; }
  172. if (y1/N!=y2/N) {
  173. if (cc[2][N*(x/N)+y1/N][grid[y2][x]-1]==0) {delta++;if (doit) cc[2][N*(x/N)+y1/N][grid[y2][x]-1]++; }
  174. if (cc[2][N*(x/N)+y1/N][grid[y1][x]-1]==1) {delta--;if (doit) cc[2][N*(x/N)+y1/N][grid[y1][x]-1]--; }
  175. if (cc[2][N*(x/N)+y2/N][grid[y2][x]-1]==1) {delta--;if (doit) cc[2][N*(x/N)+y2/N][grid[y2][x]-1]--; }
  176. if (cc[2][N*(x/N)+y2/N][grid[y1][x]-1]==0) {delta++;if (doit) cc[2][N*(x/N)+y2/N][grid[y1][x]-1]++; }
  177. }
  178. if (doit) {int tmp = grid[y1][x]; grid[y1][x] = grid[y2][x]; grid[y2][x] = tmp; }
  179. return delta;
  180. }
  181. int score;
  182. void doswitch(int x, int y, int z) {
  183. int delta = getdelta(x,y,z,false);
  184. if (delta>0) {
  185. getdelta(x,y,z,true);
  186. #ifndef ONLINE_JUDGE
  187. fprintf(stderr,"%d\n",score);
  188. score -= delta;
  189. #endif
  190. }
  191. }
  192. void doswitch2(int x, int y, int z) {
  193. int delta = getdelta2(x,y,z,false);
  194. if (delta>0) {
  195. getdelta2(x,y,z,true);
  196. #ifndef ONLINE_JUDGE
  197. fprintf(stderr,"%d\n",score);
  198. score -= delta;
  199. #endif
  200. }
  201.  
  202. }
  203. int randperm2[900];
  204.  
  205. void checkbrute() {
  206.  
  207. #ifndef ONLINE_JUDGE
  208. score = getscore();
  209. fprintf(stderr,"Starting at %d\n",score);
  210. #endif
  211. while (true) {
  212. int xx;
  213.  
  214. for (xx=0; xx<L && (clock()-starttime)*1./CLOCKS_PER_SEC<2.7; xx++) {
  215. int x = randperm2[xx];
  216. for (int y1=0; y1<L; y1++) { if (preset[x][y1]) continue;
  217. for (int y2=y1+1; y2<L && y1/N==y2/N; y2++) { if (preset[x][y2]) continue;
  218. doswitch(x,y1,y2);
  219. }}
  220. if ((clock()-starttime)*1./CLOCKS_PER_SEC>=2.7) break;
  221. for (int y1=0; y1<L; y1++) { if (preset[y1][x]) continue;
  222. for (int y2=y1+1; y2<L && y1/N==y2/N; y2++) { if (preset[y2][x]) continue;
  223. doswitch2(x,y1,y2);
  224. }}
  225. }
  226. if (xx!=L) break;
  227. }
  228. }
  229. int main() {
  230. starttime = clock();
  231. cin.read(input,50000000);
  232. numcharsin = cin.gcount();
  233. N = getint(); int K = getint();
  234. L = N*N;
  235. for (int i=0; i<L; i++) randperm2[i]=i;
  236. random_shuffle(randperm2,randperm2+L);
  237. int LL = L*L;
  238. srand(time(NULL));
  239. for (int i=0; i<LL; i++) perm[i]=i;
  240. random_shuffle(perm,perm+LL);
  241. /* for (int i=0; i<LL; i++) {
  242. int m = i/L;
  243. int n = i%L;
  244. int x = m/N;
  245. int y = m%N;
  246. x += N*(n/N);
  247. y += N*(n%N);
  248. perm[i] = x*L+y;
  249. }
  250. */
  251. numstates = (L+60)/61;
  252. int leftover = L%61;
  253.  
  254. sizeatend = (1LL<<leftover)-1;
  255. // printf("numstates: %d\n",numstates);
  256. for (int i=0; i<K; i++) {
  257. int x = getint();
  258. int y = getint();
  259. int d = getint();x--;-y--;
  260. grid[x][y]=d;
  261. preset[x][y]=true;
  262. update(x,y);
  263. }
  264. for (int i=0; i<LL; i++) {
  265. int x = perm[i]%L;
  266. int y = perm[i]/L;
  267. if (grid[x][y]==0) {
  268. grid[x][y]=getbest(x,y,0);
  269. if (grid[x][y]) update(x,y);
  270. }
  271. }
  272. for (int i=0; i<L; i++)
  273. for (int j=0; j<L; j++) {
  274. if (grid[i][j]==0) {
  275. grid[i][j]=getbest(i,j,100);
  276. update(i,j);
  277. }
  278. }
  279. checkbrute();
  280. #ifndef ONLINE_JUDGE
  281. {
  282. fprintf(stderr,"Score: %d\n",getscore());
  283. }
  284. #endif
  285. for (int i=0; i<L; i++) {
  286. for (int j=0; j<L; j++) {
  287. writeint(grid[i][j]);
  288. output[outpos++]=' ';
  289. }
  290. output[outpos++]='\n';
  291. }
  292. cout.write(output,outpos);
  293. }


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