Untitled

 avatar
unknown
c_cpp
2 years ago
1.0 kB
2
Indexable
#include <stdio.h>
int stone[16];
int jumped[17] = {0};
int color[17] = {0};
int max = -1, energy = 0, step = 0, anstep = -1;
int n, s, t;

void jump(int pos, int start, int end){
    if(jumped[pos] == 1 || pos == 0 || pos > n){
        jumped[pos] = 0;
        return;
    }
    else{
        jumped[pos] = 1;
        step++;
        energy += stone[pos];
    }
    if(pos == end){
        if(energy > max){
            max = energy;
            anstep = step;
        }
        energy = 0;
        step = 0;
        jumped[pos] = 0;
        return;
    }
    jump(pos-1, start, end);
    jump(pos+1, start, end);
    for(int i = 1; i <= n; i++){
        if(color[pos] == color[i]) jump(i, start, end);
    }
}
int main(){

    scanf("%d%d%d", &n, &s, &t);
    for(int i = 1; i <= n; i++){
        scanf("%d", &stone[i]);
    }
    for(int i = 1; i <= n; i++){
        scanf("%d", &color[i]);
    }
    jump(s, s, t);
    printf("%d %d\n", max, anstep);
}
Editor is loading...