程序设计思维与实践 Week10限时模拟 东东的魔方
生活随笔
收集整理的這篇文章主要介紹了
程序设计思维与实践 Week10限时模拟 东东的魔方
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題描述
東東有一個二階魔方,即2×2×2的一個立方體組。立方體由八個角組成。
魔方的每一塊都用三維坐標(h, k, l)標記,其中h, k, l∈{0,1}。六個面的每一個都有四個小面,每個小面都有一個正整數。
對于每一步,東東可以選擇一個特定的面,并把此面順時針或逆時針轉90度。
請你判斷,是否東東可以在一個步驟還原這個魔方(每個面沒有異色)。
input
** output **
對于每個測試用例,魔方如果可以至多 “只轉一步” 恢復,輸出YES,則輸出NO
sample input
sample output
YES YES YES NO想法
每次只能轉一次,每次只能轉90度,每次旋轉可以看做是交換了八個點的值,這樣按照點的對應關系我們就能寫出每個面順勢針旋轉90度所對應函數,每次旋轉都要判斷魔方是否復原,由judge函數實現,逆時針旋轉90度相當于順時針旋轉270度,也就是轉三次,當對一個面兩種旋轉都不能未復原就要再轉一次恢復原狀態換下一個面再判斷。
代碼
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int a[7][5]; void upShift() {int t1 = a[2][1], t2 = a[2][2];a[2][1] = a[5][2];a[2][2] = a[5][4];a[5][2] = a[4][4];a[5][4] = a[4][3];a[4][4] = a[6][3];a[4][3] = a[6][1];a[6][3] = t1;a[6][1] = t2; } void leftShift() {int t1 = a[1][1], t2 = a[1][3];a[1][1] = a[4][1];a[1][3] = a[4][3];a[4][1] = a[3][1];a[4][3] = a[3][3];a[3][1] = a[2][1];a[3][3] = a[2][3];a[2][1] = t1;a[2][3] = t2; } void frontShift() {int t1 = a[1][1], t2 = a[1][2];a[1][1] = a[5][1];a[1][2] = a[5][2];a[5][1] = a[3][4];a[5][2] = a[3][3];a[3][4] = a[6][1];a[3][3] = a[6][2];a[6][1] = t1;a[6][2] = t2; } bool Judge() {for (int i = 1; i <= 6; i++)if (count(a[i] + 1, a[i] + 4 + 1, a[i][1]) != 4)return false;return true; } int main() {freopen("input.txt", "r", stdin);freopen("out.txt", "w", stdout);int N;cin >> N;while (N--){for (int i = 1; i <= 6; i++)for (int j = 1; j <= 4; j++)cin >> a[i][j];if (Judge()){cout << "YES" << endl;continue;}upShift();if (Judge()){cout << "YES" << endl;continue;}upShift();upShift();if (Judge()){cout << "YES" << endl;continue;}upShift();leftShift();if (Judge()){cout << "YES" << endl;continue;}leftShift();leftShift();if (Judge()){cout << "YES" << endl;continue;}leftShift();frontShift();if (Judge()){cout << "YES" << endl;continue;}frontShift();frontShift();if (Judge()){cout << "YES" << endl;continue;}frontShift();cout << "NO" << endl;}fclose(stdin);fclose(stdout);return 0; }總結
以上是生活随笔為你收集整理的程序设计思维与实践 Week10限时模拟 东东的魔方的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 沉睡者-程序员利用javascript开
- 下一篇: java计算机毕业设计校园约自习网站源码