Go to diff to previous submission
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Grasshop { public static int maxX; public static int maxY; public static void rekurze(int[][] pole, int x, int y, int hodnota) { if (hodnota > 6) return; if (x<0) return; if (x>=maxX) return; if (y<0) return; if (y>=maxY) return; else { if ((pole[x][y] > hodnota) || (pole[x][y] == 0)) { pole[x][y] = hodnota; rekurze(pole, x+2, y+1, hodnota+1); rekurze(pole, x-2, y+1, hodnota+1); rekurze(pole, x+2, y-1, hodnota+1); rekurze(pole, x-2, y-1, hodnota+1); rekurze(pole, x+1, y+2, hodnota+1); rekurze(pole, x-1, y+2, hodnota+1); rekurze(pole, x+1, y-2, hodnota+1); rekurze(pole, x-1, y-2, hodnota+1); } } } boolean prvni = true; while (true) { try { String linka; linka = bfr.readLine(); if (linka == null) break; if (ln.length < 6) break; prvni = false; if ((sX == cX) && (sY == cY)) { continue; } if ((maxX < 2) || (maxY < 2)) { continue; } int[][] pole = new int[maxX][maxY]; rekurze(pole, cX, cY, 0); pole[cX][cY] = 9; //pole[sX][sY] = 7; /* for(int i=0; i<maxY; i++) { for(int j=0; j<maxX; j++) { System.out.print(pole[j][i]+" "); } System.out.println(); } */ int pocetSkoku = 0; while (pole[sX][sY] == 0) { pocetSkoku++; pole[sX][sY] = 8; int rozX = cX-sX; int rozY = cY-sY; break; } int skX, skY; if (rozX > 0) skX = 2; else skX = -2; if (rozY == 0) { if ((sY-1) < 0) skY = 1; else skY = -1; } else { if (rozY > 0) skY = 1; else skY = -1; } sX += skX; sY += skY; } else { int skX, skY; if (rozY > 0) skY = 2; else skY = -2; if (rozX == 0) { if ((sX-1) < 0) skX = 1; else skX = -1; } else { if (rozX > 0) skX = 1; else skX = -1; } sX += skX; sY += skY; } } } else { pocetSkoku += pole[sX][sY]; } /* System.out.println(); for(int i=0; i<maxY; i++) { for(int j=0; j<maxX; j++) { System.out.print(pole[j][i]+" "); } System.out.println(); } */ break; } } } }
--- c4.s1158.cteam113.grasshop.java.0.Grasshop.java +++ c4.s1253.cteam113.grasshop.java.0.Grasshop.java @@ -11,5 +11,5 @@ public static void rekurze(int[][] pole, int x, int y, int hodnota) { - if (hodnota >= 5) return; + if (hodnota > 6) return; if (x<0) return; if (x>=maxX) return; @@ -34,6 +34,6 @@ public static void main(String[] args) { + boolean prvni = true; while (true) { - try { String linka; @@ -42,4 +42,7 @@ String ln[] = linka.split(" "); if (ln.length < 6) break; + + if (!prvni) System.out.println(); + prvni = false; maxX = Integer.parseInt(ln[0]); @@ -51,10 +54,10 @@ if ((sX == cX) && (sY == cY)) { - System.out.println("0"); + System.out.print("0"); continue; } if ((maxX < 2) || (maxY < 2)) { - System.out.println("impossible"); + System.out.print("impossible"); continue; } @@ -69,5 +72,5 @@ for(int i=0; i<maxY; i++) { for(int j=0; j<maxX; j++) { - System.out.print(pole[j][i]); + System.out.print(pole[j][i]+" "); } System.out.println(); @@ -78,8 +81,9 @@ while (pole[sX][sY] == 0) { pocetSkoku++; + pole[sX][sY] = 8; int rozX = cX-sX; int rozY = cY-sY; - if (Math.abs(rozX) + Math.abs(rozY) < 5) { + if (Math.abs(rozX) + Math.abs(rozY) < 4) { pocetSkoku = Integer.MAX_VALUE; break; @@ -124,13 +128,14 @@ if (pocetSkoku == Integer.MAX_VALUE) { - System.out.println("impossible"); + System.out.print("impossible"); } else { pocetSkoku += pole[sX][sY]; - System.out.println(pocetSkoku); + System.out.print(pocetSkoku); } /* + System.out.println(); for(int i=0; i<maxY; i++) { for(int j=0; j<maxX; j++) { - System.out.print(pole[j][i]); + System.out.print(pole[j][i]+" "); } System.out.println(); @@ -140,6 +145,6 @@ break; } + } - } }