Go to diff to previous submission
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.LinkedList; import java.util.List; import java.util.Scanner; /** * * @author cteam041 */ public class grasshop { int R, C, Gr, Gc, Lr, Lc; while (sc.hasNextInt()) { List<Pos> q = new LinkedList<Pos>(); R = sc.nextInt(); C = sc.nextInt(); Gr = sc.nextInt()-1; Gc = sc.nextInt()-1; Lr = sc.nextInt()-1; Lc = sc.nextInt()-1; Pos start = new Pos(); start.r = Gr; start.c = Gc; start.iter = 1; q.add(start); int[][] pole = new int[C][R]; pole[Gc][Gr] = 1; int iter = 1; boolean found = false; while (q.size() > 0) { Pos k = q.get(0); q.remove(0); iter = k.iter; if (Lr == k.r && Lc == k.c) { found = true; break; } int i = k.c; int j = k.r; int x = i + 2; int y = j + 1; if (x >= 0 && x < C && y >= 0 && y < R) { if (pole[x][y] == 0) { Pos n = new Pos(); n.c = x; n.r = y; n.iter = iter+1; q.add(n); pole[x][y]=n.iter; } } x = i - 2; y = j + 1; if (x >= 0 && x < C && y >= 0 && y < R) { if (pole[x][y] == 0) { Pos n = new Pos(); n.c = x; n.r = y; n.iter = iter+1; q.add(n); pole[x][y]=n.iter; } } x = i - 2; y = j - 1; if (x >= 0 && x < C && y >= 0 && y < R) { if (pole[x][y] == 0) { Pos n = new Pos(); n.c = x; n.r = y; n.iter = iter+1; q.add(n); pole[x][y]=n.iter; } } x = i + 2; y = j - 1; if (x >= 0 && x < C && y >= 0 && y < R) { if (pole[x][y] == 0) { Pos n = new Pos(); n.c = x; n.r = y; n.iter = iter+1; q.add(n); pole[x][y]=n.iter; } } //opacnefound?:" x = i + 1; y = j + 2; if (x >= 0 && x < C && y >= 0 && y < R) { if (pole[x][y] == 0) { Pos n = new Pos(); n.c = x; n.r = y; n.iter = iter+1; q.add(n); pole[x][y]=n.iter; } } x = i - 1; y = j + 2; if (x >= 0 && x < C && y >= 0 && y < R) { if (pole[x][y] == 0) { Pos n = new Pos(); n.c = x; n.r = y; n.iter = iter+1; q.add(n); pole[x][y]=n.iter; } } x = i - 1; y = j - 2; if (x >= 0 && x < C && y >= 0 && y < R) { if (pole[x][y] == 0) { Pos n = new Pos(); n.c = x; n.r = y; n.iter = iter+1; q.add(n); pole[x][y]=n.iter; } } x = i + 1; y = j - 2; if (x >= 0 && x < C && y >= 0 && y < R) { if (pole[x][y] == 0) { Pos n = new Pos(); n.c = x; n.r = y; n.iter = iter+1; q.add(n); pole[x][y]=n.iter; } } } if (!found) } } private static class Pos { public int r, c, iter; } }