import java.util.*;
import static java.
lang.
Math.
*;
int r, c;
public Point(int r,
int c
) { this.r = r;
this.c = c;
}
@Override
public String toString
() { return r + ":" + c;
}
@Override
public boolean equals
(Object obj
) { if(!(obj
instanceof Point)) { return false; } return this.r == that.r && this.c == that.c;
}
@Override public int hashCode() {
return r * 17 + c * 31;
}
}
public class RecursiveMeiro {
int R = field.length;
for(int r=0; r<R; r++) {
res[r] = field[r][0];
}
return res;
}
int[] dr = { 0, 0, 1, -1 };
int[] dc = { -1, 1, 0, 0 };
String[] direction
= { "l",
"r",
"d",
"u" };
private String h
(List
<Point
> list
) { for(int i=list.size()-1; i>=1; i--) {
Point prev
= list.
get(i
-1); if(cur.r == prev.r) {
if(cur.c < prev.c) { res += "r"; }
else { res += "l"; }
} else {
if(cur.r < prev.r) { res += "d"; }
else { res += "u"; }
}
}
return res;
}
Set<Point> vis = new HashSet<Point>();
Map
<Point, Point
> prev
= new HashMap
<Point, Point
>(); Queue<Point> q = new LinkedList<Point>();
q.add(cur);
vis.add(cur);
while(!q.isEmpty()) {
cur = q.remove();
if(cur.equals(goal)) { break; }
else {
int r = cur.r;
int c = cur.c;
for(int i=0; i<4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if(nr < 0 || R <= nr || nc < 0 || C <= nc) { continue; }
if(css[nr][nc] == '#') { continue; }
if(vis.contains(node)) { continue; }
q.add(node);
vis.add(node);
prev.put(node, cur);
}
}
}
List<Point> directions = new LinkedList<Point>();
for(Point node
= goal
; node
!= null; node
= prev.
get(node
)) { directions.add(node);
}
return h(directions);
}
int R = f.length;
char[][] css = new char[R][];
for(int r=0; r<R; r++) {
css[r] = f[r].toCharArray();
}
int C = css[0].length;
Point start
= null, goal
= null; for(int r=0; r<R; r++) {
for(int c=0; c<C; c++) {
if(css
[r
][c
] == 'S') { start
= new Point(r, c
); } if(css
[r
][c
] == 'G') { goal
= new Point(r, c
); } }
}
return g(css, start, goal, R, C);
}
private void doIt() {
{ "#####" },
{ "#S..#" },
{ "###.#" },
{ "#G..#" },
{ "#####" },
};
String res1
= adventure
(sss1
);
{ "#####" },
{ "#..G#" },
{ "#...#" },
{ "#...#" },
{ "#...#" },
{ "#S..#" },
{ "#####" },
};
String res2
= adventure
(sss2
);
{ "####" },
{ "#S.#" },
{ "#.G#" },
{ "####" },
};
String res3
= adventure
(sss3
);
{ "#######" },
{ "#.....#" },
{ "#.#.#.#" },
{ "#.#S#.#" },
{ "#.###.#" },
{ "#.#G#.#" },
{ "#.#.#.#" },
{ "#.....#" },
{ "#######" },
};
String res4
= adventure
(sss4
);
{ "#######" },
{ "#.....#" },
{ "#.....#" },
{ "#..S..#" },
{ "#.....#" },
{ "#..G..#" },
{ "#.....#" },
{ "#.....#" },
{ "#######" },
};
String res5
= adventure
(sss5
);
{ "#######" },
{ "#.....#" },
{ "#.....#" },
{ "#.....#" },
{ "#.G...#" },
{ "#.....#" },
{ "#...S.#" },
{ "#.....#" },
{ "#######" },
};
String res6
= adventure
(sss6
); }
public static void main
(String[] args
) { new RecursiveMeiro().doIt();
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgc3RhdGljIGphdmEubGFuZy5NYXRoLio7CgpjbGFzcyBQb2ludCB7CiAgICBpbnQgciwgYzsKICAgIHB1YmxpYyBQb2ludChpbnQgciwgaW50IGMpIHsKICAgICAgICB0aGlzLnIgPSByOwogICAgICAgIHRoaXMuYyA9IGM7CiAgICB9CiAgICBAT3ZlcnJpZGUgcHVibGljIFN0cmluZyB0b1N0cmluZygpIHsKICAgICAgICByZXR1cm4gciArICI6IiArIGM7CiAgICB9CiAgICBAT3ZlcnJpZGUgcHVibGljIGJvb2xlYW4gZXF1YWxzKE9iamVjdCBvYmopIHsKICAgICAgICBpZighKG9iaiBpbnN0YW5jZW9mIFBvaW50KSkgeyByZXR1cm4gZmFsc2U7IH0KICAgICAgICBQb2ludCB0aGF0ID0gKFBvaW50KW9iajsKICAgICAgICByZXR1cm4gdGhpcy5yID09IHRoYXQuciAmJiB0aGlzLmMgPT0gdGhhdC5jOwogICAgfQogICAgQE92ZXJyaWRlIHB1YmxpYyBpbnQgaGFzaENvZGUoKSB7CiAgICAgICAgcmV0dXJuIHIgKiAxNyArIGMgKiAzMTsKICAgIH0KfQoKcHVibGljIGNsYXNzIFJlY3Vyc2l2ZU1laXJvIHsKICAgIHByaXZhdGUgU3RyaW5nW10gZihTdHJpbmdbXVtdIGZpZWxkKSB7CiAgICAgICAgaW50IFIgPSBmaWVsZC5sZW5ndGg7CiAgICAgICAgU3RyaW5nW10gcmVzID0gbmV3IFN0cmluZ1tSXTsKICAgICAgICBmb3IoaW50IHI9MDsgcjxSOyByKyspIHsKICAgICAgICAgICAgcmVzW3JdID0gZmllbGRbcl1bMF07CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICB9CgogICAgaW50W10gZHIgPSB7ICAwLCAwLCAxLCAtMSB9OwogICAgaW50W10gZGMgPSB7IC0xLCAxLCAwLCAgMCB9OwogICAgU3RyaW5nW10gZGlyZWN0aW9uID0geyAibCIsICJyIiwgImQiLCAidSIgfTsKCiAgICBwcml2YXRlIFN0cmluZyBoKExpc3Q8UG9pbnQ+IGxpc3QpIHsKICAgICAgICBTdHJpbmcgcmVzID0gIiI7CiAgICAgICAgZm9yKGludCBpPWxpc3Quc2l6ZSgpLTE7IGk+PTE7IGktLSkgewogICAgICAgICAgICBQb2ludCBjdXIgID0gbGlzdC5nZXQoaSk7CiAgICAgICAgICAgIFBvaW50IHByZXYgPSBsaXN0LmdldChpLTEpOwogICAgICAgICAgICBpZihjdXIuciA9PSBwcmV2LnIpIHsKICAgICAgICAgICAgICAgIGlmKGN1ci5jIDwgcHJldi5jKSB7IHJlcyArPSAiciI7IH0KICAgICAgICAgICAgICAgIGVsc2UgICAgICAgICAgICAgICB7IHJlcyArPSAibCI7IH0KICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIGlmKGN1ci5yIDwgcHJldi5yKSB7IHJlcyArPSAiZCI7IH0KICAgICAgICAgICAgICAgIGVsc2UgICAgICAgICAgICAgICB7IHJlcyArPSAidSI7IH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gcmVzOwogICAgfQoKCiAgICBwcml2YXRlIFN0cmluZyBnKGNoYXJbXVtdIGNzcywgUG9pbnQgc3RhcnQsIFBvaW50IGdvYWwsIGludCBSLCBpbnQgQykgewogICAgICAgIFNldDxQb2ludD4gdmlzID0gbmV3IEhhc2hTZXQ8UG9pbnQ+KCk7CiAgICAgICAgTWFwPFBvaW50LCBQb2ludD4gcHJldiAgPSBuZXcgSGFzaE1hcDxQb2ludCwgUG9pbnQ+KCk7CiAgICAgICAgUXVldWU8UG9pbnQ+IHEgPSBuZXcgTGlua2VkTGlzdDxQb2ludD4oKTsKICAgICAgICBQb2ludCBjdXIgPSBzdGFydDsKICAgICAgICBxLmFkZChjdXIpOwogICAgICAgIHZpcy5hZGQoY3VyKTsKICAgICAgICB3aGlsZSghcS5pc0VtcHR5KCkpIHsKICAgICAgICAgICAgY3VyID0gcS5yZW1vdmUoKTsKICAgICAgICAgICAgaWYoY3VyLmVxdWFscyhnb2FsKSkgeyBicmVhazsgfQogICAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICAgIGludCByID0gY3VyLnI7CiAgICAgICAgICAgICAgICBpbnQgYyA9IGN1ci5jOwogICAgICAgICAgICAgICAgZm9yKGludCBpPTA7IGk8NDsgaSsrKSB7CiAgICAgICAgICAgICAgICAgICAgaW50IG5yID0gciArIGRyW2ldOwogICAgICAgICAgICAgICAgICAgIGludCBuYyA9IGMgKyBkY1tpXTsKICAgICAgICAgICAgICAgICAgICBpZihuciA8IDAgfHwgUiA8PSBuciB8fCBuYyA8IDAgfHwgQyA8PSBuYykgeyBjb250aW51ZTsgfQogICAgICAgICAgICAgICAgICAgIGlmKGNzc1tucl1bbmNdID09ICcjJykgeyBjb250aW51ZTsgfQogICAgICAgICAgICAgICAgICAgIFBvaW50IG5vZGUgPSBuZXcgUG9pbnQobnIsIG5jKTsKICAgICAgICAgICAgICAgICAgICBpZih2aXMuY29udGFpbnMobm9kZSkpIHsgY29udGludWU7IH0KICAgICAgICAgICAgICAgICAgICBxLmFkZChub2RlKTsKICAgICAgICAgICAgICAgICAgICB2aXMuYWRkKG5vZGUpOwogICAgICAgICAgICAgICAgICAgIHByZXYucHV0KG5vZGUsIGN1cik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgTGlzdDxQb2ludD4gZGlyZWN0aW9ucyA9IG5ldyBMaW5rZWRMaXN0PFBvaW50PigpOwogICAgICAgIGZvcihQb2ludCBub2RlID0gZ29hbDsgbm9kZSAhPSBudWxsOyBub2RlID0gcHJldi5nZXQobm9kZSkpIHsKICAgICAgICAgICAgZGlyZWN0aW9ucy5hZGQobm9kZSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiBoKGRpcmVjdGlvbnMpOwogICAgfQoKICAgIHB1YmxpYyBTdHJpbmcgYWR2ZW50dXJlKFN0cmluZ1tdW10gZmllbGQpIHsKICAgICAgICBTdHJpbmdbXSBmID0gZihmaWVsZCk7CiAgICAgICAgaW50IFIgPSBmLmxlbmd0aDsKICAgICAgICBjaGFyW11bXSBjc3MgPSBuZXcgY2hhcltSXVtdOwogICAgICAgIGZvcihpbnQgcj0wOyByPFI7IHIrKykgewogICAgICAgICAgICBjc3Nbcl0gPSBmW3JdLnRvQ2hhckFycmF5KCk7CiAgICAgICAgfQogICAgICAgIGludCBDID0gY3NzWzBdLmxlbmd0aDsKICAgICAgICBQb2ludCBzdGFydCA9IG51bGwsIGdvYWwgPSBudWxsOwogICAgICAgIGZvcihpbnQgcj0wOyByPFI7IHIrKykgewogICAgICAgICAgICBmb3IoaW50IGM9MDsgYzxDOyBjKyspIHsKICAgICAgICAgICAgICAgIGlmKGNzc1tyXVtjXSA9PSAnUycpIHsgc3RhcnQgPSBuZXcgUG9pbnQociwgYyk7IH0KICAgICAgICAgICAgICAgIGlmKGNzc1tyXVtjXSA9PSAnRycpIHsgZ29hbCAgPSBuZXcgUG9pbnQociwgYyk7IH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gZyhjc3MsIHN0YXJ0LCBnb2FsLCBSLCBDKTsKICAgIH0KCiAgICBwcml2YXRlIHZvaWQgZG9JdCgpIHsKICAgICAgICBTdHJpbmdbXVtdIHNzczEgPSB7CiAgICAgICAgICAgIHsgIiMjIyMjIiB9LAogICAgICAgICAgICB7ICIjUy4uIyIgfSwKICAgICAgICAgICAgeyAiIyMjLiMiIH0sCiAgICAgICAgICAgIHsgIiNHLi4jIiB9LAogICAgICAgICAgICB7ICIjIyMjIyIgfSwKICAgICAgICB9OwogICAgICAgIFN0cmluZyByZXMxID0gYWR2ZW50dXJlKHNzczEpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihyZXMxKTsKCiAgICAgICAgU3RyaW5nW11bXSBzc3MyID0gewogICAgICAgICAgICB7ICIjIyMjIyIgfSwKICAgICAgICAgICAgeyAiIy4uRyMiIH0sCiAgICAgICAgICAgIHsgIiMuLi4jIiB9LAogICAgICAgICAgICB7ICIjLi4uIyIgfSwKICAgICAgICAgICAgeyAiIy4uLiMiIH0sCiAgICAgICAgICAgIHsgIiNTLi4jIiB9LAogICAgICAgICAgICB7ICIjIyMjIyIgfSwKICAgICAgICB9OwogICAgICAgIFN0cmluZyByZXMyID0gYWR2ZW50dXJlKHNzczIpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihyZXMyKTsKCiAgICAgICAgU3RyaW5nW11bXSBzc3MzID0gewogICAgICAgICAgICB7ICIjIyMjIiB9LAogICAgICAgICAgICB7ICIjUy4jIiB9LAogICAgICAgICAgICB7ICIjLkcjIiB9LAogICAgICAgICAgICB7ICIjIyMjIiB9LAogICAgICAgIH07CiAgICAgICAgU3RyaW5nIHJlczMgPSBhZHZlbnR1cmUoc3NzMyk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHJlczMpOwoKICAgICAgICBTdHJpbmdbXVtdIHNzczQgPSB7CiAgICAgICAgICAgIHsgIiMjIyMjIyMiIH0sCiAgICAgICAgICAgIHsgIiMuLi4uLiMiIH0sCiAgICAgICAgICAgIHsgIiMuIy4jLiMiIH0sCiAgICAgICAgICAgIHsgIiMuI1MjLiMiIH0sCiAgICAgICAgICAgIHsgIiMuIyMjLiMiIH0sCiAgICAgICAgICAgIHsgIiMuI0cjLiMiIH0sCiAgICAgICAgICAgIHsgIiMuIy4jLiMiIH0sCiAgICAgICAgICAgIHsgIiMuLi4uLiMiIH0sCiAgICAgICAgICAgIHsgIiMjIyMjIyMiIH0sCiAgICAgICAgfTsKICAgICAgICBTdHJpbmcgcmVzNCA9IGFkdmVudHVyZShzc3M0KTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocmVzNCk7CgogICAgICAgIFN0cmluZ1tdW10gc3NzNSA9IHsKICAgICAgICAgICAgeyAiIyMjIyMjIyIgfSwKICAgICAgICAgICAgeyAiIy4uLi4uIyIgfSwKICAgICAgICAgICAgeyAiIy4uLi4uIyIgfSwKICAgICAgICAgICAgeyAiIy4uUy4uIyIgfSwKICAgICAgICAgICAgeyAiIy4uLi4uIyIgfSwKICAgICAgICAgICAgeyAiIy4uRy4uIyIgfSwKICAgICAgICAgICAgeyAiIy4uLi4uIyIgfSwKICAgICAgICAgICAgeyAiIy4uLi4uIyIgfSwKICAgICAgICAgICAgeyAiIyMjIyMjIyIgfSwKICAgICAgICB9OwogICAgICAgIFN0cmluZyByZXM1ID0gYWR2ZW50dXJlKHNzczUpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihyZXM1KTsKCiAgICAgICAgU3RyaW5nW11bXSBzc3M2ID0gewogICAgICAgICAgICB7ICIjIyMjIyMjIiB9LAogICAgICAgICAgICB7ICIjLi4uLi4jIiB9LAogICAgICAgICAgICB7ICIjLi4uLi4jIiB9LAogICAgICAgICAgICB7ICIjLi4uLi4jIiB9LAogICAgICAgICAgICB7ICIjLkcuLi4jIiB9LAogICAgICAgICAgICB7ICIjLi4uLi4jIiB9LAogICAgICAgICAgICB7ICIjLi4uUy4jIiB9LAogICAgICAgICAgICB7ICIjLi4uLi4jIiB9LAogICAgICAgICAgICB7ICIjIyMjIyMjIiB9LAogICAgICAgIH07CiAgICAgICAgU3RyaW5nIHJlczYgPSBhZHZlbnR1cmUoc3NzNik7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHJlczYpOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBuZXcgUmVjdXJzaXZlTWVpcm8oKS5kb0l0KCk7CiAgICB9Cn0=