Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 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 _8_queen
  8. {
  9. class Program
  10. {
  11. static void Find(byte[,] board, List<byte[,]> results, int index = 0, int count_q = 0)
  12. {
  13. byte[,] saved_board = (byte[,])board.Clone();
  14. for (int i = 0; i < 8; i++)
  15. {
  16. for (int j = index; j < 8; j++)
  17. {
  18. if (board[i, j] == 0)
  19. {
  20. Mark(board, i, j);
  21. count_q++;
  22. Find(board, results, 0, count_q);
  23. }
  24. }
  25. }
  26. int saved_count = count_q;
  27. if (count_q != 8 && index != 8)
  28. {
  29. index++;
  30. count_q = saved_count;
  31. Find(saved_board, results, index, saved_count);
  32. }
  33. if (count_q == 8) results.Add(board);
  34. }
  35. static void Mark(byte[,] board, int index1, int index2)
  36. {
  37. board[index1, index2] = 2;
  38. int difference = index1 - index2;
  39. int sum = index1 + index2;
  40. for (int i = 0; i < 8; i++)
  41. {
  42. for (int j = 0; j < 8; j++)
  43. {
  44. if ((i == index1 || j == index2 || i - j == difference || i+j == sum) && board[i,j] == 0) board[i, j] = 1;
  45. }
  46. }
  47. }
  48. static void Main(string[] args)
  49. {
  50. byte[,] board = new byte[8, 8];
  51. List<byte[,]> results = new List<byte[,]>();
  52. Find(board, results);
  53.  
  54. List<byte[,]> actual_results = new List<byte[,]>();
  55. foreach (byte[,] b in results)
  56. {
  57. int count = 0;
  58. for (int i = 0; i < 8; i++)
  59. {
  60. for (int j = 0; j < 8; j++)
  61. {
  62. if (b[i, j] == 2) count++;
  63. }
  64. }
  65. if (count == 8) actual_results.Add(b);
  66. }
  67. board = actual_results[0];
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement