fork download
  1. #include <stdio.h>
  2.  
  3. #define SIZE 8
  4.  
  5. int main() {
  6. int board[SIZE][SIZE] = {0};
  7. int accessibility[SIZE][SIZE] = {
  8. {2, 3, 4, 4, 4, 4, 3, 2},
  9. {3, 4, 6, 6, 6, 6, 4, 3},
  10. {4, 6, 8, 8, 8, 8, 6, 4},
  11. {4, 6, 8, 8, 8, 8, 6, 4},
  12. {4, 6, 8, 8, 8, 8, 6, 4},
  13. {4, 6, 8, 8, 8, 8, 6, 4},
  14. {3, 4, 6, 6, 6, 6, 4, 3},
  15. {2, 3, 4, 4, 4, 4, 3, 2}
  16. };
  17.  
  18. int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2};
  19. int vertical[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
  20.  
  21. int currentRow = 0;
  22. int currentCol = 0;
  23. board[currentRow][currentCol] = 1;
  24.  
  25. for (int moveNumber = 2; moveNumber <= 64; moveNumber++) {
  26. int minAccess = 9;
  27. int bestMove = -1;
  28. int nextRow = -1;
  29. int nextCol = -1;
  30.  
  31. for (int moveType = 0; moveType < 8; moveType++) {
  32. int testRow = currentRow + vertical[moveType];
  33. int testCol = currentCol + horizontal[moveType];
  34.  
  35. if (testRow >= 0 && testRow < SIZE && testCol >= 0 && testCol < SIZE) {
  36. if (board[testRow][testCol] == 0) {
  37. if (accessibility[testRow][testCol] < minAccess) {
  38. minAccess = accessibility[testRow][testCol];
  39. bestMove = moveType;
  40. nextRow = testRow;
  41. nextCol = testCol;
  42. }
  43. }
  44. }
  45. }
  46.  
  47. if (bestMove != -1) {
  48. currentRow = nextRow;
  49. currentCol = nextCol;
  50. board[currentRow][currentCol] = moveNumber;
  51.  
  52. for (int moveType = 0; moveType < 8; moveType++) {
  53. int testRow = currentRow + vertical[moveType];
  54. int testCol = currentCol + horizontal[moveType];
  55. if (testRow >= 0 && testRow < SIZE && testCol >= 0 && testCol < SIZE) {
  56. accessibility[testRow][testCol]--;
  57. }
  58. }
  59. } else {
  60. break;
  61. }
  62. }
  63.  
  64. for (int r = 0; r < SIZE; r++) {
  65. for (int c = 0; c < SIZE; c++) {
  66. printf("%2d ", board[r][c]);
  67. }
  68. printf("\n");
  69. }
  70.  
  71. return 0;
  72. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
 1 22  3 18 25 30 13 16 
 4 19 24 29 14 17 34 31 
23  2 21 26 35 32 15 12 
20  5 56 49 28 41 36 33 
57 50 27 42 61 54 11 40 
 6 43 60 55 48 39 64 37 
51 58 45  8 53 62 47 10 
44  7 52 59 46  9 38 63