-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode1138.cpp
50 lines (47 loc) · 1.33 KB
/
leetcode1138.cpp
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
class Solution {
public:
string alphabetBoardPath(string target) {
string board[6] = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};
int nowx = 0, nowy = 0;
string ans = "";
for(int i = 0 ; i < target.size() ; i++){
int tarx = (target[i]-'a')/5;
int tary = (target[i]-'a')%5;
if(tarx == 5 && tary == 0){
while(nowy>0){
nowy--;
ans+='L';
}
}
else if(nowx == 5 && nowy == 0){
nowx--;
ans+='U';
}
while(1){
if(board[nowx][nowy] == target[i]){
ans += '!';
break;
}
if(tarx < nowx){
nowx--;
ans+='U';
}
else if(tarx == nowx){
if(tary > nowy){
nowy++;
ans+='R';
}
else{
nowy--;
ans+='L';
}
}
else{
nowx++;
ans+='D';
}
}
}
return ans;
}
};