leetcode 675. 为高尔夫比赛砍树——(每日一难day29)

675. 为高尔夫比赛砍树

你被请来给一个要举办高尔夫比赛的树林砍树。树林由一个 m x n 的矩阵表示, 在这个矩阵中:

  • 0 表示障碍,无法触碰
  • 1 表示地面,可以行走 比 1 大的数 表示有树的单元格,可以行走,
  • 数值表示树的高度

每一步,你都可以向上、下、左、右四个方向之一移动一个单位,如果你站的地方有一棵树,那么你可以决定是否要砍倒它。

你需要按照树的高度从低向高砍掉所有的树,每砍过一颗树,该单元格的值变为 1(即变为地面)。

你将从 (0, 0) 点开始工作,返回你砍完所有树需要走的最小步数。 如果你无法砍完所有的树,返回 -1

可以保证的是,没有两棵树的高度是相同的,并且你至少需要砍倒一棵树。

示例 1:

输入:forest = [[1,2,3],[0,0,4],[7,6,5]]
输出:6
解释:沿着上面的路径,你可以用 6步,按从最矮到最高的顺序砍掉这些树。

示例 2:

输入:forest = [[1,2,3],[0,0,0],[7,6,5]]
输出:-1
解释:由于中间一行被障碍阻塞,无法访问最下面一行中的树。

示例 3:

输入:forest = [[2,3,4],[0,0,5],[8,7,6]]
输出:6
解释:可以按与示例 1 相同的路径来砍掉所有的树。(0,0) 位置的树,可以直接砍去,不用算步数。

提示:

m == forest.length ,n == forest[i].length
1 <= m, n <= 50
0 <=forest[i][j] <= 1e9

解析

  1. 首先,根据树的高度从小到达排序
  2. 然后,从0 0 开始计算到步数
  3. 两点之间的最小路径可以通过bfs求得
  4. code中注释代码bfs也能通过所有样例(逻辑相同),不知道为什么通过了所有样例会超时?

code

class Solution {
public:
    int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

    int bfs( int sx, int sy, int tx, int ty,vector<vector<int>>& forest) {
        if (sx == tx && sy == ty) {
            return 0;
        }
        int row = forest.size();
        int col = forest[0].size();
        int step = 0;
        queue<pair<int, int>> qu;
        vector<vector<bool>> visited(row, vector<bool>(col, false));         
        qu.emplace(sx, sy);
        visited[sx][sy] = true;
        while (!qu.empty()) {
            step++;
            int sz = qu.size();
            for (int i = 0; i < sz; ++i) {
                auto [cx, cy] = qu.front();
                qu.pop();
                for (int j = 0; j < 4; ++j) {
                    int nx = cx + dirs[j][0];
                    int ny = cy + dirs[j][1];
                    if (nx >= 0 && nx < row && ny >= 0 && ny < col) {
                        if (!visited[nx][ny] && forest[nx][ny] > 0) {
                            if (nx == tx && ny == ty) {
                                return step;
                            }
                            qu.emplace(nx, ny);
                            visited[nx][ny] = true;
                        }
                    }
                }
            }
        }
        return -1;
    }

    /*
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    int getStep(int x1,int y1,int x2,int y2,vector<vector<int>>& forest){
        if(x1==x2&&y1==y2)
            return 0;
        queue<vector<int>> pv;
        int res=0;
        int m=forest.size(),n=forest[0].size();
        vector<vector<int>> vis(m,vector<int>(n,0));
        pv.push({x1,y1,0});
        vis[x1][y1]=1;
        while(!pv.empty()){
            auto tmp=pv.front();
            pv.pop();
            // 加入四个方向的点
            for(int i=0;i<4;i++){
                int nx=dx[i]+tmp[0];
                int ny=dy[i]+tmp[1];
                if(nx<0||ny<0||nx>=m||ny>=n||vis[nx][ny]==1||forest[nx][ny]==0)
                    continue;
                if(nx==x2&&ny==y2)
                    return tmp[2]+1;
                vis[nx][ny]=1;
                pv.push({nx,ny,tmp[2]+1});
            }
        }
        return 1;-
    }
    */
    // 每次需要访问队列中的最小值.
    int cutOffTree(vector<vector<int>>& forest) {
        int m=forest.size();
        int n=forest[0].size();
        vector<pair<int,int>> dd;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(forest[i][j]!=1&&forest[i][j]!=0)
                    dd.push_back(make_pair(i,j));
            }
        }
        sort(dd.begin(),dd.end(),[&](const pair<int,int>& l,const pair<int,int>& r){
            return forest[l.first][l.second]<forest[r.first][r.second];
        });
        int prex=0,prey=0,res=0;
        for(int i=0;i<dd.size();i++){
            int x=dd[i].first;
            int y=dd[i].second;
            int aa=bfs(prex,prey,x,y,forest);
            if(aa==-1)
                return -1;
            res+=aa;
            prex=x;
            prey=y;
        }
        return res;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值