Advertisement
kuruku

SpiralMatrix

Jul 19th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package spiralmatrix;
  2.  
  3. import java.util.Scanner;
  4. /*
  5.  * @author Georgi Machkanov
  6.  */
  7. public class SpiralMatrix {
  8.     public static void main(String[] args) {
  9.         Scanner input = new Scanner(System.in);
  10.         System.out.print("x = ");
  11.         int x = input.nextInt();
  12.         System.out.print("y = ");
  13.         int y = input.nextInt();
  14.  
  15.         System.out.println();
  16.         int[][] arrInt = new int[y][x];
  17.  
  18.         /* Generate matrix [x,y] */
  19.  
  20.         for (int row = 0; row < y; row++)
  21.         {
  22.             for (int col = 0; col < x; col++)
  23.             {
  24.                 arrInt[row][col] = x * row + col + 1;
  25.             }
  26.         }
  27.         /* Print matrix [x,y] */
  28.  
  29.         for (int a = 0; a < y; a++)
  30.         {
  31.             for (int b = 0; b < x; b++)
  32.             {
  33.                 System.out.format("%5d", arrInt[a][b]);
  34.             }
  35.             System.out.println();
  36.         }
  37.  
  38.         System.out.println();
  39.  
  40.         /* Print matrix in spiral form */
  41.  
  42.         byte direction = 0; /*0-right, 1-down, 2-left, 3-right*/
  43.         int row = 0, col = 0;
  44.         int matrixIndex = 0;  
  45.  
  46.         for (int k = 0; k < y; k++)
  47.         {
  48.             for (int c = 0; c < x; c++)
  49.             {
  50.  
  51.                 System.out.print(arrInt[row][col] + " ");
  52.                 if (direction == 0) //right
  53.                 {
  54.                     if (col + 1 + matrixIndex >= x)
  55.                     {
  56.                         direction = 1;
  57.                         row++;
  58.                     }
  59.                     else
  60.                     {
  61.                         col++;
  62.                     }
  63.                 }
  64.                 else if (direction == 1) //down
  65.                 {
  66.                     if (row + 1 + matrixIndex >= y)
  67.                     {
  68.                         direction = 2;
  69.                         col--;
  70.                     }
  71.                     else
  72.                     {
  73.                         row++;
  74.                     }
  75.                 }
  76.                 else if (direction == 2) //left
  77.                 {
  78.                     if (col - 1 - matrixIndex < 0)
  79.                     {
  80.                         direction = 3;
  81.                         row--;
  82.                     }
  83.                     else
  84.                     {
  85.                         col--;
  86.                     }
  87.                 }
  88.                 else if (direction == 3) //up
  89.                 {
  90.                     if (matrixIndex + 1 == row)
  91.                     {
  92.                         matrixIndex++;
  93.                     }
  94.                     if (row - 1 - matrixIndex < 0)
  95.                     {
  96.                         direction = 0;
  97.                         col++;
  98.                     }
  99.                     else
  100.                     {
  101.                         row--;
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.         System.out.println();
  107.         System.out.println();
  108.        
  109.     }
  110.    
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement