Untitled
unknown
plain_text
10 months ago
1.9 kB
2
Indexable
Never
#include <iostream> using namespace std; int T, N; int gate[3]; int number[3]; bool visited[3]; int ans, minA; int spot[70]; bool is_Open(){ for(int i = 0; i < 3; i++){ if(visited[i] == false){ return false; } } return true; } int disLeft(int s){ for(int i = s; i >= 1; i--){ if(spot[i] == -1){ return s - i; } } return 100000; } int disRight(int s){ for(int i = s; i <= N; i++){ if(spot[i] == -1){ return i - s; } } return 100000; } void backtrack(int ans){ if(is_Open()){ if(ans < minA){ minA = ans; } } for(int i = 0; i < 3; i++){ if(visited[i]) continue; visited[i] = true; int tmp = 0; for(int j = 0; j < number[i] - 1; j++){ int left = disLeft(gate[i]); int right = disRight(gate[i]); if(left < right){ spot[gate[i] - left] = i + 1; tmp += left + 1; }else{ spot[gate[i] + right] = i + 1; tmp += right + 1; } } int L = disLeft(gate[i]); int R = disRight(gate[i]); if(L != R){ if(L < R){ spot[gate[i] - L] = i + 1; tmp += L + 1; }else{ spot[gate[i] + R] = i + 1; tmp += R + 1; } backtrack(ans + tmp); }else{ tmp += L + 1; spot[gate[i] - L] = i + 1; backtrack(ans + tmp); spot[gate[i] - L] = -1; spot[gate[i] + R] = i + 1; backtrack(ans + tmp); spot[gate[i] + R] = -1; } visited[i] = false; for(int j = 1; j <= N; j++){ if(spot[j] == i + 1){ spot[j] = -1; } } } } int main(){ freopen("input.txt", "rt", stdin); cin >> T; for(int tc = 1; tc <= T; tc++){ cin >> N; for(int i = 0; i < 3; i++){ cin >> gate[i] >> number[i]; visited[i] = false; } for(int i = 1; i <= N; i++){ spot[i] = -1; } minA = 100000; ans = 0; backtrack(0); cout << "Case #" << tc << endl; cout << minA << endl; } return 0; }
Leave a Comment