鱼C论坛

 找回密码
 立即注册
查看: 1847|回复: 0

[技术交流] C++刷LeetCode(1575. 统计所有可行路径)【动态规划】

[复制链接]
发表于 2021-1-9 13:25:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目描述:
  1. 给你一个 互不相同 的整数数组,其中 locations[i] 表示第 i 个城市的位置。同时给你 start,finish 和 fuel 分别表示出发城市、目的地城市和你初始拥有的汽油总量

  2. 每一步中,如果你在城市 i&#160;,你可以选择任意一个城市 j&#160;,满足 &#160;j != i&#160;且&#160;0 <= j < locations.length&#160;,并移动到城市&#160;j&#160;。从城市&#160;i&#160;移动到&#160;j&#160;消耗的汽油量为&#160;|locations[i] - locations[j]|,|x|&#160;表示&#160;x&#160;的绝对值。

  3. 请注意,&#160;fuel&#160;任何时刻都&#160;不能&#160;为负,且你&#160;可以&#160;经过任意城市超过一次(包括&#160;start&#160;和&#160;finish&#160;)。

  4. 请你返回从&#160;start&#160;到&#160;finish&#160;所有可能路径的数目。

  5. 由于答案可能很大, 请将它对&#160;10^9 + 7&#160;取余后返回。

  6. &#160;

  7. 示例 1:

  8. 输入:locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5
  9. 输出:4
  10. 解释:以下为所有可能路径,每一条都用了 5 单位的汽油:
  11. 1 -> 3
  12. 1 -> 2 -> 3
  13. 1 -> 4 -> 3
  14. 1 -> 4 -> 2 -> 3
  15. 示例 2:

  16. 输入:locations = [4,3,1], start = 1, finish = 0, fuel = 6
  17. 输出:5
  18. 解释:以下为所有可能的路径:
  19. 1 -> 0,使用汽油量为 fuel = 1
  20. 1 -> 2 -> 0,使用汽油量为 fuel = 5
  21. 1 -> 2 -> 1 -> 0,使用汽油量为 fuel = 5
  22. 1 -> 0 -> 1 -> 0,使用汽油量为 fuel = 3
  23. 1 -> 0 -> 1 -> 0 -> 1 -> 0,使用汽油量为 fuel = 5
  24. 示例 3:

  25. 输入:locations = [5,2,1], start = 0, finish = 2, fuel = 3
  26. 输出:0
  27. 解释:没有办法只用 3 单位的汽油从 0 到达 2 。因为最短路径需要 4 单位的汽油。
  28. 示例 4 :

  29. 输入:locations = [2,1,5], start = 0, finish = 0, fuel = 3
  30. 输出:2
  31. 解释:总共有两条可行路径,0 和 0 -> 1 -> 0 。
  32. 示例 5:

  33. 输入:locations = [1,2,3], start = 0, finish = 2, fuel = 40
  34. 输出:615088286
  35. 解释:路径总数为 2615088300 。将结果对 10^9 + 7 取余,得到 615088286 。
  36. &#160;

  37. 提示:

  38. 2 <= locations.length <= 100
  39. 1 <= locations[i] <= 10^9
  40. 所有&#160;locations&#160;中的整数 互不相同&#160;。
  41. 0 <= start, finish <&#160;locations.length
  42. 1 <= fuel <= 200

  43. 来源:力扣(LeetCode)
  44. 链接:https://leetcode-cn.com/problems/count-all-possible-routes
  45. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码



1.自顶向下的动态规划
  1. class Solution {
  2. private:
  3.     int len;
  4.     int mode = 1e9+7;
  5. public:
  6.     int dfs(vector<int>&locations, int start, int finish, int fuel, vector<vector<int> >&dp){//vector<int>&locations中加不加&会出现巨大计算时间差别!!!
  7.         if(dp[start][fuel] != -1)return dp[start][fuel];
  8.         long long res = 0;
  9.         if(start == finish) res++;
  10.         for(int i = 0; i < len; i++){
  11.             if(i == start)continue;
  12.             int diff = abs(locations[i] - locations[start]);
  13.             if(fuel - diff >= 0){
  14.                 res += dfs(locations, i, finish, fuel - diff, dp) % mode;
  15.             }
  16.         }
  17.         dp[start][fuel] = res % mode;
  18.         return dp[start][fuel];
  19.     }
  20.     int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
  21.         len = locations.size();
  22.         vector<vector<int> >dp(len, vector<int>(fuel + 1, -1));
  23.         return dfs(locations, start, finish, fuel, dp);
  24.     }
  25. };
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-3 05:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表