-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphabetBoardPath.cpp
More file actions
34 lines (34 loc) · 1.02 KB
/
alphabetBoardPath.cpp
File metadata and controls
34 lines (34 loc) · 1.02 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
class Solution {
public:
string getmove(char prev, char c) {
string ret = "", post = "";
if (prev == 'z') {
ret = "U";
prev = 'u';
}
if (c == 'z') {
post = "D";
c = 'u';
}
int px = static_cast<int>(prev - 'a') / 5, py = static_cast<int>(prev - 'a') % 5;
int x = static_cast<int>(c - 'a') / 5, y = static_cast<int>(c - 'a') % 5;
x = std::abs(x - px) * (x - px >= 0 ? 1 : -1);
y = std::abs(y - py) * (y - py >= 0 ? 1 : -1);
if (x > 0) ret += string(x, 'D');
else if (x < 0) ret += string(-x, 'U');
if (y > 0) ret += string(y, 'R');
else if (y < 0) ret += string(-y, 'L');
ret += post;
return std::move(ret);
}
string alphabetBoardPath(string target) {
char prev = 'a';
string ans = "";
for (char c: target) {
if (prev != c) ans += getmove(prev, c);
ans += '!';
prev = c;
}
return ans;
}
};