Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. bool canCross(vector<int>& stones)
  8. {
  9. int n=stones.size();
  10. if(n==1)
  11. {
  12. if(stones[0]==0) return true;
  13. else return false;
  14. }
  15. if(stones[0]!=0 || stones[1]!=1) return false;
  16. int bomb=0;
  17. list<int> gaps;
  18. for(int i=0; i<n-1; ++i)
  19. {
  20. gaps.push_back(stones[i+1]-stones[i]);
  21. }
  22.  
  23. for(auto i=gaps.begin(); i!=gaps.end(); ++i)
  24. {
  25. while(bomb>0)
  26. {
  27. --i;
  28. --bomb;
  29. }
  30. auto i_next=i;
  31. ++i_next;
  32. auto i_prev=i;
  33. --i_prev;
  34. if(i_next==gaps.end()) return true;
  35. int diff=(*i_next)-(*i);
  36. if(diff>=-1 && diff<=1) continue;
  37. if(diff>0)
  38. {
  39. if(i_prev==gaps.begin() || i==gaps.begin()) return false;
  40. (*i)=(*i)+(*i_prev);
  41. gaps.erase(i_prev);
  42. bomb=2;
  43. }
  44. else
  45. {
  46. if((*i_prev)<(*i))
  47. {
  48. auto i_next_next=i_next;
  49. ++i_next_next;
  50. if(i_next_next==gaps.end()) return false;
  51. (*i_next)=(*i_next)+(*i_next_next);
  52. gaps.erase(i_next_next);
  53. bomb=1;
  54. }
  55. else
  56. {
  57. if(i_next==gaps.end()) return false;
  58. (*i)=(*i)+(*i_next);
  59. gaps.erase(i_next);
  60. --i;
  61. bomb=1;
  62. }
  63. }
  64. }
  65. return true;
  66. }
  67.  
  68. int main()
  69. {
  70. vector<int> input={0,1,3,6,10,13,15,16,19,21,25};
  71. vector<int> input2={0,1,3,5,6,8,12,17};
  72. vector<int> input3={0,1,2,3,4,8,9,11};
  73. cout << canCross(input) << endl;
  74. cout << canCross(input2) << endl;
  75. cout << canCross(input3) << endl;
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement