Advertisement
Guest User

Untitled

a guest
May 15th, 2016
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SpiralMatrix {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         int n = sc.nextInt();
  7.         int[][] matrix = new int[n][n];
  8.         int num = 1;
  9.         int row = 0;
  10.         int col = 0;
  11.         while (num <= n * n) {
  12.  
  13.             // GO RIGHT
  14.             for (; col < n; col++) {
  15.                 if (matrix[row][col] == 0) {
  16.                     matrix[row][col] = num++;
  17.                 } else {
  18.                     col--;
  19.                     break;
  20.                 }
  21.             }
  22.  
  23.             // GO DOWN
  24.             if (col >= n) {
  25.                 col--;
  26.             }
  27.             if (col < 0) {
  28.                 col = 0;
  29.             }
  30.  
  31.             row++;
  32.             for (; row < n; row++) {
  33.                 if (matrix[row][col] == 0) {
  34.                     matrix[row][col] = num++;
  35.                 } else {
  36.                     row--;
  37.                     break;
  38.                 }
  39.             }
  40.  
  41.             // GO LEFT
  42.             if (row < 0) {
  43.                 row = 0;
  44.             }
  45.  
  46.             if (row >= n) {
  47.                 row--;
  48.             }
  49.             col--;
  50.             for (; col >= 0; col--) {
  51.                 if (matrix[row][col] == 0) {
  52.                     matrix[row][col] = num++;
  53.                 } else {
  54.                     col++;
  55.                     break;
  56.                 }
  57.             }
  58.  
  59.             // GO UP
  60.  
  61.             if (col < 0) {
  62.                 col = 0;
  63.             }
  64.             row--;
  65.             if (row < 0) {
  66.                 row = 0;
  67.             }
  68.             for (; row >= 0; row--) {
  69.                 if (matrix[row][col] == 0) {
  70.                     matrix[row][col] = num++;
  71.                 } else {
  72.                     row++;
  73.                     break;
  74.                 }
  75.             }
  76.             col++;
  77.             if (col >= n) {
  78.                 col--;
  79.             }
  80.             if (row >= n) {
  81.                 row--;
  82.             }
  83.         }
  84.  
  85.         // PRINT MATRIX
  86.        
  87.         for (int i = 0; i < matrix.length; i++) {
  88.             for (int j = 0; j < matrix[i].length; j++) {
  89.                 System.out.print(matrix[i][j]);
  90.                 if (matrix[i][j] < 10) {
  91.                     System.out.print("  ");
  92.                 } else {
  93.                     System.out.print(" ");
  94.                 }
  95.             }
  96.             System.out.println();
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement