Leetcode之Graph and Search

graph

332. Reconstruct Itinerary

class Solution {
public:
    vector<string> findItinerary(vector<pair<string, string>> tickets) {
        tickets_.clear();
        routes_.clear();
        for(const auto& tic: tickets) {
            tickets_[tic.first].push_back(tic.second);
        }    
        for(auto& tic:tickets_) {
            auto& dests = tic.second;
            std::sort(dests.begin(), dests.end());
        }    

        const string stt="JFK";
        schedule(stt);

        return vector<string>(routes_.crbegin(), routes_.crend());
    }
private:
    unordered_map<string, deque<string>> tickets_;
    vector<string> routes_;

    void schedule(const string&stt_station) {
        auto& visit = tickets_[stt_station];
        while(!visit.empty()) {
            string dest=visit.front();
            visit.pop_front();
            schedule(dest);
        }    
        routes_.push_back(stt_station);    
    }    
};

675. Cut Off Trees for Golf Event

class Solution {
public:
    int BFS(vector<vector<int>> forest, int ty, int tx, int cy, int cx) {
        int h = forest.size();
        int w = forest[0].size();
        int step = 0;
        queue<pair<int, int>> visited;
        visited.push(make_pair(cy, cx));
        int dirs[4][2] = {
  {-1, 0}, {
  0, 1}, {
  1, 0}, {
  0, -1}};
        while(visited.size()) {
            int tmp = visited.size();
            for(int idx = 0; idx < tmp; idx++) {
                auto tp = visited.front();
                int tmp_y = tp.first;
                int tmp_x = tp.second;

                if(tmp_y == ty && tmp_x == tx) {
                    return step;
                }

                visited.pop();
                forest[tmp_y][tmp_x] = 0;
                for(int i = 0; i < 4; i++) {
                    int tmp_y_ = tmp_y+dirs[i][0];
                    int tmp_x_ = tmp_x+dirs[i][1];
                    if(tmp_y_ >= 0 && tmp_y_< h) {
                        if(tmp_x_>= 0 && tmp_x_< w) {
                            if(forest[tmp_y_][tmp_x_] > 0) {
                                visited.push(make_pair(tmp_y_, tmp_x_));
                                forest[tmp_y_][tmp_x_] = 0;
                            }
                        }
                    }
                }
            }
            step++;
        }
        return INT_MAX;
    }

    int cutOffTree(vector<vector<int>>& forest) {
        int ans = 0;
        int h = forest.size();
        if(!h) {
            return ans;
        }
        int w = forest[0].size();
        vector<tuple<int, int, int> > trees;
        for(int idx1 = 0; idx1 < h; idx1++) {
            for(int idx2 = 0; idx2 < w; idx2++) {
                if(forest[idx1][idx2] > 1) {
                    trees.emplace_back(forest[idx1][idx2], idx1, idx2);
                }
            }
        }
        // cout << trees.size() << endl;
        sort(trees.begin(), trees.end());

        int len = trees.size();
        int cx = 0, cy = 0;
        for(int idx = 0; idx < len; idx++) {
            int ty = get<1>(trees[idx]);
            int tx = get<2>(trees[idx]);
            int steps = BFS(forest, ty, tx, cy, cx);
            // cout << steps << endl;
            if(steps == INT_MAX) {
                return -1;
            }
            ans += steps;
            cy = ty; 
            cx = tx; 
        }

        return ans;
    }
};

200. Number of Islands

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        if(grid.empty()) {
            return 0;
        }
        rows = grid.size();
        if(!rows) {
            return 0;
        }    
        cols = grid[0].size();
        int num_iland = 0;
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                if(grid[i][j]=='1') {
                    dfs(grid, i, j);
                    num_iland++;
                }    
            }    
        }    

        return num_iland;
    }
private:
    int rows;
    int cols;
    void dfs(vector<vector<char>>&grid, int row, int col) {
        if(row < 0 || col < 0 || row >= rows || col >= cols) {
            return;
        }    
        if(grid[row][col]=='0') {
            return;
        }    
        grid[row][col]='0';
        dfs(grid,row-1,col);
        dfs(grid,row+1,col);
        dfs(grid,row,col-1);
        dfs(grid,row,col+1);

    }    
};

547. Friend Circles


class Solution {
private:
    void dfs(vector<vector<int>>&M, vector<int>&rec, int n, int idx) {
        if(rec[idx]) {
            return;
        }
        rec[idx]=1;
        for(int i=0;i<n;i++) {
            if(M[idx][i] && !rec[i]) {
                dfs(M, rec, n, i);
            }
        }    
    }    
public:
    int findCircleNum(vector<vector<int>>& M) {
        assert(!M.empty() && M.size()>0 && M.size()==M[0].size());
        int n=M.size();
        int ans=0;
        vector<int> rec(n,0);
        for(int i=0; i<n; i++) {
            if(rec[i]) {
                continue;
            }
            ans++;
            dfs(M,rec,n,i);
        }
        return ans;
    }    
};

class Solution {
private:
    void dfs(vector<vector<int>>&M, int n, int idx) {
        if(!M[idx][idx]) {
            return;
        }
        M[idx][idx]=0;
        for(int i=0;i<n;i++) {
            if(M[idx][i]) {
                M[idx][i] = M[i][idx] = 0;
                dfs(M, n, i);
            }
        }    
    } 

public:
    int findCircleNum(vector<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值