In the last brush inscription respectively experience easy and medium difficulty, feel easy belong to water problems, belong to medium skill. Well, this of course, to experience the hard difficulty
675. Cut Off Trees for Golf Event
Difficulty:
Hard
Ideas:
Figure topic, in fact, that Italy is also very clear, in one kind of garden trees matrix, in accordance with the height of the tree felling small to large, for the shortest distance.
On FIG shortest distance search bfs of course, the number, the search for the sequence, the sequence can be pre-tree.
This problem is not visible difficulty lies in the idea, but the coding of the bfs.
STL Knowledge Review:
stack:
stack<element>
LIFO
stl deque based on the default stack to achieve, but in fact can also manually change the list, array or list to realize when it was declared, but the basic operation does not increase, and there is no much difference.
stack the basic operation are: insert push (), pop-pop (), query stack top (), sentenced empty empty (), empty clear (), query size size ()
stack supports copy an existing stack is constructed, but does not support traversal iterator
Function pop pop () no return value, using a general top () function in conjunction with the top of the stack
queue:
queue<element>
FIFO
queue and stack the basic functions and basic exactly the same, only the basic operations have a bit of difference
Basic operation queue there: Insert push (), pop-pop (), sentenced empty empty (), empty clear (), query size size ()
But pop () pop-up is the head of the queue elements instead of the top element
There are also front () and back () both queries and operations on the first team position
sort:
sort<struct>
This part mainly talk about the sort ordering custom structure.
To sort by general cmp function implemented in the plug, but not in the class leetcode cmp class definition, so this number is smaller than the brief overload talk directly implemented in the structure, because the default sort in ascending order
/*
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;
Code:
/*
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;
}
};