Untitled
unknown
c_cpp
3 years ago
1.2 kB
9
Indexable
#include <stdio.h>
int stone[16];
int jumped[17] = {0};//已走過的石頭
int color[17] = {0};
int max = -1, energy = 0, step = -1, anstep = -1;
int n, s, t;
int abs(int a){
if(a < 0) return -a;
else return a;
}
void jump(int expos, int pos, int end){//上一個所在位置、目前位置、結束點
if(jumped[pos] == 1 || pos == 0 || pos > n){
return;
}//遇到已走過的石頭或超出石頭範圍則結束
else{
jumped[pos] = 1;
step++;
energy += abs(expos-pos)*abs(stone[pos]-stone[expos]);
}
if(pos == end){//走到結束的石頭時
if(energy > max){
max = energy;
anstep = step;
}
energy = 0;
step = -1;
jumped[pos] = 0;
return;
}
jump(pos, pos-1, end);
jump(pos, pos+1, end);
for(int i = 1; i <= n; i++){
if(color[pos] == color[i]) jump(pos, i, 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...