import java.util.*;
import static java.
lang.
Math.
*;
int r, c, d;
public Point(int r,
int c
) { this.r = r;
this.c = c;
}
public Point(int r,
int c,
int d
) { this(r, c);
this.d = d;
}
}
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 };
private String bfs
(char[][] css
) { int R = css.length;
int C = css[0].length;
Queue<Point> q = new LinkedList<Point>();
int[][] iss = new int[R][C];
for(int r=0; r<R; r++) {
for(int c=0; c<C; c++) {
if(css[r][c] == '#') {
iss[r][c] = 987654321;
}
}
}
int sr = -1, sc = -1;
here: for(int r=0; r<R; r++) {
for(int c=0; c<C; c++) {
if(css[r][c] == 'S') {
q.
add(new Point(r, c,
0)); iss[r][c] = 0;
css[r][c] = '#';
sr = r;
sc = c;
break here;
}
}
}
int goal = -1;
while(!q.isEmpty()) {
int r = p.r, c = p.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; }
css[nr][nc] = '#';
q.
add(new Point(nr, nc, p.
d+1)); iss[nr][nc] = p.d+1;
goal = max(goal, p.d+1);
if(css[nr][nc] == 'G') { break; }
}
}
int cur = 0;
int r = sr;
int c = sc;
for( ; cur < goal ; ) {
if(iss[r][c] == cur) {
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(iss[nr][nc] == cur+1) {
if(i == 0) { res += "l"; }
else if(i == 1) { res += "r"; }
else if(i == 2) { res += "d"; }
else if(i == 3) { res += "u"; }
cur += 1;
r = nr;
c = nc;
}
}
}
}
return res;
}
int R = f.length;
char[][] css = new char[R][];
for(int r=0; r<R; r++) {
css[r] = f[r].toCharArray();
}
return bfs(css);
}
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
); }
public static void main
(String[] args
) { new RecursiveMeiro().doIt();
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgc3RhdGljIGphdmEubGFuZy5NYXRoLio7CgpjbGFzcyBQb2ludCB7CiAgICBpbnQgciwgYywgZDsKICAgIHB1YmxpYyBQb2ludChpbnQgciwgaW50IGMpIHsKICAgICAgICB0aGlzLnIgPSByOwogICAgICAgIHRoaXMuYyA9IGM7CiAgICB9CiAgICBwdWJsaWMgUG9pbnQoaW50IHIsIGludCBjLCBpbnQgZCkgewogICAgICAgIHRoaXMociwgYyk7CiAgICAgICAgdGhpcy5kID0gZDsKICAgIH0KfQoKcHVibGljIGNsYXNzIFJlY3Vyc2l2ZU1laXJvIHsKICAgIHByaXZhdGUgU3RyaW5nW10gZihTdHJpbmdbXVtdIGZpZWxkKSB7CiAgICAgICAgaW50IFIgPSBmaWVsZC5sZW5ndGg7CiAgICAgICAgU3RyaW5nW10gcmVzID0gbmV3IFN0cmluZ1tSXTsKICAgICAgICBmb3IoaW50IHI9MDsgcjxSOyByKyspIHsKICAgICAgICAgICAgcmVzW3JdID0gZmllbGRbcl1bMF07CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICB9CgogICAgaW50W10gZHIgPSB7ICAwLCAwLCAxLCAtMSB9OwogICAgaW50W10gZGMgPSB7IC0xLCAxLCAwLCAgMCB9OwoKICAgIHByaXZhdGUgU3RyaW5nIGJmcyhjaGFyW11bXSBjc3MpIHsKICAgICAgICBpbnQgUiA9IGNzcy5sZW5ndGg7CiAgICAgICAgaW50IEMgPSBjc3NbMF0ubGVuZ3RoOwogICAgICAgIFF1ZXVlPFBvaW50PiBxID0gbmV3IExpbmtlZExpc3Q8UG9pbnQ+KCk7CiAgICAgICAgaW50W11bXSBpc3MgPSBuZXcgaW50W1JdW0NdOwogICAgICAgIGZvcihpbnQgcj0wOyByPFI7IHIrKykgewogICAgICAgICAgICBmb3IoaW50IGM9MDsgYzxDOyBjKyspIHsKICAgICAgICAgICAgICAgIGlmKGNzc1tyXVtjXSA9PSAnIycpIHsKICAgICAgICAgICAgICAgICAgICBpc3Nbcl1bY10gPSA5ODc2NTQzMjE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaW50IHNyID0gLTEsIHNjID0gLTE7CmhlcmU6ICAgZm9yKGludCByPTA7IHI8UjsgcisrKSB7CiAgICAgICAgICAgIGZvcihpbnQgYz0wOyBjPEM7IGMrKykgewogICAgICAgICAgICAgICAgaWYoY3NzW3JdW2NdID09ICdTJykgewogICAgICAgICAgICAgICAgICAgIHEuYWRkKG5ldyBQb2ludChyLCBjLCAwKSk7CiAgICAgICAgICAgICAgICAgICAgaXNzW3JdW2NdID0gMDsKICAgICAgICAgICAgICAgICAgICBjc3Nbcl1bY10gPSAnIyc7CiAgICAgICAgICAgICAgICAgICAgc3IgPSByOwogICAgICAgICAgICAgICAgICAgIHNjID0gYzsKICAgICAgICAgICAgICAgICAgICBicmVhayBoZXJlOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIFN0cmluZyByZXMgPSAiIjsKICAgICAgICBpbnQgZ29hbCA9IC0xOwogICAgICAgIHdoaWxlKCFxLmlzRW1wdHkoKSkgewogICAgICAgICAgICBQb2ludCBwID0gcS5yZW1vdmUoKTsKICAgICAgICAgICAgaW50IHIgPSBwLnIsIGMgPSBwLmM7CiAgICAgICAgICAgIGZvcihpbnQgaT0wOyBpPDQ7IGkrKykgewogICAgICAgICAgICAgICAgaW50IG5yID0gciArIGRyW2ldOwogICAgICAgICAgICAgICAgaW50IG5jID0gYyArIGRjW2ldOwogICAgICAgICAgICAgICAgaWYobnIgPCAwIHx8IFIgPD0gbnIgfHwgbmMgPCAwIHx8IEMgPD0gbmMpIHsgY29udGludWU7IH0KICAgICAgICAgICAgICAgIGlmKGNzc1tucl1bbmNdID09ICcjJykgeyBjb250aW51ZTsgfQogICAgICAgICAgICAgICAgY3NzW25yXVtuY10gPSAnIyc7CiAgICAgICAgICAgICAgICBxLmFkZChuZXcgUG9pbnQobnIsIG5jLCBwLmQrMSkpOwogICAgICAgICAgICAgICAgaXNzW25yXVtuY10gPSBwLmQrMTsKICAgICAgICAgICAgICAgIGdvYWwgPSBtYXgoZ29hbCwgcC5kKzEpOwogICAgICAgICAgICAgICAgaWYoY3NzW25yXVtuY10gPT0gJ0cnKSB7IGJyZWFrOyB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaW50IGN1ciA9IDA7CiAgICAgICAgaW50IHIgPSBzcjsKICAgICAgICBpbnQgYyA9IHNjOwogICAgICAgIGZvciggOyBjdXIgPCBnb2FsIDsgKSB7CiAgICAgICAgICAgIGlmKGlzc1tyXVtjXSA9PSBjdXIpIHsKICAgICAgICAgICAgICAgIGZvcihpbnQgaT0wOyBpPDQ7IGkrKykgewogICAgICAgICAgICAgICAgICAgIGludCBuciA9IHIgKyBkcltpXTsKICAgICAgICAgICAgICAgICAgICBpbnQgbmMgPSBjICsgZGNbaV07CiAgICAgICAgICAgICAgICAgICAgaWYobnIgPCAwIHx8IFIgPD0gbnIgfHwgbmMgPCAwIHx8IEMgPD0gbmMpIHsgY29udGludWU7IH0KICAgICAgICAgICAgICAgICAgICBpZihpc3NbbnJdW25jXSA9PSBjdXIrMSkgewogICAgICAgICAgICAgICAgICAgICAgICBpZihpID09IDApIHsgcmVzICs9ICJsIjsgfQogICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGlmKGkgPT0gMSkgeyByZXMgKz0gInIiOyB9CiAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UgaWYoaSA9PSAyKSB7IHJlcyArPSAiZCI7IH0KICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBpZihpID09IDMpIHsgcmVzICs9ICJ1IjsgfQogICAgICAgICAgICAgICAgICAgICAgICBjdXIgKz0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgciA9IG5yOwogICAgICAgICAgICAgICAgICAgICAgICBjID0gbmM7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXM7CiAgICB9CgogICAgcHVibGljIFN0cmluZyBhZHZlbnR1cmUoU3RyaW5nW11bXSBmaWVsZCkgewogICAgICAgIFN0cmluZ1tdIGYgPSBmKGZpZWxkKTsKICAgICAgICBpbnQgUiA9IGYubGVuZ3RoOwogICAgICAgIGNoYXJbXVtdIGNzcyA9IG5ldyBjaGFyW1JdW107CiAgICAgICAgZm9yKGludCByPTA7IHI8UjsgcisrKSB7CiAgICAgICAgICAgIGNzc1tyXSA9IGZbcl0udG9DaGFyQXJyYXkoKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGJmcyhjc3MpOwogICAgfQoKICAgIHByaXZhdGUgdm9pZCBkb0l0KCkgewogICAgICAgIFN0cmluZ1tdW10gc3NzMSA9IHsKICAgICAgICAgICAgeyAiIyMjIyMiIH0sCiAgICAgICAgICAgIHsgIiNTLi4jIiB9LAogICAgICAgICAgICB7ICIjIyMuIyIgfSwKICAgICAgICAgICAgeyAiI0cuLiMiIH0sCiAgICAgICAgICAgIHsgIiMjIyMjIiB9LAogICAgICAgIH07CiAgICAgICAgU3RyaW5nIHJlczEgPSBhZHZlbnR1cmUoc3NzMSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHJlczEpOwoKICAgICAgICBTdHJpbmdbXVtdIHNzczIgPSB7CiAgICAgICAgICAgIHsgIiMjIyMjIiB9LAogICAgICAgICAgICB7ICIjLi5HIyIgfSwKICAgICAgICAgICAgeyAiIy4uLiMiIH0sCiAgICAgICAgICAgIHsgIiMuLi4jIiB9LAogICAgICAgICAgICB7ICIjLi4uIyIgfSwKICAgICAgICAgICAgeyAiI1MuLiMiIH0sCiAgICAgICAgICAgIHsgIiMjIyMjIiB9LAogICAgICAgIH07CiAgICAgICAgU3RyaW5nIHJlczIgPSBhZHZlbnR1cmUoc3NzMik7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHJlczIpOwoKICAgICAgICBTdHJpbmdbXVtdIHNzczMgPSB7CiAgICAgICAgICAgIHsgIiMjIyMiIH0sCiAgICAgICAgICAgIHsgIiNTLiMiIH0sCiAgICAgICAgICAgIHsgIiMuRyMiIH0sCiAgICAgICAgICAgIHsgIiMjIyMiIH0sCiAgICAgICAgfTsKICAgICAgICBTdHJpbmcgcmVzMyA9IGFkdmVudHVyZShzc3MzKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocmVzMyk7CgogICAgICAgIFN0cmluZ1tdW10gc3NzNCA9IHsKICAgICAgICAgICAgeyAiIyMjIyMjIyIgfSwKICAgICAgICAgICAgeyAiIy4uLi4uIyIgfSwKICAgICAgICAgICAgeyAiIy4jLiMuIyIgfSwKICAgICAgICAgICAgeyAiIy4jUyMuIyIgfSwKICAgICAgICAgICAgeyAiIy4jIyMuIyIgfSwKICAgICAgICAgICAgeyAiIy4jRyMuIyIgfSwKICAgICAgICAgICAgeyAiIy4jLiMuIyIgfSwKICAgICAgICAgICAgeyAiIy4uLi4uIyIgfSwKICAgICAgICAgICAgeyAiIyMjIyMjIyIgfSwKICAgICAgICB9OwogICAgICAgIFN0cmluZyByZXM0ID0gYWR2ZW50dXJlKHNzczQpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihyZXM0KTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgbmV3IFJlY3Vyc2l2ZU1laXJvKCkuZG9JdCgpOwogICAgfQp9