LiliyaBurlakova

Chess

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