Advertisement
Guest User

Untitled

a guest
Oct 12th, 2013
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. enum {S0=3, S1=S0*S0, S2=S1*S1};
  2.  
  3. static int _count(int num)
  4. {
  5. int i, count = 0;
  6. for (i=0; i<S1; i++)
  7. if ((num & (1<<i)))
  8. count++;
  9. return count;
  10. }
  11.  
  12. static int countzeros(int num)
  13. {
  14. return _count(~num);
  15. }
  16.  
  17. static void set_banned(unsigned short *banned, char *d, int j)
  18. {
  19. if (d[j] != '0') {
  20. int jr = j / S1, jc = j % S1;
  21. int k, kr, kc, b = 1<<(d[j]-'1');
  22. for (k=0; k<S1; k++)
  23. banned[jr*S1+k] |= b, banned[k*S1+jc] |= b;
  24. for (kr=0; kr<S0; kr++)
  25. for (kc=0; kc<S0; kc++)
  26. banned[(kr+(jr/S0)*S0)*S1+kc+(jc/S0)*S0] |= b;
  27. }
  28. }
  29.  
  30. static int bestindex(unsigned short *banned, char *d)
  31. {
  32. int score, index, bestscore=S1, bestindex=-1;
  33. for (index=0; index<S2; index++)
  34. if (d[index]=='0' && (score = countzeros(banned[index]), score < bestscore))
  35. bestindex = index, bestscore = score;
  36. return bestindex;
  37. }
  38.  
  39. static void recurse(char *d, unsigned short *in_banned, int index)
  40. {
  41. unsigned short banned[S2];
  42. memcpy(banned, in_banned, sizeof(banned));
  43. if (index < 0)
  44. for (index = 0; index<S2; index++)
  45. set_banned(banned, d, index);
  46. else
  47. set_banned(banned, d, index);
  48.  
  49. if (index = bestindex(banned, d), index == -1) {
  50. puts(d);
  51. return;
  52. }
  53. for (d[index]='0'+S1; d[index]>'0'; d[index]--)
  54. if (!(banned[index] & (1<<(d[index]-'1'))))
  55. recurse(d, banned, index);
  56. }
  57.  
  58. int main(int argc, char *argv[])
  59. {
  60. unsigned short banned[S2];
  61. memset(banned, 0, sizeof(banned));
  62. if (argc >= 2)
  63. recurse(argv[1], banned, -1);
  64. return 0;
  65. }
  66. // 100000100000000000000000000010000010000000000000000000000000000002000002003000003
  67. // 010000000020000000030000000040000000050000000000000000000006789000000000000000000
  68. // 003000051502006400007050000000630700200708006004021000000070800008100609170000500
  69. // 006900070000010002800000000020000004000000001005006000000000060000002050010043000 (hard - one solution)
  70. // 452619378371248965986573241794162583628435719135897624213754896849326157567981432
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement