-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphabet_Board_Path.cpp
100 lines (92 loc) · 2.71 KB
/
Alphabet_Board_Path.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class Solution {
public:
string alphabetBoardPath(string target) {
unordered_map<char,pair<int,int>>m;
int c = 0;
for(int i = 0 ; i < 5; i++){
for(int j = 0 ; j < 5; j++){
char ch = c+'a';
m[ch] ={i,j};
//cout<<ch<<"--"<<i<<" "<<j<<endl;
c++;
}
}
m['z'] = {5,0};
pair<int,int> curr = {0,0};
string ans = "";
for(int i = 0 ; i < target.size(); i++){
pair<int,int>req = m[target[i]];
if(req.first == curr.first && req.second == curr.second){
ans += '!';
continue;
}
else if(req.first == curr.first){
int diff = req.second - curr.second;
if(diff > 0){
while(diff--){
ans += 'R';
}
}
else{
while(diff++){
ans += 'L';
}
}
curr = req;
}
else if(req.second == curr.second){
int diff = req.first - curr.first;
if(diff > 0){
while(diff--){
ans += 'D';
}
}
else{
while(diff++){
ans += 'U';
}
}
curr = req;
}
else{
int xdiff = req.first - curr.first;
int ydiff = req.second - curr.second;
curr = req;
if(xdiff > 0 && ydiff > 0){
while(xdiff--){
ans += 'D';
}
while(ydiff--){
ans += 'R';
}
}
else if(xdiff < 0 && ydiff > 0){
while(xdiff++){
ans += 'U';
}
while(ydiff--){
ans += 'R';
}
}
else if(xdiff > 0 && ydiff < 0){
while(ydiff++){
ans += 'L';
}
while(xdiff--){
ans += 'D';
}
}
else if(xdiff < 0 && ydiff < 0){
while(xdiff++){
ans += 'U';
}
while(ydiff++){
ans += 'L';
}
}
}
ans += '!';
}
return ans;
}
};