2

I am trying to print a mxn size matrix in a spiral manner. I tried to work this code through Java and I was able to print the spiral correctly if rows = columns. For unequal rows and columns I tend to have repetitions in printing. So it prints all the way to the center correctly but then it repeats one or more elements. I tried debugging but could not reach far. Can anyone help me with this? Here is the code I have got so far.

public class SpiralMatrix {
public static void main (String args[]){
    int[][] mat = { {1, 2,  3,  4,  5},
                    {6, 7,  8,  9,  10},
                    {11,12, 13, 14, 15}};
    int i,n=3,m=5,r=m-1,q=n-1;
    while(q>0 && r>0){
        for(i=m-1-r;i<=r;i++){
            System.out.println(mat[m-1-r][i]);
        }
        for(i=n-1-q+1;i<=q;i++){
            System.out.println(mat[i][r]);
        }
        for(i=r-1;i>=m-1-r;i--){
            System.out.println(mat[q][i]);
        }
        for(i=q-1;i>=n-1-q+1;i--){
            System.out.println(mat[i][n-1-q]);
        }
        q--;
        r--;
    }
}
}

The output of this is as follows:

1
2
3
4
5
10
15
14
13
12
11
6
7
8
9
8
7

So after reaching the middle value 9 it tracks back to 8 and 7. PS: This is not a homework, I am brushing up my skills for interviews. And please go easy on me, I am a life long learner! Thanks! I will appreciate your help.

Sid
  • 65
  • 8

3 Answers3

1

There are discussion on this in this question, you can see the many answers there.

And my answer there is this one.

It seems that you are just printing too much, and it seems to come from the third inner for loop (and also in other cases when the height is more than the width, possible from the fourth for loop).

That's because you're printing from right-to-left (the third inner for loop) although the height (q) is one. That's a mistake, since if the height is one, that line would have been printed by the first inner for loop. And so you need the condition to print only if the height is greater than one. So is the case for the fourth inner for loop.

public class SpiralMatrix {
    public static void main (String args[]){
        int[][] mat = { {1, 2,  3,  4,  5},
                        {6, 7,  8,  9,  10},
                        {11,12, 13, 14, 15}};
        int i,n=3,m=5,r=m-1,q=n-1;
        while(q>0 && r>0){
            for(i=m-1-r;i<=r;i++){
                System.out.println(mat[m-1-r][i]);
            }
            for(i=n-1-q+1;i<=q;i++){
                System.out.println(mat[i][r]);
            }
            for(i=r-1;i>=m-1-r && q>1;i--){
                System.out.println(mat[q][i]);
            }
            for(i=q-1;i>=n-1-q+1 && r>1;i--){
                System.out.println(mat[i][n-1-q]);
            }
            q--;
            r--;
        }
    }
}
Community
  • 1
  • 1
justhalf
  • 8,960
  • 3
  • 47
  • 74
0
public class SpiralMatrix {
    public static void main(String[] args) {
        int a[][] = {{1, 2, 3, 4,21,26},
                {5, 6, 7, 8,22,27},
                {9, 10, 11, 12,23,28},
                {13,14,15,16,24,29},
                {17,18,19,20,25,30},
                {31,32,33,34,35,36}};
        System.out.println(a[0].length);
        for(int i=0; i<6; i++) {
            for(int j=0;j<6;j++){
                System.out.print(a[i][j] + " , ");
            }
            System.out.println();
        }
        int m=6,n=6;

        for (int i=0;i<m;i++) { 
            int j=i;
            for(;j<n;j++){
                System.out.print(a[i][j] + " , ");
            }

            for(j=i+1;j<m;j++) {
                System.out.print(a[j][n-1] +" , ");
            }
            for(j=n-2;j>=i;j--){
                System.out.print(a[m-1][j] + " , ");
            }
            for(j=m-2;j>i;j--){
                System.out.print(a[j][i] + " , ");
            }
            m=m-1;
            n=n-1;          
        }
    }

}
//tried to simplfy the code to print a square matrix spiral way.
//complete code given, please post if feel any modification required.
iinpw
  • 1
  • 1
0

You need an if condition every time the robot turns 90 degrees!

    public static Stack<int> SpiralTraverse(int[,] arr)
    {
        Stack<int> trav = new Stack<int>();
        int m = arr.GetLength(0)-1; //end point (m,n)
        int n = arr.GetLength(1)-1;
        int k = 0; int l = 0; // starting point (k,l)
        int i; //index
        while( k <= m && l <= n)
        {
            for (i= l; i <= n; i++)
            {
                trav.Push((int)arr[k, i]);
            }
            k++;
            for (i = k; i <= m; i++)
            {
                trav.Push((int)arr[i, n]);
            }
            n--;
            if (k < m)
            {
                for (i= n;i>= l; i--)
                {
                    trav.Push((int)arr[m,i]);
                }
                m--;
            }
            if (l < n)
            {
                for (i= m;i>= k; i--)
                {
                    trav.Push((int)arr[i, l]);
                }
                l++;
            }
        }
        return trav;
    }
aerin
  • 20,607
  • 28
  • 102
  • 140