static void solve() { int r = in.nextInt(); int c = in.nextInt(); int sy = in.nextInt() - 1; int sx = in.nextInt() - 1; int gy = in.nextInt() - 1; int gx = in.nextInt() - 1; char[][] t = new char[r][c]; for (int i = 0; i < r; i++) { t[i] = in.next().toCharArray(); } ArrayDeque<int[]> que = new ArrayDeque<>(); BitSet visited = new BitSet(); que.add(new int[] {sy, sx, 0}); visited.set(sy * c + sx); int[] dx = {0, 1, 0, -1}; int[] dy = {1, 0, -1, 0}; int[][] dist = new int[r][c]; while (!que.isEmpty()) { int[] p = que.pollFirst(); int cy = p[0]; int cx = p[1]; int d = p[2]; dist[cy][cx] = d; for (int i = 0; i < 4; i++) { int ny = cy + dy[i]; int nx = cx + dx[i]; if (ny < 0 || nx < 0 || r <= ny || c <= nx) continue; if (visited.get(ny * c + nx) || t[ny][nx] == '#') continue; que.add(new int[] {ny, nx, d + 1}); visited.set(ny * c + nx); } } out.println(dist[gy][gx]); }
public static Expr parse(Tokens tokens) { int pos = tokens.getPosition(); Expr firstExpr = MultiplyExpr.parse(tokens); if (firstExpr == null) { tokens.setPosition(pos); return null; } List<Expr> multiplyExprs = new LinkedList<Expr>(); multiplyExprs.add(firstExpr); BitSet operators = new BitSet(); for (int i = 0; tokens.hasNext(); i++) { char operator = tokens.nextChar(); if (operator == '+' || operator == '-') { Expr nextExpr = MultiplyExpr.parse(tokens); if (nextExpr == null) { throw new QuerySyntaxException(tokens); } multiplyExprs.add(nextExpr); if (operator == '+') { operators.set(i); } } else { tokens.pushback(); break; } } return multiplyExprs.size() == 1 ? firstExpr : new AdditiveExpr(multiplyExprs, operators); }