-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_150365.java
More file actions
62 lines (57 loc) · 1.61 KB
/
_150365.java
File metadata and controls
62 lines (57 loc) · 1.61 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Solution {
static StringBuilder sb = new StringBuilder();
static int distance;
public static String solution(int n, int m, int x, int y, int r, int c, int k) {
x--;
y--;
r--;
c--;
distance = getDistance(r,c,x,y);
if(distance%2!=k%2 || distance>k) return "impossible";
while(true){
if(distance == k){
while(x<r){
x++;
sb.append('d');
}
while(c<y){
y--;
sb.append('l');
}
while(y<c){
y++;
sb.append('r');
}
while(r<x){
x--;
sb.append('u');
}
return sb.toString();
}
else if(getDistance(r,c,x+1,y)<=k-1 && x+1<n){
x++;
k--;
sb.append('d');
}
else if(getDistance(r,c,x,y-1)<=k-1 && y-1>=0){
y--;
k--;
sb.append('l');
}
else if(getDistance(r,c,x,y+1)<=k-1 && y+1<m){
y++;
k--;
sb.append('r');
}
else if(getDistance(r,c,x-1,y)<=k-1 && x-1>=0){
x--;
k--;
sb.append('u');
}
distance = getDistance(r,c,x,y);
}
}
public static int getDistance(int r, int c, int x, int y){
return Math.abs(r-x)+Math.abs(c-y);
}
}