1 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <cstdio> #include <map> using namespace std; int magnatic[4][8]; map<int, int> task; void rotate(int n, int dir) { if (dir == 1) { int first_ele = magnatic[n][7]; for (int i = 6; i >= 0; --i) { magnatic[n][i + 1] = magnatic[n][i]; } magnatic[n][0] = first_ele; } else if (dir == -1) { int last_ele = magnatic[n][0]; for (int i = 1; i < 8; ++i) { magnatic[n][i - 1] = magnatic[n][i]; } magnatic[n][7] = last_ele; } } void checkRotate(int current_num, int prior_num, int dir) { int count_dir = dir > 0 ? -1 : 1; task.insert({ current_num, dir }); if (current_num == 0) { if (prior_num != 1) { if (magnatic[current_num][2] != magnatic[current_num + 1][6]) { checkRotate(current_num + 1, current_num, count_dir); } } } else if (current_num == 1) { if (prior_num != 0) { if (magnatic[current_num - 1][2] != magnatic[current_num][6]) { checkRotate(current_num - 1, current_num, count_dir); } } if (prior_num != 2) { if (magnatic[current_num][2] != magnatic[current_num + 1][6]) { checkRotate(current_num + 1, current_num, count_dir); } } } else if (current_num == 2) { if (prior_num != 1) { if (magnatic[current_num - 1][2] != magnatic[current_num][6]) { checkRotate(current_num - 1, current_num, count_dir); } } if (prior_num != 3) { if (magnatic[current_num][2] != magnatic[current_num + 1][6]) { checkRotate(current_num + 1, current_num, count_dir); } } } else if (current_num == 3) { if (prior_num != 2) { if (magnatic[current_num - 1][2] != magnatic[current_num][6]) { checkRotate(current_num - 1, current_num, count_dir); } } } } int getScore() { int ans = 0, score = 1; for (int n = 0; n < 4; ++n) { if (magnatic[n][0] == 1) { ans += score; } score *= 2; } return ans; } int main() { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 8; ++j) { scanf("%1d", &magnatic[i][j]); } } int k; scanf("%d", &k); for (int i = 0; i < k; ++i) { int number, dir; scanf("%d %d", &number, &dir); checkRotate(number - 1, -1, dir); for (auto e : task) { rotate(e.first, e.second); } task.clear(); } printf("%d\n", getScore()); return 0; }
|