Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.2 kB
5
Indexable
Never
#include <stdio.h>
int stone[16];
int jumped[17] = {0};//已走過的石頭
int color[17] = {0};
int max = -1, anstep = -1;
int n, s, t;

int abs(int a){
    if(a < 0) return -a;
    else return a;
}

void jump(int pos, int energy, int cnt){
    if(jumped[pos] == 1 || pos == 0 || pos > n){
        return;
    }//遇到已走過的石頭或超出石頭範圍則結束
    else{
        jumped[pos] = 1;
    }
    if(pos == t){//走到結束的石頭時
        if(energy > max){
            max = energy;
            anstep = cnt;
        }
        return;
    }
    jump(pos-1, abs(stone[pos]-stone[pos-1]), cnt+1);
    jumped[pos-1] = 0;
    jump(pos+1, abs(stone[pos]-stone[pos+1]), cnt+1);
    jumped[pos+1] = 0;
    for(int i = 1; i <= n; i++){
        if(color[pos] == color[i]){
            jump(i, abs(i-pos)*abs(stone[pos]-stone[i]), cnt+1);
            jumped[i] = 0;
        }
    }
}
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, 0, -1);
    printf("%d %d\n", max, anstep);
}