Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. void n_queens(int board[10][10], int size, int solution_num)
  2. {
  3. int temp;
  4. int counter = 0;
  5. int malkot[10] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };
  6. for (int i = 0; i < size; i++)
  7. {
  8. temp = PutMeMalkot(malkot, size, 0, 0, i);
  9. if (temp != -4)
  10. {
  11. malkot[i] = temp;
  12. }
  13. else
  14. {
  15. temp = ChnageAllThePastMalkot(malkot, i, size, i);
  16. if (temp == -6)
  17. {
  18. printf("no solution ");
  19. }
  20. else
  21. {
  22. i = temp;
  23. }
  24. }
  25. if (i == size-1)
  26. {
  27. counter++;
  28. ChnageAllThePastMalkot(malkot, i, size, i);
  29. }
  30. if (counter==solution_num)
  31. {
  32. printmalkot(board, size, 0, 0);
  33. }
  34. }
  35. }
  36. int ChnageAllThePastMalkot(int malkot[], int Row, int size, int witchRow)
  37. {
  38. if (malkot[Row] + 1 < size - 1 && Row < 0)
  39. {
  40. if (malkot[Row - 1] + 1 > size)
  41. {
  42. malkot[Row - 1] = -1;
  43. return ChnageAllThePastMalkot(malkot, Row - 1, size, witchRow);
  44. }
  45. else
  46. {
  47. int temp = PutMeMalkot(malkot, size, 0, malkot[Row - 1] + 1, Row - 1);
  48. if (temp == -4)
  49. {
  50. malkot[Row - 1] = -1;
  51. return ChnageAllThePastMalkot(malkot, Row - 1, size, witchRow);
  52. }
  53. else
  54. {
  55. malkot[Row - 1] = temp;
  56. return Row - 1;
  57. }
  58. }
  59. }
  60. else
  61. {
  62. malkot[witchRow] = -6;
  63. return -6;
  64. }
  65. }
  66. int PutMeMalkot(int malkot[], int size, int row, int place, int WhatRow)
  67. {
  68. if (place >= size)
  69. {
  70. return -4;
  71. }
  72. if (row < size - 1 && WhatRow != 0 && row != WhatRow)
  73. {
  74. if (malkot[row] != place && (malkot[row] + (WhatRow - row) != place) && (malkot[row] - (WhatRow - row) != place))
  75. {
  76. return PutMeMalkot(malkot, size, row + 1, place, WhatRow);
  77. }
  78. else
  79. {
  80. return PutMeMalkot(malkot, size, 0, place + 1, WhatRow);
  81. }
  82. }
  83. else
  84. {
  85. return place;
  86. }
  87. return -2;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement