Leetcode-403 Frog Jump(青蛙过河)

 1 #define pb push_back
 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 3 class Solution
 4 {
 5     public:
 6         bool canCross(vector<int>& stones)
 7         {
 8             int sz = stones.size();
 9             if(sz==2&&stones[1]==1) return true;
10 
11             vector<set<int>> dp(sz);
12             dp[0].insert(0);
13             for(int i = 1; i < sz; i ++)
14             {
15                 int szz = dp[i-1].size();
16                 cout << szz << endl;
17                 for(auto j = dp[i-1].begin(); j != dp[i-1].end(); j ++)
18                 {
19                     if(binary_search(stones.begin(),stones.end(),stones[i-1]+*j))
20                         dp[lower_bound(stones.begin(),stones.end(),stones[i-1]+*j)-stones.begin()].insert(*j);
21                     if(binary_search(stones.begin(),stones.end(),stones[i-1]+*j+1))
22                         dp[lower_bound(stones.begin(),stones.end(),stones[i-1]+*j+1)-stones.begin()].insert(*j+1);
23                     if(*j!=1&&binary_search(stones.begin(),stones.end(),stones[i-1]+*j-1))
24                         dp[lower_bound(stones.begin(),stones.end(),stones[i-1]+*j-1)-stones.begin()].insert(*j-1);
25                 }
26             }
27             if(dp[sz-1].empty())
28                 return false;
29             return true;
30         }
31 };

猜你喜欢

转载自www.cnblogs.com/Asurudo/p/10350968.html