Wednesday, July 18, 2012

Wheeling Numbers in Arrays Extended


Implementation:
public class Wheeling {
      private int rows;
      private int cols;
      private int range;
      private int row;
      private int col;
      private int val;
      private int opr;
      private int[][] a;
      private boolean out;
      private int[] oprs;

      public Wheeling(int row, int col) {
            rows = row;
            cols = col;
            a = new int[row][col];
            range = row * col;
      }

      private void reset() {
            this.val = 0;
            this.row = 0;
            this.col = 0;
            this.opr = 0;
            for (int i = 0; i < rows; i++) {
                  for (int j = 0; j < cols; j++) {
                        a[i][j] = 0;
                  }
            }
      }

      public void wheel(boolean clockWise, boolean out) {
            reset();
            this.out = out;
            if (clockWise ^ out) {
                  oprs = new int[] { 1, 0, 3, 2 };
            } else {         
                  oprs = new int[] { 0, 1, 2, 3 };
            }          
            if (out) {
                  a[row][col] = ++val;
            } else {
                  a[row][col] = range + 1 - ++val;
            }
            while (val < range) {
                  if (move(opr)) {
                        continue;
                  } else {
                        ++opr;
                  }
            }
            display();
      }

      private void display() {
            for (int i = 0; i < rows; i++) {
                  System.out.println();
                  for (int j = 0; j < cols; j++) {
                        System.out.printf("%5d", a[i][j]);
                  }
            }
      }

      private boolean move(int opr) {
            int trow = row;
            int tcol = col;
            switch (oprs[opr % 4]) {
            case 0:
                  tcol = col + 1;
                  break;
            case 1:
                  trow = row + 1;
                  break;
            case 2:
                  tcol = col - 1;
                  break;
            default:
                  trow = row - 1;
                  break;
            }
            return assign(trow, tcol);
      }

      private boolean assign(int row, int col) {
            if (row == rows || row == -1 || col == cols || col == -1) {
                  return false;
            } else if (a[row][col] != 0) {
                  return false;
            } else {
                  if (out) {
                        a[row][col] = ++val;
                  } else {
                        a[row][col] = range + 1 - ++val;
                  }
                  this.row = row;
                  this.col = col;
                  return true;
            }
      }
}
Test:
public class WheelingTest {
      public static void main(String[] args) {
            Wheeling wheeling = new Wheeling(8, 8);
            wheeling.wheel(true, true);
      }
}

No comments:

Post a Comment