5494. 统计所有可行路径(动态规划)
class Solution { public int countRoutes(int[] pos, int start, int end, int f) { int n = pos.length; int mod = (int)(1e9 + 7); long[][] dp = new long[f+1][n]; // dp[i][j] 表示当前剩余油量为i,当前位置为j的总路径数目 dp[0][start] = 1; for(int i = 1; i <= f; i++) { for(int j = 0; j < n; j++) { for(int k = 0; k < n; k++) { if(k == j) continue; int sum = Math.abs(pos[k]-pos[j]); if(sum > i) continue; dp[i][j] += dp[i-sum][k]; } dp[i][j] %= mod; } } long res = 0; for(int i = 0; i <= f; i++) { res += dp[i][end] % mod; } return (int)(res%mod); } }