Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- int[][] matrix = new int[n][n];
- int[] dirRows = {-2, -2, -1, -1, +1, +1, +2, +2};
- int[] dirCols = {-1, +1, -2, +2, -2, +2, -1, +1};
- int counter = 1;
- for (int row = 0; row < matrix.length; row++) {
- for (int col = 0; col < matrix[0].length; col++) {
- int currentRow = row;
- int currentCol = col;
- while (matrix[currentRow][currentCol] == 0) {
- matrix[currentRow][currentCol] = counter;
- counter++;
- for (int dir = 0; dir < dirRows.length; dir++) {
- int nextRow = currentRow + dirRows[dir];
- int nextCol = currentCol + dirCols[dir];
- if (nextRow < 0 || nextRow >= matrix.length || nextCol < 0 || nextCol >= matrix.length) {
- continue;
- }
- if (matrix[nextRow][nextCol] != 0) {
- continue;
- }
- currentRow = nextRow;
- currentCol = nextCol;
- break;
- }
- }
- }
- }
- for(int[] row : matrix){
- String result = "";
- for (int cell: row) {
- result += cell + " ";
- }
- System.out.println(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement