views:

1402

answers:

3

I'm trying to fill an array in spiral order. So far, I can print the array in spiral order, but is there a way to modify the array so that i can fill it in spiral order and then just print the array? I'd like it to go in decreasing order like a countdown. Please help!

public class Spiral {
  public static void main(int m, int n) { 

    // create m by n array of integers 1 through m*n
    int[][] values = new int[m][n];
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            values[i][j] = 1 + (m*n)*i + j;

    // spiral
    for (int i = (m*n)-1, j = 0; i > 0; i--, j++) {
          for (int k = j; k < i; k++) System.out.println(values[j][k]);
          for (int k = j; k < i; k++) System.out.println(values[k][i]);
          for (int k = i; k > j; k--) System.out.println(values[i][k]);
          for (int k = i; k > j; k--) System.out.println(values[k][j]);
    }
  }
}
A: 

If you've figured out code to do the reads (for printing), then surely you can just modify that to do writes instead, using the same logic?

If you want each cell in the matrix to contain its "sequential number", counting backwards, something like this ought to work, assuming your access logic is correct:

for (int i = (m*n)-1, j = 0, index = m * n; i > 0; i--, j++) {
      for (int k = j; k < i; k++) values[j][j] = index--;
      for (int k = j; k < i; k++) values[k][i] = index--;
      for (int k = i; k > j; k--) values[i][k] = index--;
      for (int k = i; k > j; k--) values[k][j] = index--;
}
unwind
I keep getting an index out of bounds exception error on the 3rd for loop
A: 

Not the most efficient, but it should work: g is the array. I'm also using exceptions to control logic.

public static void spiralFill()
{
    x = (g.length-1)/2;
    y = (g[0].length-1)/2;

    try
    {
        while(true)
        {
             east();
             south();
             step++;
             west();
             north();
             step++;        
        }
    }
    catch(ArrayIndexOutOfBoundsException e)
    {

    }
}

public static void east()
{
for(int i = 0; i < step; i++)
{
        g[x][y] = count;
        count++;
    x++;
    }
}
public static void south()
{
    for(int i = 0; i < step; i++)
    {
        g[x][y] = count;
        count++;
        y--;
    }
}   
public static void west()
{
    for(int i = 0; i < step; i++)
    {
         g[x][y] = count;
         count++;
         x--;
    }
}   
public static void north()
{
   for(int i = 0; i < step; i++)
   {
       g[x][y] = count;
       count++;
       y++;
   }
}
CookieOfFortune
A: 

Here is some of the variations:

public class SpiralMatrix {

    private static int[][] createSpiralMatrix(int size) {
        int[][] matrix = new int[size][size];

        int row = 0, col = -1;
        int value = 1;

        boolean horizontal = true;
        boolean increasing = true;
        boolean finish = false;

        while(!finish) {

            finish = true;
            if (horizontal && increasing) {
                while(tryAndSet(matrix, row, col + 1, value)) {
                    finish = false;
                    col++;
                    value++;
                }
            } else if (horizontal && !increasing) {
                while(tryAndSet(matrix, row, col - 1, value)) {
                    finish = false;
                    col--;
                    value++;
                }
            } else if (!horizontal && increasing) {
                while(tryAndSet(matrix, row + 1, col, value)) {
                    finish = false;
                    row++;
                    value++;
                }
            } else {
                while(tryAndSet(matrix, row - 1, col, value)) {
                    finish = false;
                    row--;
                    value++;
                }
            }

            if (!horizontal) {
                increasing = !increasing;
            }
            horizontal = !horizontal;
        }

        return matrix;
    }


    private static boolean tryAndSet(int[][] matrix, int row, int col, int value) {
        if (row < 0 || col < 0 || row >= matrix.length || col >= matrix[row].length || matrix[row][col] != 0) {
            return false;
        }
        matrix[row][col] = value;
        return true;
    }

    private static void printMatrix(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix[i].length; j++) {
                System.out.print("\t" + matrix[i][j]);
            }
            System.out.println();
        }
    }


    public static void main(String[] args) {
        try {
            int[][] spiralMatrix = createSpiralMatrix(40);
            printMatrix(spiralMatrix);
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }
djiva