LiliyaBurlakova

HorseMove

Dec 8th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int N = Integer.parseInt(scanner.nextLine());
  8. int moves = N * N;
  9.  
  10. int[][] chessBoard = new int[N][N];
  11.  
  12. for (int row = 0; row < N; row++) {
  13. for (int col = 0; col < N; col++) {
  14. chessBoard[row][col] = 0;
  15. }
  16. }
  17. chessBoard[0][0] = 1;
  18.  
  19. for (int row = 0; row < N; row++) {
  20. for (int col = 0; col < N; col++) {
  21. for (int i = 2; i <= moves; i++) {
  22. if (row - 2 >= 0 && col - 1 >= 0) {
  23. if (chessBoard[row - 2][col - 1] == 0) {
  24. chessBoard[row - 2][col - 1] = i;
  25. row -= 2;
  26. col -= 1;
  27. continue;
  28. }
  29. }
  30. if (row - 2 >= 0 && col + 1 < N) {
  31. if (chessBoard[row - 2][col + 1] == 0) {
  32. chessBoard[row - 2][col + 1] = i;
  33. row -= 2;
  34. col += 1;
  35. continue;
  36. }
  37. }
  38. if (row + 1 < N && col - 2 >= 0) {
  39. if (chessBoard[row + 1][col - 2] == 0) {
  40. chessBoard[row + 1][col - 2] = i;
  41. row += 1;
  42. col -= 2;
  43. continue;
  44. }
  45. }
  46. if (row - 1 >= 0 && col - 2 >= 0) {
  47. if (chessBoard[row - 1][col - 2] == 0) {
  48. chessBoard[row - 1][col - 2] = i;
  49. row -= 1;
  50. col -= 2;
  51. continue;
  52. }
  53. }
  54. if (row - 1 >= 0 && col + 2 < N) {
  55. if (chessBoard[row - 1][col + 2] == 0) {
  56. chessBoard[row - 1][col + 2] = i;
  57. row -= 1;
  58. col += 2;
  59. continue;
  60. }
  61. }
  62. if (row + 1 < N && col + 2 < N) {
  63. if (chessBoard[row + 1][col + 2] == 0) {
  64. chessBoard[row + 1][col + 2] = i;
  65. row += 1;
  66. col += 2;
  67. continue;
  68. }
  69. }
  70. if (row + 2 < N && col - 1 >= 0) {
  71. if (chessBoard[row + 2][col - 1] == 0) {
  72. chessBoard[row + 2][col - 1] = i;
  73. row += 2;
  74. col -= 1;
  75. continue;
  76. }
  77. }
  78. if (row + 2 < N && col + 1 < N) {
  79. if (chessBoard[row + 2][col + 1] == 0) {
  80. chessBoard[row + 2][col + 1] = i;
  81. row += 2;
  82. col += 1;
  83. continue;
  84. }
  85. }
  86. search:
  87. {
  88. for (int c = 0; c < N; c++) {
  89. for (int r = 0; r < N; r++) {
  90. if (chessBoard[r][c] == 0) {
  91. chessBoard[r][c] = i;
  92. row = r;
  93. col = c;
  94. break search;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. for (int r = 0; r < N; r++) {
  103. for (int c = 0; c < N; c++) {
  104. System.out.printf("%d ", chessBoard[r][c]);
  105. }
  106. System.out.println();
  107. }
  108. }
  109. }
Add Comment
Please, Sign In to add comment