50168. Subway
難度:3.7/51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60#include <stdio.h>
#include "stationDistance.h"
#define MAXN 10000
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int get_arr_size(int a[]){
int n = 0;
while(a[n] != 0) n++;
return n;
}
int get_dist(int arr[], int s, int d){
int min_num = min(s, d);
int max_num = max(s, d);
int ans = 0;
for(int i = min_num - 1; i < max_num - 1; i++){
ans += arr[i];
}
return ans;
}
int stationDistance(int r[],int g[],int b[],int g_ori, int g_des, int start_st[],int end_st[]){
int g_size = get_arr_size(g);
int* arr[3] = {r, g, b};
if(start_st[0] == end_st[0]){
int color = start_st[0];
return get_dist(arr[color], start_st[1], end_st[1]);
}
else{
int start_color, start_idx, end_color, end_idx;
if(start_st[0] > end_st[0]){
start_color = end_st[0], start_idx = end_st[1];
end_color = start_st[0], end_idx = start_st[1];
}
else{
start_color = start_st[0], start_idx = start_st[1];
end_color = end_st[0], end_idx = end_st[1];
}
int ans = 0;
if(start_color == 0 && end_color == 1){ // R -> G
ans += get_dist(arr[0], start_idx, g_ori);
ans += get_dist(arr[1], 1, end_idx);
}
else if(start_color == 1 && end_color == 2){ // G -> B
ans += get_dist(arr[1], start_idx, g_size + 1);
ans += get_dist(arr[2], g_des, end_idx);
}
else{ // R -> G
ans += get_dist(arr[0], start_idx, g_ori);
ans += get_dist(arr[1], 1, g_size + 1);
ans += get_dist(arr[2], g_des, end_idx);
}
return ans;
}
}