Escape TCI '02 Semifinals 2
Autor: animeworld | Publicado: 2015-02-16T14:48:00-08:00


Solution in Java for TopCoder problem Escape


http://community.topcoder.com/tc?module=ProblemDetail&amp;rd=4371&amp;pm=1170

Syntax highlighted used&nbsp;http://tohtml.com/java/

Other that may be useful for highlight code:&nbsp;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 &amp;&amp; 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 &lt; 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 &gt; x2) { int tmp = x1; x1 = x2; x2 = tmp; }
   if(y1 &gt; y2) { int tmp = y1; y1 = y2; y2 = tmp; }
   for(int x = x1; x &lt;= x2; ++x)
    for(int y = y1; y &lt;= y2; ++y)
     isHarmful[x][y] = true;
  }
  
  for(int i = 0; i &lt; 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 &gt; x2) { int tmp = x1; x1 = x2; x2 = tmp; }
   if(y1 &gt; y2) { int tmp = y1; y1 = y2; y2 = tmp; }
   for(int x = x1; x &lt;= x2; ++x)
    for(int y = y1; y &lt;= y2; ++y)
     isDeadly[x][y] = true;
  }
  
  LinkedList&lt;P&gt; list = new LinkedList&lt;&gt;();
  list.add(new P(0,0,0));
  
  int ans = Integer.MAX_VALUE;
  while(!list.isEmpty())
  {
   P p = list.pop();
   if(p.x == 500 &amp;&amp; p.y == 500) {
    ans = (p.damage &lt; ans) ? p.damage : ans;
    continue;
   }
   
   // left
   if(p.x - 1 &gt;= 0 &amp;&amp; !isVisited[p.x-1][p.y] &amp;&amp; !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 &lt;= 500 &amp;&amp; !isVisited[p.x+1][p.y] &amp;&amp; !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 &lt;= 500 &amp;&amp; !isVisited[p.x][p.y+1] &amp;&amp; !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 &gt;= 0 &amp;&amp; !isVisited[p.x][p.y-1] &amp;&amp; !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 253 134",
 "0 308 453 55",
 "169 28 313 498",
 "103 247 165 376",
 "264 287 363 407",
 "185 255 110 415",
 "475 126 293 112",
 "285 200 66 484",
 "60 178 461 301",
 "347 352 470 479",
 "433 130 383 370",
 "405 378 117 377",
 "403 324 369 133",
 "12 63 174 309",
 "181 0 356 56",
 "473 380 315 378"},
   new String[]{"250 384 355 234",
 "28 155 470 4",
 "333 405 12 456",
 "329 221 239 215",
 "334 20 429 338",
 "85 42 188 388",
 "219 187 12 111",
 "467 453 358 133",
 "472 172 257 288",
 "412 246 431 86",
 "335 22 448 47",
 "150 14 149 11",
 "224 136 466 328",
 "369 209 184 262",
 "274 488 425 195",
 "55 82 279 253",
 "153 201 65 228",
 "208 230 132 223",
 "369 305 397 267",
 "200 145 98 198",
 "422 67 252 479",
 "231 252 401 190",
 "312 20 0 350",
 "406 72 207 294",
 "488 329 338 326",
 "117 264 497 447",
 "491 341 139 438",
 "40 413 329 290",
 "148 245 53 386",
 "147 70 186 131",
 "300 407 71 183",
 "300 186 251 198",
 "178 67 487 77",
 "98 158 55 433",
 "167 231 253 90",
 "268 406 81 271",
 "312 161 387 153",
 "33 442 25 412",
 "56 69 177 428",
 "5 92 61 247"}));
 }
}