题目说明:立方体相邻两点可以同时相加或相减,要把每个点变为0的过程
解题思路:
1. 要有解则A + C + F + H = B + D + E + G
2. 进入循环直至每个点为0
3. 找到最小的位置。如果这个最小的位置旁边3个点都为0,则找到另外一个有数的点,再找这两点分别相邻的相邻的点加1。如果这个最小的位置旁边3个领点有数字,则找一个有数字的点进行相减。(听上去会有点绕)
- Firstly, if it possible it will satisfy the equation A + C + F + H = B + D + E + G.
- Secondly, go into loop, if every position is 0 quit the loop.
- Thirdly, find the maximum position to decrease/increase. If this maximum position doesn’t have adjacent position to decrease, than find another position with value (not 0), increase 1 to two adjacent position adjacent to the maximum position and another position. Else decrease 1 to maximum position and the position adjacent to it.
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 |
#include <iostream> #include <string> using namespace std; int judge_done(int arr[8]) { for (int i = 0; i < 8; i++) { if (arr[i] != 0) { return 1; } } return 0; } int judge_zero(int arr[3], int arr_all[8]) { for (int i = 0; i < 3; i++) { if (arr_all[arr[i]] != 0) { return 1; } } return 0; } int find_same(int arr1[3], int arr2[3]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (arr1[i] == arr2[j]) { return arr1[i]; } } } return 0; } int main() { int arr[8]; string arr_name = "ABCDEFGH"; for (int i = 0; i < 8; i++) cin >> arr[i]; if (arr[0] + arr[2] + arr[5] + arr[7] != arr[1] + arr[3] + arr[4] + arr[6]) { cout << "IMPOSSIBLE" << endl; } else { int neighbor[8][3] = { { 1, 3, 4 } ,{ 0, 2, 5 },{ 1, 3, 6 },{ 0, 2, 7 },{ 0, 5, 7 },{ 1, 4, 6 },{ 2, 5, 7 },{ 3, 4, 6 } }; int max, position, temp_position; while (judge_done(arr)) { max = arr[0]; position = 0; for (int i = 0; i < 8; i++) { max = max >= arr[i] ? max : arr[i]; position = max > arr[i] ? position : i; } if (max > 0) { if (judge_zero(neighbor[position], arr)) { for (int i = 0; i < 3; i++) { if (arr[neighbor[position][i]] != 0) { arr[position]--; arr[neighbor[position][i]]--; cout << arr_name[position] << arr_name[neighbor[position][i]] << "-" << endl; break; } } } else { for (int i = 0; i < 8; i++) { if (i == position) continue; else if (arr[i] != 0) { temp_position = i; break; } } int position1 = neighbor[position][0]; int position2 = find_same(neighbor[position1], neighbor[temp_position]); arr[position1]++; arr[position2]++; cout << arr_name[position1] << arr_name[position2] << "+" << endl; } } } } return 0; } |