bizzcuit

Horse Path

Nov 29th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner in = new Scanner(System.in);
  9.  
  10.         int n = in.nextInt();
  11.  
  12.         int[][] matrix = new int[n][n];
  13.  
  14.  
  15.         int horseRowPosition = 0;
  16.         int horseColPosition = 0;
  17.         matrix[horseRowPosition][horseColPosition] = 1;
  18.         int count = 2;
  19.  
  20.         while (count <= n * n){
  21.             if(horseRowPosition - 2 >= 0 && horseColPosition - 1 >= 0 && matrix[horseRowPosition - 2][horseColPosition - 1] == 0){
  22.                 matrix[horseRowPosition - 2][horseColPosition - 1] = count;
  23.                 horseRowPosition -= 2;
  24.                 horseColPosition -= 1;
  25.             } else if(horseRowPosition - 2 >= 0 && horseColPosition + 1 < n && matrix[horseRowPosition - 2][horseColPosition + 1] == 0){
  26.                 matrix[horseRowPosition - 2][horseColPosition + 1] = count;
  27.                 horseRowPosition -= 2;
  28.                 horseColPosition += 1;
  29.             } else if(horseRowPosition - 1 >= 0 && horseColPosition - 2 >= 0 && matrix[horseRowPosition - 1][horseColPosition - 2] == 0){
  30.                 matrix[horseRowPosition - 1][horseColPosition - 2] = count;
  31.                 horseRowPosition -= 1;
  32.                 horseColPosition -= 2;
  33.             } else if(horseRowPosition - 1 >= 0 && horseColPosition + 2 < n && matrix[horseRowPosition - 1][horseColPosition + 2] == 0){
  34.                 matrix[horseRowPosition - 1][horseColPosition + 2] = count;
  35.                 horseRowPosition -= 1;
  36.                 horseColPosition += 2;
  37.             } else if(horseRowPosition + 1 < n && horseColPosition - 2 >= 0 && matrix[horseRowPosition + 1][horseColPosition - 2] == 0){
  38.                 matrix[horseRowPosition + 1][horseColPosition - 2] = count;
  39.                 horseRowPosition += 1;
  40.                 horseColPosition -= 2;
  41.             } else if(horseRowPosition + 1 < n && horseColPosition + 2 < n && matrix[horseRowPosition + 1][horseColPosition + 2] == 0){
  42.                 matrix[horseRowPosition + 1][horseColPosition + 2] = count;
  43.                 horseRowPosition += 1;
  44.                 horseColPosition += 2;
  45.             } else if(horseRowPosition + 2 < n && horseColPosition - 1 >= 0 && matrix[horseRowPosition + 2][horseColPosition - 1] == 0){
  46.                 matrix[horseRowPosition + 2][horseColPosition - 1] = count;
  47.                 horseRowPosition += 2;
  48.                 horseColPosition -= 1;
  49.             } else if(horseRowPosition + 2 < n && horseColPosition + 1 < n && matrix[horseRowPosition + 2][horseColPosition + 1] == 0){
  50.                 matrix[horseRowPosition + 2][horseColPosition + 1] = count;
  51.                 horseRowPosition += 2;
  52.                 horseColPosition += 1;
  53.             } else{
  54.                 for (int i = 0; i < matrix.length; i++) {
  55.                     boolean newPositionFound = false;
  56.                     for (int j = 0; j < matrix[i].length; j++) {
  57.                         if (matrix[i][j] == 0) {
  58.                             horseRowPosition = i;
  59.                             horseColPosition = j;
  60.                             newPositionFound = true;
  61.                             matrix[i][j] = count;
  62.                             break;
  63.                         }
  64.                     }
  65.                     if (newPositionFound) break;
  66.                 }
  67.             }
  68.             count++;
  69.         }
  70.  
  71.  
  72.         for (int row = 0; row < matrix.length; row++) {
  73.             for (int col = 0; col < matrix[row].length; col++) {
  74.                 System.out.print(matrix[row][col] + " ");
  75.             }
  76.             System.out.println();
  77.  
  78.         }
  79.         //i -= 2, j -= 1
  80.         //i -= 2, j += 1
  81.         //i -= 1, j -= 2
  82.         //i -= 1, j += 2
  83.         //i += 1. j -= 2
  84.         //i += 1, j += 2
  85.         //i += 2, j -= 1
  86.         //i += 2, j += 1
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment