Advertisement
Guest User

Untitled

a guest
May 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp3
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int[,] board=new int[10,10];
  14. n_queens(board, 3, 2);
  15. }
  16. public static void n_queens(int[,] board, int size, int solution_num)
  17. {
  18. int temp;
  19. int count = 0;
  20. int[] malkot = new int[size];
  21. for (int k = 0; k < size; k++)
  22. {
  23. malkot[k] = -1;
  24. }
  25. for (int i = 0; i < size; i++)
  26. {
  27. temp = PutMeMalkot(malkot, size, 0, 0, i);
  28. if (temp != -4)
  29. {
  30. malkot[i] = temp;
  31. }
  32. else
  33. {
  34. temp = ChnageAllThePastMalkot(malkot, i, size, i);
  35. if (temp == -6)
  36. {
  37. Console.WriteLine("no answer");
  38. }
  39. else
  40. {
  41. i = temp;
  42. }
  43. }
  44. if (i == size - 1)
  45. {
  46. count++;
  47. }
  48. if (solution_num == count)
  49. {
  50. Console.WriteLine("answerHere");
  51. }
  52. }
  53. }
  54. public static int ChnageAllThePastMalkot(int[] malkot, int Row,int size,int witchRow)
  55. {
  56. if (malkot[Row] + 1 < size - 1 && Row < 0)
  57. {
  58. if (malkot[Row - 1] + 1 > size)
  59. {
  60. malkot[Row - 1] = -1;
  61. return ChnageAllThePastMalkot(malkot, Row - 1, size, witchRow);
  62. }
  63. else
  64. {
  65. int temp = PutMeMalkot(malkot, size, 0, malkot[Row - 1] + 1, Row - 1);
  66. if (temp == -4)
  67. {
  68. malkot[Row - 1] = -1;
  69. return ChnageAllThePastMalkot(malkot, Row - 1, size, witchRow);
  70. }
  71. else
  72. {
  73. malkot[Row - 1] = temp;
  74. return Row - 1;
  75. }
  76. }
  77. }
  78. else
  79. {
  80. malkot[witchRow] = -6;
  81. return -6;
  82. }
  83. }
  84. public static int PutMeMalkot(int[] malkot, int size,int row,int place,int WhatRow)
  85. {
  86. if (place > size-1)
  87. {
  88. return -4;
  89. }
  90. if (row < size-1 && WhatRow != 0 && row != WhatRow)
  91. {
  92. if (malkot[row] != place
  93. && (malkot[row] + (1 * (WhatRow - row)) != place)
  94. && (malkot[row] - (1 * (WhatRow - row)) != place))
  95. {
  96. return PutMeMalkot(malkot, size, row + 1, place, WhatRow);
  97. }
  98. else
  99. {
  100. return PutMeMalkot(malkot, size, 0, place + 1, WhatRow);
  101. }
  102. }
  103. else
  104. {
  105. return place;
  106. }
  107. return -2;
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement