日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

程序设计思维与实践 Week10限时模拟 东东的魔方

發(fā)布時(shí)間:2024/5/8 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 程序设计思维与实践 Week10限时模拟 东东的魔方 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問題描述

東東有一個(gè)二階魔方,即2×2×2的一個(gè)立方體組。立方體由八個(gè)角組成。
魔方的每一塊都用三維坐標(biāo)(h, k, l)標(biāo)記,其中h, k, l∈{0,1}。六個(gè)面的每一個(gè)都有四個(gè)小面,每個(gè)小面都有一個(gè)正整數(shù)。

對(duì)于每一步,東東可以選擇一個(gè)特定的面,并把此面順時(shí)針或逆時(shí)針轉(zhuǎn)90度。

請(qǐng)你判斷,是否東東可以在一個(gè)步驟還原這個(gè)魔方(每個(gè)面沒有異色)。
input

** output **
對(duì)于每個(gè)測試用例,魔方如果可以至多 “只轉(zhuǎn)一步” 恢復(fù),輸出YES,則輸出NO
sample input

4 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 6 6 6 6 1 1 1 1 2 2 2 2 3 3 3 3 5 5 5 5 4 4 4 4 1 4 1 4 2 1 2 1 3 2 3 2 4 3 4 3 5 5 5 5 6 6 6 6 1 3 1 3 2 4 2 4 3 1 3 1 4 2 4 2 5 5 5 5 6 6 6 6

sample output

YES YES YES NO

想法

每次只能轉(zhuǎn)一次,每次只能轉(zhuǎn)90度,每次旋轉(zhuǎn)可以看做是交換了八個(gè)點(diǎn)的值,這樣按照點(diǎn)的對(duì)應(yīng)關(guān)系我們就能寫出每個(gè)面順勢針旋轉(zhuǎn)90度所對(duì)應(yīng)函數(shù),每次旋轉(zhuǎn)都要判斷魔方是否復(fù)原,由judge函數(shù)實(shí)現(xiàn),逆時(shí)針旋轉(zhuǎn)90度相當(dāng)于順時(shí)針旋轉(zhuǎn)270度,也就是轉(zhuǎn)三次,當(dāng)對(duì)一個(gè)面兩種旋轉(zhuǎn)都不能未復(fù)原就要再轉(zhuǎn)一次恢復(fù)原狀態(tài)換下一個(gè)面再判斷。

代碼

#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; }

總結(jié)

以上是生活随笔為你收集整理的程序设计思维与实践 Week10限时模拟 东东的魔方的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。