fork download
  1. #define N 6
  2.  
  3. void Row(int y);
  4.  
  5. int Counter;
  6. int Solution[N];
  7. int ListElement[1000][N];
  8.  
  9. void CheckUniqueSolution(void)
  10. {
  11. int i,j,s;
  12. int chk[7];
  13.  
  14. for(j=0; j<Counter; j++)
  15. {
  16. for(i=0;i<7;i++) chk[i]=0;
  17. for(i=0; i<N; i++)
  18. {
  19. s=Solution[i];
  20. if(s==ListElement[j][N-1-i]) chk[0]++;
  21. if(N-1-s==ListElement[j][i]) chk[1]++;
  22. if(N-1-s==ListElement[j][N-1-i]) chk[2]++;
  23. if(i==ListElement[j][s]) chk[3]++;
  24. if(i==ListElement[j][N-1-s]) chk[4]++;
  25. if(N-1-i==ListElement[j][s]) chk[5]++;
  26. if(N-1-i==ListElement[j][N-1-s]) chk[6]++;
  27. }
  28. for(i=0;i<7;i++) if(chk[i]==N) return;
  29. }
  30. for(i=0;i<N;i++)ListElement[Counter][i]=Solution[i];
  31. Counter++;
  32. }
  33.  
  34. int Check(int x, int y)
  35. {
  36. int i;
  37.  
  38. for(i=0; i<N; i++) if(Solution[i]>=0)
  39. {
  40. if(Solution[i]==x) return 0;
  41. if((i==y-2)||(i==y+2))if((Solution[i]==x-1)||(Solution[i]==x+1))return 0;
  42. if((i==y-1)||(i==y+1))if((Solution[i]==x-2)||(Solution[i]==x+2))return 0;
  43. }
  44. return 1;
  45. }
  46.  
  47. void Row(int y)
  48. {
  49. int i, z;
  50.  
  51. for(i=0; i<N; i++)
  52. {
  53. for(z=y; z<N; z++) Solution[z]=-1;
  54. if(Check(i,y))
  55. {
  56. Solution[y]=i;
  57. if(y==N-1) CheckUniqueSolution();
  58. else Row(y+1);
  59. }
  60. }
  61. }
  62.  
  63. int main(void)
  64. {
  65. Counter=0;
  66. Row(0);
  67. printf("\n%d", Counter);
  68. return 0;
  69. }
Success #stdin #stdout 0.02s 1696KB
stdin
Standard input is empty
stdout
21