题目大意
给定一个环形路线,其上有n个加油站,每个加油站只能提供gas[i]单位的汽油,从i~i+1加油站要花费cost[i]单位汽油,返回能环绕一圈的起点位置
思路
暴力,赤裸裸的暴力,直接1Y了,此贴记录一下第一次白板1Y。以后水题就不开贴了。
代码
#include<iostream>
#include<set>
#include<vector>
#include<map>
using namespace std;
int n = 0;
int travel(int deep, int rest, int index, int cur,vector<int> &gas, vector<int> &cost)
{
if (deep == n)return index;
if (rest+gas[cur]-cost[cur]<0 || travel(deep+1, rest+gas[cur]-cost[cur],index,(cur+1)%n, gas, cost) == -1) return -1;
return index;
}
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
n = gas.size();
for (int i = 0; i < n; i ++){
int tmp = travel(0,0,i,i,gas,cost);
if (tmp != -1)return tmp;
}
return -1;
}
int main()
{
int n;
while (cin >> n){
vector <int> a,b;
a.clear();
b.clear();
for (int i = 0; i < n; i ++){
int tmp1, tmp2;
cin >> tmp1>>tmp2;
a.push_back(tmp1);
b.push_back(tmp2);
}
cout << "index:"<<canCompleteCircuit(a,b)<<endl;
}
return 0;
}