-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path미로탈출명령어.java
31 lines (25 loc) · 919 Bytes
/
미로탈출명령어.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
private final String[] dirs = {"d", "l", "r", "u"};
private final int[] dx = {1, 0, 0, -1};
private final int[] dy = {0, -1, 1, 0};
public String solution(int n, int m, int x, int y, int r, int c, int k) {
if (x == r && y == c && k == 0) {
return "";
}
int distance = Math.abs(x - r) + Math.abs(y - c);
if (distance > k || (k - distance) % 2 == 1) {
return "impossible";
}
for (int d = 0; d < dirs.length; d++) {
int nx = x + dx[d];
int ny = y + dy[d];
if (0 < nx && nx <= n && 0 < ny && ny <= m) {
String s = solution(n, m, nx, ny, r, c, k - 1);
if (!s.equals("impossible")) {
return dirs[d] + s;
}
}
}
return "impossible";
}
}