Monday, 2 November 2015

Spiral Value Assigned Matrix

Hi,


I got a question from my friend, if user gives input 6 or  any number it should show matrix in the form of normal and Spiral assigned values.

Matrix Size is: 6

Normal Matrix is:

    1    2    3    4    5    6
    7    8    9   10   11   12
   13   14   15   16   17   18
   19   20   21   22   23   24
   25   26   27   28   29   30
   31   32   33   34   35   36
Spiral Matrix is:

    1     2        3     4      5      6
   20    21    22    23    24     7
   19    32    33    34    25     8
   18    31    36    35    26     9
   17    30    29    28    27    10
   16    15    14    13    12    11

I had gone through the problem, got it can be solved in 4 recurring  steps.

 package newlearn;  
 import java.util.Scanner;  
 /**  
  *   
  * @author Ramakrishna Panni  
  *  
  */  
 public class SpiralMatrix {  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           try{  
                System.out.println("Enter matrix size to get spiral matrix");  
                Scanner sc = new Scanner(System.in);  
                String[] inter = sc.nextLine().trim().split(" ");  
                int size = Integer.parseInt(inter[0]);  
                System.out.println("Matrix Size is: "+size);  
                sc.close();  
                int higestValue = size*size;  
                int spiralMatrix[][] = new int[size][size];  
                int normalMatrix[][] = new int[size][size];  
                int normalValue = 0;  
                for(int i=0;i<size;i++)  
                {  
                     for(int j=0;j<size;j++)  
                     {  
                          normalMatrix[i][j] = ++normalValue;  
                          spiralMatrix[i][j] = 0;  
                     }  
                }  
                System.out.println("Normal Matrix is:\n");  
                for(int i=0;i<size;i++)  
                {  
                     for(int j=0;j<size;j++)  
                     {  
                          System.out.printf("%5d",normalMatrix[i][j]);  
                     }  
                     System.out.println("");  
                }  
                int lastValue=0;  
                int step = 1;  
                int step1Col = 0;  
                int step1Row = 0;  
                int step2Col = 0;  
                int step2Row = 0;  
                int step3Col = 0;  
                int step3Row = 0;  
                int step4Col = -1;  
                int step4Row = 0;  
                while (lastValue < higestValue) {  
                     step = 1;  
                     //this is the step where value assigned in Horizontal incrementing positional way  
                     if(step == 1)  
                     {  
                          step1Row = step4Row;  
                          step1Col = ++step4Col;  
                          for(int j=step1Col;j<size;j++)  
                          {  
                               if(spiralMatrix[step1Row][j] == 0)  
                               {  
                                    spiralMatrix[step1Row][j] = ++lastValue;  
                                    step1Col = j;  
                               }  
                               else{  
                                    break;  
                               }  
                          }  
                          step = ++step;  
                     }  
                     //this is the step where value assigned in Vertical incrementing positional way  
                      if (step == 2)  
                     {  
                          step2Col = step1Col;  
                          step2Row = step1Row+1;  
                          for(int i=step2Row;i<size;i++)  
                          {  
                               if(spiralMatrix[i][step1Col] == 0)  
                                    {  
                                         spiralMatrix[i][step1Col] = ++lastValue;  
                                         step2Row = i;  
                                    }  
                                    else{  
                                         break;  
                                    }  
                          }  
                          step = ++step;  
                     }  
                     //this is the step where value assigned in Horizontal decrementing positional way  
                      if(step==3)  
                     {  
                          step3Row = step2Row;  
                          step3Col = step2Col-1;  
                               for(int j=step3Col;j>=0;j--)  
                               {  
                                    if(spiralMatrix[step3Row][j] == 0)  
                                    {  
                                         step3Col = j;  
                                         spiralMatrix[step3Row][j] = ++lastValue;  
                                    }  
                                    else{  
                                         break;  
                                    }  
                               }  
                          step = ++step;  
                     }  
                     //this is the step where value assigned in Vertical incrementing decrementing positional way  
                     if(step==4)  
                     {  
                               step4Row = --step3Row;  
                               step4Col = step3Col;  
                               for(int i=step4Row;i>=0;i--)  
                               {  
                                    if(spiralMatrix[i][step4Col] == 0)  
                                    {  
                                         step4Row = i;  
                                         spiralMatrix[i][step4Col] = ++lastValue;  
                                    }  
                                    else{  
                                         break;  
                                    }  
                               }  
                          step = ++step;  
                     }  
                }  
                System.out.println("Spiral Matrix is:\n");  
                for( int i=0;i<size;i++)  
                {  
                     for( int j=0;j<size;j++)  
                     {  
                          System.out.printf("%5d ",spiralMatrix[i][j]);  
                     }  
                     System.out.println("");  
                }  
           }  
           catch(Exception e)  
           {  
                e.printStackTrace();  
                System.out.println("Something Went wrong............Please run again.....");  
           }  
      }  
 }  

1 comment:

  1. This solution does work for the problem, but I found it a lil lengthy and confusing.
    I updated you code and here it is.
    See if you find it simpler. :)

    package matrix;

    import java.util.Scanner;
    /**
    *
    * @author Ramakrishna Panni
    *
    * @updatedby Anupam Upadhyay
    *
    */
    public class anupamSpiral {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
    System.out.println("Enter matrix size to get spiral matrix");
    Scanner sc = new Scanner(System.in);
    String[] inter = sc.nextLine().trim().split(" ");
    int size = Integer.parseInt(inter[0]);
    System.out.println("Matrix Size is: "+size);
    sc.close();
    int highestValue = size*size;
    int spiralMatrix[][] = new int[size][size];
    int normalMatrix[][] = new int[size][size];
    int normalValue = 0;
    for(int i=0;i<size;i++)
    {
    for(int j=0;j<size;j++)
    {
    normalMatrix[i][j] = ++normalValue;
    spiralMatrix[i][j] = 0;
    }
    }
    System.out.println("Normal Matrix is:\n");
    for(int i=0;i<size;i++)
    {
    for(int j=0;j<size;j++)
    {
    System.out.printf("%5d",normalMatrix[i][j]);
    }
    System.out.println("");
    }

    int lastValue=0;
    int step = 1;
    int r = 0;
    int c = -1;

    while (lastValue < highestValue) {
    if(step == 1)
    {
    if(c+1 != size && spiralMatrix[r][c+1] == 0)
    {
    spiralMatrix[r][++c] = ++lastValue;
    }
    else{
    step++;
    }
    }
    else if(step == 2)
    {
    if(r+1 != size && spiralMatrix[r+1][c] == 0)
    {
    spiralMatrix[++r][c] = ++lastValue;
    }
    else{
    step++;
    }
    }

    else if(step == 3)
    {
    if(c-1 != -1 && spiralMatrix[r][c-1] == 0)
    {
    spiralMatrix[r][--c] = ++lastValue;
    }
    else{
    step++;
    }
    }

    else if(step == 4)
    {
    if(r-1 != -1 && spiralMatrix[r-1][c] == 0)
    {
    spiralMatrix[--r][c] = ++lastValue;
    }
    else{
    step = 1;
    }
    }
    }
    System.out.println("Spiral Matrix is:\n");
    for( int i=0;i<size;i++)
    {
    for( int j=0;j<size;j++)
    {
    System.out.printf("%5d ",spiralMatrix[i][j]);
    }
    System.out.println("");
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    System.out.println("Something Went wrong............Please run again.....");
    }
    }
    }

    ReplyDelete