Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.math.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import java.util.concurrent.*;
- import java.util.function.*;
- import java.util.regex.*;
- import java.util.stream.*;
- import static java.util.stream.Collectors.joining;
- import static java.util.stream.Collectors.toList;
- class Result {
- /*
- * Complete the 'knightlOnAChessboard' function below.
- *
- * The function is expected to return a 2D_INTEGER_ARRAY.
- * The function accepts INTEGER n as parameter.
- */
- public static List<List<Integer>> knightlOnAChessboard(int n) {
- // Write your code here
- List<List<Integer>> result = new ArrayList<>();
- //int[][] grid = new int[n][n];
- int[] start = new int[]{0,0};
- List<int[]> next = new ArrayList<>();
- next.add(start);
- for (int i = 1; i < n; i++) {
- List<Integer> mins = new ArrayList<>();
- for (int j = 1; j < n; j++) {
- if (i == j) {
- if ((n-1) % i == 0) {
- mins.add((n-1) / i);
- continue;
- } else if (i > (n-1) / 2) {
- mins.add(-1);
- continue;
- }
- }
- if (i > j) {
- mins.add(result.get(j-1).get(i-1));
- continue;
- }
- mins.add(minimumMove(next, i, j, new int[n][n], n));
- }
- result.add(mins);
- }
- //System.out.println(minimumMove(start, 1, 2, grid, n));
- return result;
- }
- public static int minimumMove(List<int[]> start, int xMove, int yMove, int[][] grid, int n) {
- int moves = 0;
- List<int[]> next;
- int subsequent;
- next = nextMove(start, xMove, yMove, grid, n);
- if (next != null) {
- moves++;
- if (containsEnd(next, n)) {
- return moves;
- }
- subsequent = minimumMove(next, xMove, yMove, grid, n);
- if (subsequent != -1) {
- return moves + subsequent;
- }
- }
- return -1;
- }
- public static List<int[]> nextMove(List<int[]> start, int xMove, int yMove, int[][] grid, int n) {
- List<int[]> nextMove = new ArrayList<>();
- int[] next;
- for (int[] pos : start) {
- next = upRight(pos, xMove, yMove, grid, n);
- if (next != null) {
- nextMove.add(next);
- }
- next = rightUp(pos, xMove, yMove, grid, n);
- if (next != null) {
- nextMove.add(next);
- }
- next = rightDown(pos, xMove, yMove, grid, n);
- if (next != null) {
- nextMove.add(next);
- }
- next = downRight(pos, xMove, yMove, grid, n);
- if (next != null) {
- nextMove.add(next);
- }
- next = downLeft(pos, xMove, yMove, grid, n);
- if (next != null) {
- nextMove.add(next);
- }
- next = leftDown(pos, xMove, yMove, grid, n);
- if (next != null) {
- nextMove.add(next);
- }
- next = leftUp(pos, xMove, yMove, grid, n);
- if (next != null) {
- nextMove.add(next);
- }
- next = upLeft(pos, xMove, yMove, grid, n);
- if (next != null) {
- nextMove.add(next);
- }
- }
- return nextMove.size() > 0 ? nextMove : null;
- }
- public static boolean containsEnd(List<int[]> next, int n) {
- for (int[] pos : next) {
- if (end(pos, n)) {
- return true;
- }
- }
- return false;
- }
- public static boolean end(int[] next, int n) {
- return next[0] == n-1 && next[1] == n-1;
- }
- public static int[] upRight(int[] start, int xMove, int yMove, int[][] grid, int n) {
- int xNew = start[0] - xMove;
- int yNew = start[1] + yMove;
- if (xNew < 0 || yNew >= n) {
- return null;
- }
- if (grid[xNew][yNew] != 0) {
- return null;
- }
- grid[xNew][yNew] = 1;
- return new int[]{xNew, yNew};
- }
- public static int[] rightUp(int[] start, int xMove, int yMove, int[][] grid, int n) {
- int xNew = start[0] - yMove;
- int yNew = start[1] + xMove;
- if (xNew < 0 || yNew >= n) {
- return null;
- }
- if (grid[xNew][yNew] != 0) {
- return null;
- }
- grid[xNew][yNew] = 1;
- return new int[]{xNew, yNew};
- }
- public static int[] rightDown(int[] start, int xMove, int yMove, int[][] grid, int n) {
- int xNew = start[0] + yMove;
- int yNew = start[1] + xMove;
- if (xNew >= n || yNew >= n) {
- return null;
- }
- if (grid[xNew][yNew] != 0) {
- return null;
- }
- grid[xNew][yNew] = 1;
- return new int[]{xNew, yNew};
- }
- public static int[] downRight(int[] start, int xMove, int yMove, int[][] grid, int n) {
- int xNew = start[0] + xMove;
- int yNew = start[1] + yMove;
- if (xNew >= n || yNew >= n) {
- return null;
- }
- if (grid[xNew][yNew] != 0) {
- return null;
- }
- grid[xNew][yNew] = 1;
- return new int[]{xNew, yNew};
- }
- public static int[] downLeft(int[] start, int xMove, int yMove, int[][] grid, int n) {
- int xNew = start[0] + xMove;
- int yNew = start[1] - yMove;
- if (yNew < 0 || xNew >= n) {
- return null;
- }
- if (grid[xNew][yNew] != 0) {
- return null;
- }
- grid[xNew][yNew] = 1;
- return new int[]{xNew, yNew};
- }
- public static int[] leftDown(int[] start, int xMove, int yMove, int[][] grid, int n) {
- int xNew = start[0] + yMove;
- int yNew = start[1] - xMove;
- if (yNew < 0 || xNew >= n) {
- return null;
- }
- if (grid[xNew][yNew] != 0) {
- return null;
- }
- grid[xNew][yNew] = 1;
- return new int[]{xNew, yNew};
- }
- public static int[] leftUp(int[] start, int xMove, int yMove, int[][] grid, int n) {
- int xNew = start[0] - yMove;
- int yNew = start[1] - xMove;
- if (xNew < 0 || yNew < 0) {
- return null;
- }
- if (grid[xNew][yNew] != 0) {
- return null;
- }
- grid[xNew][yNew] = 1;
- return new int[]{xNew, yNew};
- }
- public static int[] upLeft(int[] start, int xMove, int yMove, int[][] grid, int n) {
- int xNew = start[0] - xMove;
- int yNew = start[1] - yMove;
- if (xNew < 0 || yNew < 0) {
- return null;
- }
- if (grid[xNew][yNew] != 0) {
- return null;
- }
- grid[xNew][yNew] = 1;
- return new int[]{xNew, yNew};
- }
- }
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
- int n = Integer.parseInt(bufferedReader.readLine().trim());
- List<List<Integer>> result = Result.knightlOnAChessboard(n);
- result.stream()
- .map(
- r -> r.stream()
- .map(Object::toString)
- .collect(joining(" "))
- )
- .map(r -> r + "\n")
- .collect(toList())
- .forEach(e -> {
- try {
- bufferedWriter.write(e);
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
- });
- bufferedReader.close();
- bufferedWriter.close();
- }
- }
Add Comment
Please, Sign In to add comment