LeetCodeGOGOGO刷題記02——體驗Hard題型(BFS模擬)

在上次的刷題記中分別體驗了easy和medium難度,感覺easy屬於純水題,medium屬於技巧性。那麼,這次當然要來體驗一下hard難度

675. Cut Off Trees for Golf Event

難度:

Hard

思路:

圖論題,其實題意也十分清晰,在一個種滿樹的的矩陣中,按照樹的高度從小到大依次砍樹,求最短距離。

圖論最短距離的搜索當然要數bfs了,對於搜索順序,對樹預處理排序即可。

可見這題的難度並不是在於思路上,而是對bfs的coding上。

STL知識回顧:

stack:

stack<element>

LIFO

stl的stack默認基於deque實現,但其實也能在聲明的時候手動改成list,array或者list實現,但基本操作並不會增加,所以並沒有什麼太大區別。

stack基本操作主要有:插入push(),彈出pop(),查詢棧頂top(),判空empty(),查詢大小size()

stack支持複製已有stack進行構造,但不支持遍歷的iterator

彈出函數pop()無返回值,一般與棧頂函數top()結合使用

queue:

queue<element>

FIFO

queue和stack基本功能和實現基本一模一樣,只有基本操作有一點小小區別

queue基本操作也有:插入push(),彈出pop(),判空empty(),查詢大小size()

不過pop()彈出的是隊首元素而不是棧頂元素

此外還有front()和back()這兩個查詢隊首和對位的操作

sort:

sort<struct>

這部分主要想說一下sort對自定義結構體的排序。

一般情況可以通過往sort中塞cmp函數實現,但由於leetcode中無法在class類定義cmp,於是這次簡要說說直接在結構體中重載小於號來實現,因爲sort默認從小到大排序


/*
Author Owen_Q
*/

typedef struct DataStruct
{
        int variable, variable_used_for_sort;
        
        bool operator < (const struct DataStuct& dts) const
        {
            if(variable_used_for_sort < dts.variable_used_for_sort)
                return true;
            else
                return false;
        }
        
}Ds;

 

代碼:


/*
Author Owen_Q
*/

class Solution {
public:
    int cutOffTree(vector<vector<int>>& forest) {
        m = forest[0].size();
        n = forest.size();
        aim.clear();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(forest[i][j]>1)
                {
                    Point temp;
                    temp.x = i;
                    temp.y = j;
                    temp.h = forest[i][j];
                    aim.push_back(temp);
                }
                //cout << forest[i][j] << " ";
            }
            //cout << endl;
        }
        sort(aim.begin(),aim.end()/*,cmp*/);
        int sum = 0;
        int len = aim.size();
        pair<int,int> nowPoint = make_pair(0,0);
        for(int i=0;i<len;i++)
        {
            //cout << aim[i].x << "*" << aim[i].y << " ";
            int re = bfs(nowPoint,make_pair(aim[i].x,aim[i].y), forest);
            //cout << re << endl;
            if(re>=0)
            {
                sum += re;
                nowPoint = make_pair(aim[i].x,aim[i].y);
            }
            else
            {
                sum = -1;
                break;
            }
        }
        return sum;
    }
private:
    typedef struct POINT
    {
        int x, y, h;
        
        bool operator < (const struct POINT& rhs) const
        {
            if(h<rhs.h)
                return true;
            else
                return false;
        }
        
    }Point;
    
   /* bool cmp(const Point& a, const Point& b)
    {
        if(a.h<b.h)
            return true;
        else
            return false;
    }*/
    
     typedef struct STEP
    {
        int x,y,s;
    }Step;
    
    vector<Point> aim;
    
    int m;
    int n;
    
    bool legalPoint(Step p)
    {
        if(p.x<0||p.x>=n||p.y<0||p.y>=m)
            return false;
        else
            return true;
    }
    
    
    int bfs(pair<int,int>a, pair<int,int>b, vector<vector<int>>& forest)
    {
        bool forestin[55][55];
        
        memset(forestin, false, sizeof forestin);
        
        if(forest[a.first][a.second]==0||forest[b.first][b.second]==0)
            return -1;
        if(a==b)
            return 0;
        
        
        int d[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
    
        queue<Step> bfsqueue;
        
        forestin[a.first][a.second] = true;
        
        Step nexpos;
        nexpos.x = a.first;
        nexpos.y = a.second;
        nexpos.s = 0;
        
        bfsqueue.push(nexpos);
        
        while(!bfsqueue.empty())
        {
            Step nowpos = bfsqueue.front();
            //if(a==make_pair(3,1)&&b==make_pair(0,2))
                //cout << nowpos.x << "^" << nowpos.y << "^" << nowpos.s << endl;
            bfsqueue.pop();
            for(int i=0;i<4;i++)
            {
                nexpos.x = nowpos.x + d[i][0];
                nexpos.y = nowpos.y + d[i][1];
                nexpos.s = nowpos.s + 1;
                //if(a==make_pair(3,1)&&b==make_pair(0,2))
                    //cout << nexpos.x << "$" << nexpos.y << "$" << nexpos.s << endl;
                if(legalPoint(nexpos)&&forest[nexpos.x][nexpos.y]>0&&(!forestin[nexpos.x][nexpos.y]))
                {
                    forestin[nexpos.x][nexpos.y] = true;
                    if(nexpos.x==b.first&&nexpos.y==b.second)
                        return  nexpos.s;
                    else
                    {
                        bfsqueue.push(nexpos);
                        //if(a==make_pair(3,1)&&b==make_pair(0,2))
                            //cout << nexpos.x << "#" << nexpos.y << "#" << nexpos.s << endl;
                    }
                }
            }
        }
        return -1;
    }
    
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章