Advertisement
Guest User

Diagonal array filling

a guest
Dec 7th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1.  
  2. public class SA {
  3.  
  4.     /**
  5.      * Print out a square array with numbers filling the array diagonally (moving northeast, wrapping
  6.      * towards the top left of the array).
  7.      * See a diagram here: http://imgur.com/sLOfNNn
  8.      *
  9.      * Input:  
  10.      * dim = non-zero-based dimensions of the grid to return.
  11.      * startX, startY = a point within the grid, not zero-based; return zeroed-out grid if it's outside
  12.      * startCount = an integer to begin counting from
  13.      *
  14.      * Example:
  15.      * dim = 3
  16.      * startX, startY = 3,2
  17.      * startCount = 12
  18.      * [ 18, 17, 15 ]
  19.      * [ 16, 14, 12 ]
  20.      * [ 13, 20, 19 ]
  21.      */
  22.  
  23.     public static void main(String[] args) {
  24.  
  25.         int dim = 5;            // not zero-based
  26.         int startX = 2;         // not zero-based
  27.         int startY = 4;         // not zero-based
  28.         int startCount = 200;
  29.        
  30.         int currentCount = startCount;
  31.         int currX = startX-1;
  32.         int currY = startY-1;      
  33.        
  34.         int[][] grid = new int[dim][dim];
  35.        
  36.         // Check input for errors
  37.             // If dim <= 0
  38.         if (dim <=0) {
  39.             System.out.println("Error! Dimensions of the grid must be 1x1 or larger.");
  40.             System.exit(0);
  41.         }
  42.        
  43.         // 1x1 grid
  44.         if (dim == 1) {
  45.             System.out.println("Result:");
  46.             System.out.println(startCount);
  47.             System.exit(0);
  48.         }
  49.        
  50.             // If startX or startY are outside the grid
  51.         if ((startX > dim) || (startY > dim) || startX <0 || startY <0) {
  52.             System.out.println("Error! StartX and startY must be within the grid.");
  53.             System.exit(0);
  54.         }
  55.        
  56.         // Step through every element of the array     
  57.         while (currentCount < (startCount + (dim)*(dim) )) {
  58.             // insert the number in the current spot, and increment it
  59.             System.out.println("Inserting " + currentCount + " at X, Y = " + currX + ", " + currY);
  60.             grid[currX][currY] = currentCount++;
  61.             // now increment currX and currY
  62.             currX += 1;
  63.             currY -= 1;
  64.             // if either is outside the bounds of the grid, find a new currX & currY
  65.  
  66.             if ((currX == dim) || (currY < 0))
  67.             {
  68.                 // move the other way diagonally until either X or Y is off the grid
  69.                 while (currX!=-1 && currY != dim) {
  70.                     currX-=1;
  71.                     currY+=1;
  72.                 }
  73.             } else {
  74.                 continue;
  75.             }
  76.            
  77.             System.out.println("Need a new start point. We moved off the grid to: " + currX + ", " + currY);
  78.             // if we were in the ULH corner, swap to the BRH corner
  79.             if (currX == -1 && currY ==1) {
  80.                 currX = dim-1;
  81.                 currY = dim-1;             
  82.             } else {
  83.                 if (currY == dim) { // we were on the bottom row
  84.                     currY -=1;
  85.                     if (currX == -1) { // BLH corner case
  86.                         currX +=1;
  87.                         currY -=1;
  88.                         continue;
  89.                     } else {continue;}
  90.                 } // we were on the first column
  91.                 currX +=1;
  92.                 currY -=2;
  93.             }
  94.            
  95.            
  96.         }
  97.        
  98.         // Print out results
  99.         System.out.println("Result, with currentCount = " + currentCount + " :");
  100.         for (int y = 0; y<dim; y++) {
  101.             for (int x = 0; x<dim; x++) {
  102.                 System.out.print(grid[x][y]);
  103.                 if (x < dim-1) System.out.print(", ");
  104.             }
  105.             System.out.println();
  106.         }  
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement