@Test public final void testGRID() { String actual = Matrix.GRID(2, 2); Assert.assertEquals("L",actual); actual = Matrix.GRID(3, 3); Assert.assertEquals("R",actual); }
static String GRID(int N, int M) { String d = "R"; String p = d; int[][] a = new int[N][M]; int c = 1, x = 0, y = 0, n = 0; while (c <= N * M) { if (a[x][y] == 0) { a[x][y] = c++; } else { n++; n = n % 4; } switch (n) { case 0: if (y < M - 1 && a[x][y + 1] < 1) y++; else { n = 1; p = d; d = "D"; x++; } break; case 1: if (x < N - 1 && a[x + 1][y] < 1) x++; else { y--; n = 2; p = d; d = "L"; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; n = 3; p = d; d = "U"; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; n = 0; p = d; d = "R"; } break; default: break; } } return p; }
Matrix { static String GRID(int N, int M) { String d = "R"; String p = d; int[][] a = new int[N][M]; int c = 1, x = 0, y = 0, n = 0; while (c <= N * M) { if (a[x][y] == 0) { a[x][y] = c++; } else { n++; n = n % 4; } switch (n) { case 0: if (y < M - 1 && a[x][y + 1] < 1) y++; else { n = 1; p = d; d = "D"; x++; } break; case 1: if (x < N - 1 && a[x + 1][y] < 1) x++; else { y--; n = 2; p = d; d = "L"; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; n = 3; p = d; d = "U"; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; n = 0; p = d; d = "R"; } break; default: break; } } return p; } }
Matrix { static String GRID(int N, int M) { String d = "R"; String p = d; int[][] a = new int[N][M]; int c = 1, x = 0, y = 0, n = 0; while (c <= N * M) { if (a[x][y] == 0) { a[x][y] = c++; } else { n++; n = n % 4; } switch (n) { case 0: if (y < M - 1 && a[x][y + 1] < 1) y++; else { n = 1; p = d; d = "D"; x++; } break; case 1: if (x < N - 1 && a[x + 1][y] < 1) x++; else { y--; n = 2; p = d; d = "L"; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; n = 3; p = d; d = "U"; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; n = 0; p = d; d = "R"; } break; default: break; } } return p; }  }
Matrix { static String GRID(int N, int M) { String d = "R"; String p = d; int[][] a = new int[N][M]; int c = 1, x = 0, y = 0, n = 0; while (c <= N * M) { if (a[x][y] == 0) { a[x][y] = c++; } else { n++; n = n % 4; } switch (n) { case 0: if (y < M - 1 && a[x][y + 1] < 1) y++; else { n = 1; p = d; d = "D"; x++; } break; case 1: if (x < N - 1 && a[x + 1][y] < 1) x++; else { y--; n = 2; p = d; d = "L"; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; n = 3; p = d; d = "U"; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; n = 0; p = d; d = "R"; } break; default: break; } } return p; }  static void main(String[] args); }
Matrix { static String GRID(int N, int M) { String d = "R"; String p = d; int[][] a = new int[N][M]; int c = 1, x = 0, y = 0, n = 0; while (c <= N * M) { if (a[x][y] == 0) { a[x][y] = c++; } else { n++; n = n % 4; } switch (n) { case 0: if (y < M - 1 && a[x][y + 1] < 1) y++; else { n = 1; p = d; d = "D"; x++; } break; case 1: if (x < N - 1 && a[x + 1][y] < 1) x++; else { y--; n = 2; p = d; d = "L"; } break; case 2: if (y > 0 && a[x][y - 1] < 1) { y--; } else { x--; n = 3; p = d; d = "U"; } break; case 3: if (x > 0 && a[x - 1][y] < 1) { x--; } else { y++; n = 0; p = d; d = "R"; } break; default: break; } } return p; }  static void main(String[] args);  }