Go to diff to previous submission
import java.util.Scanner; import java.util.List; import java.util.LinkedList; public class Grasshop { static int x; static int y; static int gx; static int gy; static int lx; static int ly; while (sc.hasNextInt()) { x = sc.nextInt(); y = sc.nextInt(); gx = sc.nextInt() - 1; gy = sc.nextInt() - 1; lx = sc.nextInt() - 1; ly = sc.nextInt() - 1; List<Position> list = new LinkedList<Position>(); int[][] map = new int[x][y]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { } } map[gx][gy] = 0; Position p; while (true) { p = list.get(0); jump(map, p.x, p.y, list); list.remove(p); if (list.isEmpty()) { break; } break; } /* if (map[lx][ly] < Integer.MAX_VALUE) { change = false; System.out.println(map[lx][ly]); } for (int j = 0; j < y; j++) { for (int i = 0; i < x; i++) { if (map[i][j] == Integer.MAX_VALUE) { System.out.print(" - "); } else { System.out.print(map[i][j] + " "); } } System.out.println(); } System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); */ } } } } } public static void jump(int[][] map, int a, int b, List<Position> list) { int dx = 0; int dy = 0; //System.out.println("Solving..... " + a + ", " + b) ; for (int k=0; k < 8; k++) { if (k == 0) {dx = -2; dy = -1;} if (k == 1) {dx = -2; dy = 1;} if (k == 2) {dx = -1; dy = -2;} if (k == 3) {dx = -1; dy = 2;} if (k == 4) {dx = 1; dy = -2;} if (k == 5) {dx = 1; dy = 2;} if (k == 6) {dx = 2; dy = -1;} if (k == 7) {dx = 2; dy = 1;} int newX = a + dx; int newY = b + dy; if (isValid(newX, newY)) { if (map[a][b] + 1 < map[newX][newY]) { map[newX][newY] = map[a][b] + 1; //System.out.println(a + ", " + b + " changed ") ; } } } } public static boolean isValid(int a, int b) { boolean valid = (a >= 0 && a < x && b >=0 && b < y); //System.out.println(a + ", " + b + " : " + valid); return valid; } } int x; int y; this.x = x; this.y = y; } }
--- c4.s785.cteam094.grasshop.java.0.Grasshop.java +++ c4.s988.cteam094.grasshop.java.0.Grasshop.java @@ -1,3 +1,5 @@ import java.util.Scanner; +import java.util.List; +import java.util.LinkedList; public class Grasshop { @@ -21,4 +23,6 @@ ly = sc.nextInt() - 1; + List<Position> list = new LinkedList<Position>(); + int[][] map = new int[x][y]; @@ -29,14 +33,27 @@ } map[gx][gy] = 0; + list.add(new Position(gx, gy)); + + Position p; + + - boolean change = true; - while (change) { - change = false; - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { - change = jump(map, i, j) || change; + while (true) { + + + + p = list.get(0); + + + jump(map, p.x, p.y, list); - } + list.remove(p); + + if (list.isEmpty()) { + break; + } + if (map[lx][ly] < Integer.MAX_VALUE) { + break; } @@ -76,16 +93,13 @@ } - public static boolean jump(int[][] map, int a, int b) { + public static void jump(int[][] map, int a, int b, List<Position> list) { int dx = 0; int dy = 0; - boolean change = false; + //System.out.println("Solving..... " + a + ", " + b) ; - if (map[a][b] == Integer.MAX_VALUE) { - return false; - - } + @@ -108,5 +122,6 @@ map[newX][newY] = map[a][b] + 1; //System.out.println(a + ", " + b + " changed ") ; - change = true; + + list.add(new Position(newX, newY)); } } @@ -115,5 +130,5 @@ } - return change; + } @@ -126,5 +141,17 @@ return valid; } -} +} + +class Position { + int x; + int y; + + Position(int x, int y) { + this.x = x; + this.y = y; + } + + +}