leetcode 困难题 1575. 统计所有可行路径-Count All Possible Routes
Problem: 1575. 统计所有可行路径-Count All Possible Routes需要使用数学公式|a-b| |a-c| |c-b|若从start到finish的cost fuel则记忆化mem[start][fuel] 0 返回0start 到 finish 可以经过某一点 point, 要么 |start - point| |point - finish|point若在start和finish之间则和|start - finish|相等若在两侧则需要的cost更多mem[start][fuel]表示当前在城市索引start剩下的oil是fuelCodeclass Solution { public: const int mod 1e9 30 - 23; int mem[101][201], subtract[101][101], n, a, fin; int dfs(vectorint locations, int start, int fuel) { long long sum 0; if(start fin) sum 1; if(subtract[start][fin] fuel) { mem[start][fuel] 0; return 0; } if(mem[start][fuel] 0) return mem[start][fuel]; for( int i 0; i n; i ) { if( start ! i ) { a fuel - subtract[start][i]; if(a 0 i fin) sum; else if(a 0) sum sum dfs(locations, i, a); } } sum % mod; mem[start][fuel] sum; return sum; } int countRoutes(vectorint locations, int start, int finish, int fuel) { n locations.size(); memset(mem, 0, sizeof(mem)); fin finish; for( int i 0; i n; i ) { a locations[i]; for( int j i 1; j n; j ) { subtract[j][i] subtract[i][j] abs( a - locations[j] ); } } int ret dfs(locations, start, fuel); return ret; } };