Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int[][] matrix = new int[n][n];
- int horseRowPosition = 0;
- int horseColPosition = 0;
- matrix[horseRowPosition][horseColPosition] = 1;
- int count = 2;
- while (count <= n * n){
- if(horseRowPosition - 2 >= 0 && horseColPosition - 1 >= 0 && matrix[horseRowPosition - 2][horseColPosition - 1] == 0){
- matrix[horseRowPosition - 2][horseColPosition - 1] = count;
- horseRowPosition -= 2;
- horseColPosition -= 1;
- } else if(horseRowPosition - 2 >= 0 && horseColPosition + 1 < n && matrix[horseRowPosition - 2][horseColPosition + 1] == 0){
- matrix[horseRowPosition - 2][horseColPosition + 1] = count;
- horseRowPosition -= 2;
- horseColPosition += 1;
- } else if(horseRowPosition - 1 >= 0 && horseColPosition - 2 >= 0 && matrix[horseRowPosition - 1][horseColPosition - 2] == 0){
- matrix[horseRowPosition - 1][horseColPosition - 2] = count;
- horseRowPosition -= 1;
- horseColPosition -= 2;
- } else if(horseRowPosition - 1 >= 0 && horseColPosition + 2 < n && matrix[horseRowPosition - 1][horseColPosition + 2] == 0){
- matrix[horseRowPosition - 1][horseColPosition + 2] = count;
- horseRowPosition -= 1;
- horseColPosition += 2;
- } else if(horseRowPosition + 1 < n && horseColPosition - 2 >= 0 && matrix[horseRowPosition + 1][horseColPosition - 2] == 0){
- matrix[horseRowPosition + 1][horseColPosition - 2] = count;
- horseRowPosition += 1;
- horseColPosition -= 2;
- } else if(horseRowPosition + 1 < n && horseColPosition + 2 < n && matrix[horseRowPosition + 1][horseColPosition + 2] == 0){
- matrix[horseRowPosition + 1][horseColPosition + 2] = count;
- horseRowPosition += 1;
- horseColPosition += 2;
- } else if(horseRowPosition + 2 < n && horseColPosition - 1 >= 0 && matrix[horseRowPosition + 2][horseColPosition - 1] == 0){
- matrix[horseRowPosition + 2][horseColPosition - 1] = count;
- horseRowPosition += 2;
- horseColPosition -= 1;
- } else if(horseRowPosition + 2 < n && horseColPosition + 1 < n && matrix[horseRowPosition + 2][horseColPosition + 1] == 0){
- matrix[horseRowPosition + 2][horseColPosition + 1] = count;
- horseRowPosition += 2;
- horseColPosition += 1;
- } else{
- for (int i = 0; i < matrix.length; i++) {
- boolean newPositionFound = false;
- for (int j = 0; j < matrix[i].length; j++) {
- if (matrix[i][j] == 0) {
- horseRowPosition = i;
- horseColPosition = j;
- newPositionFound = true;
- matrix[i][j] = count;
- break;
- }
- }
- if (newPositionFound) break;
- }
- }
- count++;
- }
- for (int row = 0; row < matrix.length; row++) {
- for (int col = 0; col < matrix[row].length; col++) {
- System.out.print(matrix[row][col] + " ");
- }
- System.out.println();
- }
- //i -= 2, j -= 1
- //i -= 2, j += 1
- //i -= 1, j -= 2
- //i -= 1, j += 2
- //i += 1. j -= 2
- //i += 1, j += 2
- //i += 2, j -= 1
- //i += 2, j += 1
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment