Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 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. Find(saved_board, results, index, saved_count);
  31. }
  32. if (count_q == 8) results.Add(board);
  33. }
  34. static void Mark(byte[,] board, int index1, int index2)
  35. {
  36. board[index1, index2] = 2;
  37. int difference = index1 - index2;
  38. int sum = index1 + index2;
  39. for (int i = 0; i < 8; i++)
  40. {
  41. for (int j = 0; j < 8; j++)
  42. {
  43. if ((i == index1 || j == index2 || i - j == difference || i+j == sum) && board[i,j] == 0) board[i, j] = 1;
  44. }
  45. }
  46. }
  47. static void Main(string[] args)
  48. {
  49. byte[,] board = new byte[8, 8];
  50. List<byte[,]> results = new List<byte[,]>();
  51. Find(board, results);
  52.  
  53.  
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement