Created
February 18, 2013 09:40
-
-
Save falsetru/4976237 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <algorithm> | |
#define swc(i) (1L << (i * 3)) | |
long long switches[10] = { | |
swc(0) | swc(1) | swc(2), | |
swc(3) | swc(7) | swc(9) | swc(11), | |
swc(4) | swc(10) | swc(14) | swc(15), | |
swc(0) | swc(4) | swc(5) | swc(6) | swc(7), | |
swc(6) | swc(7) | swc(8) | swc(10) | swc(12), | |
swc(0) | swc(2) | swc(14) | swc(15), | |
swc(3) | swc(14) | swc(15), | |
swc(4) | swc(5) | swc(7) | swc(14) | swc(15), | |
swc(1) | swc(2) | swc(3) | swc(4) | swc(5), | |
swc(3) | swc(4) | swc(5) | swc(9) | swc(13), | |
}; | |
long long mask = 0x6db6db6db6dbL; | |
int MAX = 987654321; | |
int solve(long long clocks, int s) | |
{ | |
if (s < 0) | |
return clocks == 0 ? 0 : MAX; | |
int result = MAX; | |
for (int i = 0; i < 4; i++) { | |
result = std::min(result, i + solve(clocks, s-1)); | |
clocks = (clocks + switches[s]) & mask; | |
} | |
return result; | |
} | |
int main() | |
{ | |
int c; | |
int i; | |
int x; | |
int ignore; | |
long long clocks; | |
ignore = scanf("%d\n", &c); | |
while (c--) { | |
clocks = 0L; | |
for (i = 0; i < 16; i++) { | |
ignore = scanf("%d", &x); | |
clocks |= (long long) ((x / 3) % 4) << (i*3); | |
} | |
x = solve(clocks, 9); | |
printf("%d\n", x < MAX ? x : -1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment