Paste Search Dynamic
Recent pastes
jumping
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. int num, total_step, max_step = INT_MIN;
  6. long long max_E = INT_MIN;
  7. int high[16];
  8. int color[16];
  9. int jump[16];   //第 i 號石頭是否已經跳過
  10. int path[16];
  11.  
  12. void jumping(int cur_x, int destination, int step);
  13. int Energy();
  14.  
  15. int main() {
  16.     int position, destination;
  17.     scanf("%d %d %d", &num, &position, &destination);
  18.  
  19.     for (int i = 1; i <= num; i++)
  20.         scanf(" %d", &high[i]);
  21.  
  22.     for (int i = 1; i <= num; i++) {
  23.         scanf(" %d", &color[i]);
  24.     }
  25.  
  26.     jump[position] = 1;
  27.     jumping(position, destination, 1);
  28.    
  29.  
  30.     printf("%lld %dn", max_E, max_step - 1);
  31. }
  32.  
  33. void jumping(int cur_x, int destination, int step) {
  34.     path[step] = cur_x;
  35.  
  36.     if (cur_x != destination) {
  37.  
  38.         if (cur_x - 1 > 0 && jump[cur_x - 1] != 1) {
  39.             jump[cur_x - 1] = 1;
  40.             jumping(cur_x - 1, destination, step + 1);
  41.             jump[cur_x - 1] = 0;
  42.         }
  43.         if (cur_x + 1 <= num && jump[cur_x + 1] != 1) {
  44.             jump[cur_x + 1] = 1;
  45.             jumping(cur_x + 1, destination, step + 1);
  46.             jump[cur_x + 1] = 0;
  47.         }
  48.         for (int i = 1; i <= num; i++) {
  49.             if (color[i] == color[cur_x] && jump[i] != 1) {
  50.                 jump[i] = 1;
  51.                 jumping(i, destination, step + 1);
  52.                 jump[i] = 0;
  53.             }
  54.         }       //若有其他石頭可以跳,則先不跳去destination
  55.     }
  56.     else if (cur_x == destination) {
  57.         total_step = step;
  58.                        
  59.         int energy = Energy();
  60.  
  61.         if (energy > max_E) {
  62.             max_E = energy;
  63.             max_step = total_step;
  64.         }
  65.         else if (energy = max_E) {
  66.             if (total_step > max_step)
  67.                 max_step = total_step;
  68.         }
  69.     }
  70. }
  71.  
  72. int Energy() {
  73.     int tem_E = 0;
  74.  
  75.     for (int i = 2; i <= total_step; i++) {
  76.         tem_E += abs(path[i] - path[i - 1]) * abs(high[path[i]] - high[path[i - 1]]);
  77.     }
  78.  
  79.     return tem_E;
  80. }
Parsed in 0.007 seconds