# 1138.字母板上的路径

## 问题描述
[1138.字母板上的路径](https://leetcode.cn/problems/alphabet-board-path/)

## 解题思路
考虑到'z'单独在一个地方，因此移动顺序中，左下、右上不能反过来，即不能先往下再往左或者先往右再往上。

## 代码
```cpp
class Solution {
public:
    string alphabetBoardPath(string target) {
        string res;
        vector<int> cur_loc{0, 0};
        vector<int> target_loc{0, 0};
        // 要注意边缘的存在
        for (auto &c : target) {
            target_loc[0] = (c - 'a') / 5;
            target_loc[1] = (c - 'a') % 5;
            int move_row = target_loc[0] - cur_loc[0];
            int move_col = target_loc[1] - cur_loc[1];
            cur_loc[0] = target_loc[0];
            cur_loc[1] = target_loc[1];
            if (move_col < 0) {
                res.insert(res.end(), -move_col, 'L');
                if (move_row >= 0)
                    res.insert(res.end(), move_row, 'D');
                else
                    res.insert(res.end(), -move_row, 'U');
            } else {
                if (move_row >= 0)
                    res.insert(res.end(), move_row, 'D');
                else
                    res.insert(res.end(), -move_row, 'U');
                res.insert(res.end(), move_col, 'R');
            }
            res.push_back('!');
        }
        return res;
    }
};
```


