---
# System prepended metadata

title: '題解 zerojudge j124: 3. 石窟探險 APCS'
tags: [APCS, c++]

---

###### tags: `APCS` `c++`
# 題解 zerojudge j124: 3. 石窟探險 APCS
遞迴
```cpp
#include<bits/stdc++.h>
using namespace std;
int idx=0;
vector<int>a;
long long ans=0;
int f(int r){
    int child;
    if(r&1) child=3;
    else    child=2;

    while(child--){
        int nextt=a[++idx];
        if(nextt==0)    continue;
        ans+=abs(r-nextt);
        f(nextt);
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    string s;
    getline(cin,s);
    stringstream ss;ss<<s;
    while(ss>>s){
        a.push_back(stoi(s));
    }
    f(a[idx]);
    cout<<ans;
}

```