Solution in Java for TopCoder problem Escapehttp://community.topcoder.com/tc?module=ProblemDetail&rd=4371&pm=1170Syntax highlighted used http://tohtml.com/java/Other that may be useful for highlight code: http://hilite.me/import java.util.LinkedList;public class Escape{ class P { int x, y, damage; P(int x1, int y1, int damage1) { x = x1; y = y1; damage = damage1; } } public int lowest(String[] harmful, String[] deadly) { if(harmful.length == 0 && deadly.length == 0) return 0; boolean[][] isVisited = new boolean[501][501]; boolean[][] isHarmful = new boolean[501][501]; boolean[][] isDeadly = new boolean[501][501]; for(int i = 0; i < harmful.length; ++i) { String[] ps = harmful[i].split(" "); int x1 = Integer.parseInt(ps[0]); int y1 = Integer.parseInt(ps[1]); int x2 = Integer.parseInt(ps[2]); int y2 = Integer.parseInt(ps[3]); if(x1 > x2) { int tmp = x1; x1 = x2; x2 = tmp; } if(y1 > y2) { int tmp = y1; y1 = y2; y2 = tmp; } for(int x = x1; x <= x2; ++x) for(int y = y1; y <= y2; ++y) isHarmful[x][y] = true; } for(int i = 0; i < deadly.length; ++i) { String[] ps = deadly[i].split(" "); int x1 = Integer.parseInt(ps[0]); int y1 = Integer.parseInt(ps[1]); int x2 = Integer.parseInt(ps[2]); int y2 = Integer.parseInt(ps[3]); if(x1 > x2) { int tmp = x1; x1 = x2; x2 = tmp; } if(y1 > y2) { int tmp = y1; y1 = y2; y2 = tmp; } for(int x = x1; x <= x2; ++x) for(int y = y1; y <= y2; ++y) isDeadly[x][y] = true; } LinkedList<P> list = new LinkedList<>(); list.add(new P(0,0,0)); int ans = Integer.MAX_VALUE; while(!list.isEmpty()) { P p = list.pop(); if(p.x == 500 && p.y == 500) { ans = (p.damage < ans) ? p.damage : ans; continue; } // left if(p.x - 1 >= 0 && !isVisited[p.x-1][p.y] && !isDeadly[p.x-1][p.y]) { isVisited[p.x-1][p.y] = true; int d = p.damage; if(isHarmful[p.x-1][p.y]) ++d; list.add(new P(p.x-1,p.y, d)); } // right if(p.x + 1 <= 500 && !isVisited[p.x+1][p.y] && !isDeadly[p.x+1][p.y]) { isVisited[p.x+1][p.y] = true; int d = p.damage; if(isHarmful[p.x+1][p.y]) ++d; list.add(new P(p.x+1,p.y, d)); } // up if(p.y + 1 <= 500 && !isVisited[p.x][p.y+1] && !isDeadly[p.x][p.y+1]) { isVisited[p.x][p.y+1] = true; int d = p.damage; if(isHarmful[p.x][p.y+1]) ++d; list.add(new P(p.x,p.y+1, d)); } // down if(p.y - 1 >= 0 && !isVisited[p.x][p.y-1] && !isDeadly[p.x][p.y-1]) { isVisited[p.x][p.y-1] = true; int d = p.damage; if(isHarmful[p.x][p.y-1]) ++d; list.add(new P(p.x,p.y-1, d)); } } return (ans != Integer.MAX_VALUE) ? ans : -1; } public static void main(String[] args) { Escape e = new Escape(); System.out.println(e.lowest( new String[]{}, new String[]{})); System.out.println(e.lowest( new String[]{"500 0 0 500"}, new String[]{"0 0 0 0"})); System.out.println(e.lowest( new String[]{"0 0 250 250","250 250 500 500"}, new String[]{"0 251 249 500","251 0 500 249"})); System.out.println(e.lowest( new String[]{"0 0 250 250","250 250 500 500"}, new String[]{"0 250 250 500","250 0 500 250"})); System.out.println(e.lowest( new String[]{"468 209 456 32", "71 260 306 427", "420 90 424 492", "374 253 54 253", "319 334 152 431", "38 93 204 84", "246 0 434 263", "12 18 118 461", "215 462 44 317", "447 214 28 475", "3 89 38 125", "157 108 138 264", "363 17 333 387", "457 362 396 324", "95 27 374 175", "381 196 265 302", "105 255