Leetcode 1138:字母板上的路径

题目描述

我们从一块字母板上的位置 (0, 0) 出发,该坐标对应的字符为 board[0][0]。

在本题里,字母板为board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"].

我们可以按下面的指令规则行动:

如果方格存在,'U' 意味着将我们的位置上移一行;
如果方格存在,'D' 意味着将我们的位置下移一行;
如果方格存在,'L' 意味着将我们的位置左移一列;
如果方格存在,'R' 意味着将我们的位置右移一列;
'!' 会把在我们当前位置 (r, c) 的字符 board[r][c] 添加到答案中。
返回指令序列,用最小的行动次数让答案和目标 target 相同。你可以返回任何达成目标的路径。

 

示例 1:

输入:target = "leet"
输出:"DDR!UURRR!!DDD!"
示例 2:

输入:target = "code"
输出:"RR!DDRR!UUL!R!"
 

提示:

1 <= target.length <= 100
target 仅含有小写英文字母。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/alphabet-board-path
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

 

 

解题思路

笨方法,bfs

class Solution {
public:
    vector<int> dx = {-1,0,1,0};
    vector<int> dy = {0,-1,0,1};
    string bfs(vector<vector<char>> graph,int &i,int &j,char it){
        string tans = "";
        int ti = i,tj = j;
        queue<pair<int,int>> que;
        que.push(make_pair(i,j));
        while(!que.empty()){
            int l = que.size();
            for(int k=0;k<l;++k){
                ti = que.front().first;
                tj = que.front().second;
                que.pop();
                for(int c=0;c<4;++c){
                    int ni = ti+dx[c];
                    int nj = tj+dy[c];
                    if(ni>=0&&ni<6&&nj>=0&&nj<5&&graph[ni][nj]!=' '){
                        que.push(make_pair(ni,nj));
                        if(graph[ni][nj] == it){
                            if(i == 5){
                                if(ni>i) tans += string(ni-i,'D');
                                else tans += string(i-ni,'U');
                                if(nj>j) tans += string(nj-j,'R');
                                else tans += string(j-nj,'L');
                            }
                            else{
                                if(nj>j) tans += string(nj-j,'R');
                                else tans += string(j-nj,'L');
                                if(ni>i) tans += string(ni-i,'D');
                                else tans += string(i-ni,'U');
                            }
                            i = ni;j = nj;
                            return tans+="!";
                        }
                        graph[ni][nj] = ' ';
                    }
                }
            }
        }
        return tans;
    }
    string alphabetBoardPath(string target) {
        vector<vector<char>> graph = {{'a','b','c','d','e'},{'f','g','h','i','j'},{'k','l','m','n','o'},{'p','q','r','s','t'},{'u','v','w','x','y'},{'z',' ',' ',' ',' '}};
        string ans = "";
        int i = 0,j = 0;
        for(auto it : target){
            if(graph[i][j] == it){
                ans += "!";
                continue;
            }
            ans += bfs(graph,i,j,it);
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值