Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <list>
- #include <iostream>
- using namespace std;
- bool canCross(vector<int>& stones)
- {
- int n=stones.size();
- if(n==1)
- {
- if(stones[0]==0) return true;
- else return false;
- }
- if(stones[0]!=0 || stones[1]!=1) return false;
- int bomb=0;
- list<int> gaps;
- for(int i=0; i<n-1; ++i)
- {
- gaps.push_back(stones[i+1]-stones[i]);
- }
- for(auto i=gaps.begin(); i!=gaps.end(); ++i)
- {
- while(bomb>0)
- {
- --i;
- --bomb;
- }
- auto i_next=i;
- ++i_next;
- auto i_prev=i;
- --i_prev;
- if(i_next==gaps.end()) return true;
- int diff=(*i_next)-(*i);
- if(diff>=-1 && diff<=1) continue;
- if(diff>0)
- {
- if(i_prev==gaps.begin() || i==gaps.begin()) return false;
- (*i)=(*i)+(*i_prev);
- gaps.erase(i_prev);
- bomb=2;
- }
- else
- {
- if((*i_prev)<(*i))
- {
- auto i_next_next=i_next;
- ++i_next_next;
- if(i_next_next==gaps.end()) return false;
- (*i_next)=(*i_next)+(*i_next_next);
- gaps.erase(i_next_next);
- bomb=1;
- }
- else
- {
- if(i_next==gaps.end()) return false;
- (*i)=(*i)+(*i_next);
- gaps.erase(i_next);
- --i;
- bomb=1;
- }
- }
- }
- return true;
- }
- int main()
- {
- vector<int> input={0,1,3,6,10,13,15,16,19,21,25};
- vector<int> input2={0,1,3,5,6,8,12,17};
- vector<int> input3={0,1,2,3,4,8,9,11};
- cout << canCross(input) << endl;
- cout << canCross(input2) << endl;
- cout << canCross(input3) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement