1. 程式人生 > >HDU 4801 Pocket Cube(模擬題——轉魔方)

HDU 4801 Pocket Cube(模擬題——轉魔方)

題面:

Pocket Cube

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 882    Accepted Submission(s): 271


Problem Description Pocket Cube is a 3-D combination puzzle. It is a 2 × 2 × 2 cube, which means it is constructed by 8 mini-cubes. For a combination of 2 × 2 mini-cubes which sharing a whole cube face, you can twist it 90 degrees in clockwise or counterclockwise direction, this twist operation is called one twist step.
Considering all faces of mini-cubes, there will be totally 24 faces painted in 6 different colors (Indexed from 0), and there will be exactly 4 faces painted in each kind of color. If 4 mini-cubes' faces of same color rely on same large cube face, we can call the large cube face as a completed face.



Now giving you an color arrangement of all 24 faces from a scrambled Pocket Cube, please tell us the maximum possible number of completed faces in no more than N twist steps.
Index of each face is shown as below:



Input There will be several test cases. In each test case, there will be 2 lines. One integer N (1 ≤ N ≤ 7) in the first line, then 24 integers Ci separated by a single space in the second line. For index 0 ≤ i < 24, Ci is color of the corresponding face. We guarantee that the color arrangement is a valid state which can be achieved by doing a finite number of twist steps from an initial cube whose all 6 large cube faces are completed faces.
Output For each test case, please output the maximum number of completed faces during no more than N twist step(s).
Sample Input 1 0 0 0 0 1 1 2 2 3 3 1 1 2 2 3 3 4 4 4 4 5 5 5 5 1 0 4 0 4 1 1 2 5 3 3 1 1 2 5 3 3 4 0 4 0 5 2 5 2
Sample Output 6 2
Source

解題:

    轉魔方,注意對應位置別弄錯就好,深搜,廣搜都可以。就是比較難調。

總結:

    一些重複性程式碼,最好寫成函式呼叫,減少程式碼的冗餘性,同時提高程式碼的準確性。

程式碼:

#include <iostream>
#include <cstdio>
using namespace std;
int cube[24],tmp,a,b,c,ans,n;
int check()
{
	int res=0;
	tmp=cube[0];
	if(tmp==cube[1]&&tmp==cube[2]&&tmp==cube[3])res++;
	tmp=cube[4];
	if(tmp==cube[5]&&tmp==cube[10]&&tmp==cube[11])res++;
	tmp=cube[6];
	if(tmp==cube[7]&&tmp==cube[12]&&tmp==cube[13])res++;
    tmp=cube[8];
	if(tmp==cube[9]&&tmp==cube[14]&&tmp==cube[15])res++;
    tmp=cube[16];
	if(tmp==cube[17]&&tmp==cube[18]&&tmp==cube[19])res++;
    tmp=cube[20];
	if(tmp==cube[21]&&tmp==cube[22]&&tmp==cube[23])res++;
    return res;
}
void rotate(int oper)
{
    if(oper==1)
	{
		a=cube[1];
		b=cube[3];
		c=cube[9];
		cube[1]=cube[7];
		cube[3]=cube[13];
		cube[7]=cube[17];
		cube[13]=cube[19];
		cube[17]=cube[21];
		cube[19]=cube[23];
		cube[21]=a;
		cube[23]=b;
		cube[9]=cube[8];
		cube[8]=cube[14];
        cube[14]=cube[15];
		cube[15]=c;
	}
	else if(oper==6)
    {
		a=cube[7];
		b=cube[13];
		c=cube[14];
		cube[7]=cube[1];
		cube[13]=cube[3];
		cube[1]=cube[21];
		cube[3]=cube[23];
		cube[21]=cube[17];
		cube[23]=cube[19];
		cube[17]=a;
		cube[19]=b;
		cube[14]=cube[8];
		cube[8]=cube[9];
        cube[9]=cube[15];
		cube[15]=c;
	}
    else if(oper==2)
    {
		a=cube[8];
		b=cube[14];
		c=cube[7];
		cube[8]=cube[17];
		cube[14]=cube[16];
		cube[17]=cube[11];
		cube[16]=cube[5];
		cube[11]=cube[2];
		cube[5]=cube[3];
		cube[2]=a;
		cube[3]=b;
		cube[7]=cube[13];
		cube[13]=cube[12];
        cube[12]=cube[6];
		cube[6]=c;
	}
    else if(oper==5)
    {
		a=cube[16];
		b=cube[17];
		c=cube[12];
		cube[17]=cube[8];
		cube[16]=cube[14];
		cube[8]=cube[2];
		cube[14]=cube[3];
		cube[2]=cube[11];
		cube[3]=cube[5];
		cube[11]=b;
		cube[5]=a;
		cube[12]=cube[13];
		cube[13]=cube[7];
        cube[7]=cube[6];
		cube[6]=c;
	}
    else if(oper==3)
    {
		a=cube[12];
		b=cube[13];
		c=cube[16];
		cube[12]=cube[14];
		cube[13]=cube[15];
		cube[14]=cube[21];
		cube[15]=cube[20];
		cube[21]=cube[10];
		cube[20]=cube[11];
		cube[10]=a;
		cube[11]=b;
		cube[16]=cube[17];
		cube[17]=cube[19];
        cube[19]=cube[18];
		cube[18]=c;
	}
	else
    {
		a=cube[12];
		b=cube[13];
		c=cube[16];
		cube[12]=cube[10];
		cube[13]=cube[11];
		cube[10]=cube[21];
		cube[11]=cube[20];
		cube[20]=cube[15];
		cube[21]=cube[14];
		cube[14]=a;
		cube[15]=b;
		cube[16]=cube[18];
		cube[18]=cube[19];
        cube[19]=cube[17];
		cube[17]=c;
	}
}
void search(int oper,int step)
{
	rotate(oper);
	tmp=check();
	if(tmp>ans)ans=tmp;
    if(step==n)
	{
		rotate(7-oper);
		return;
	}
	for(int i=1;i<=6;i++)
	{
		if(i!=(7-oper))
		{
			search(i,step+1);
		}
	}
    rotate(7-oper);
}
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=0;i<24;i++)
			scanf("%d",&cube[i]);
        ans=check();
		for(int i=1;i<=6;i++)
			search(i,1);
		printf("%d\n",ans);
	}
	return 0;
}


相關推薦

HDU 4801 Pocket Cube模擬——魔方

題面: Pocket Cube Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 882    Acce

HDU 4801 Pocket Cube暴力模擬 dfs

題意:求旋轉次數小於等於n(每次旋轉90度)的情況下,求出魔方拼出完整的面的最大面數。 AC程式碼: #include <cstdio> #include <cstring&

HDU 4801 Pocket Cube (模擬)

題意: 給出一個2階魔方,問在少於N步旋轉內,最多產生多少個同樣顏色的面? 思路: 共6個有效方向,模擬即可。 有一個坑點,顏色不是0-5的,是 Integer 。判重時注意下即可 程式碼: #include <bits/stdc++.h> using

HDU 6023-Automatic Judge模擬

分析 主要就是多次ac取時間最短的那次。 -=和%=1000的問題 如果沒有ac則罰時不計,都知道的 我卻忘考慮了。 #include<bits/stdc++.h> using namespace std; const int maxn=1e5+5; struct

HDU 5983 Pocket Cube模擬

題意:2*2*2的魔方只轉一次,問是否能讓魔方六面都相等 思路: 用陣列記錄每個面的數字,並且從1到24編號,注意這裡要根據題目要求的輸入順序編號,能一次就完成任務的情況只有兩種,一種是六面都已經滿足條件了,第二種是存在兩個對立面相等的情況,只限兩個,多一個都不行;對

HDU 5983-Pocket Cube一步還原二階魔方

The first line of input contains one integer N(N ≤ 30) which is the number of test cases. For each test case, the first line describes the top face of the

HDU 5097 Page Rank模擬,矩陣運算

題意:有點難讀懂,我是跟著圖YY的。 輸入一個N*N的01矩陣A,先將它轉換成類似圖片裡面的矩陣S。 具體做法是,對於第i行,求出1的個數cnt,然後如果A[i][j]是1,B[j][i]就等於1/cnt,否則等於0。注意這裡i , j前後是相反的。 然後根據題目裡面的公

hdu 5983 Pocket Cube(模擬)

Pocket Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s):

HDU--5983 Pocket Cube

連結:https://www.nowcoder.com/acm/contest/207/B 來源:牛客網   題目描述 The Pocket Cube, also known as the Mini Cube or the Ice Cube, is the 2×2×2 equiva

Berland Fair CodeForces - 1073D模擬

題意:n個數排成一個圓盤,一個人有T元錢,從第一個開始,只要他能買的,他一定會買,這樣依次迴圈,直到他的錢不能買任何物品時結束,問,他能買多少件東西。 思路:按題意模擬即可,不過T太大,考慮優化,優化的方法是當n個數的最大值都比T小,並且n個數的sum也小於T時,我們直接一次性買n個即可。

pat 甲級 1080模擬

題目連結:https://pintia.cn/problem-sets/994805342720868352/problems/994805387268571136 思路: 1、題目大意是,模擬高考錄取學生的過程,每個學生有三種成績 (1)GE錄取成績(2)GI面試成績(3)最終成績GF

pat 甲級 1100模擬

題目連結:https://pintia.cn/problem-sets/994805342720868352/problems/994805367156883456 思路:(1)整行輸入,分別對數字和字串處理 (2)對字串要判斷是一串還是兩串,然後判斷是十位還是個位 (3)對數字判斷十位

Capture the Flag ZOJ - 3879模擬

  In computer security, Capture the Flag (CTF) is a computer security competition. CTF contests are usually designed to serve as an educational exerc

Score 模擬

  #include<stdio.h> #include<string.h> char a[10000+10]; int main() { int n,m,j,k,i,T; scanf("%d",&T); getchar(); whi

hdu 1050 Moving Tables 模擬

Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following fig

福建工程大ACM程式設計E分配物資模擬

連結:https://ac.nowcoder.com/acm/contest/289/E 題目描述 要將n種物資分配到m個村莊中.村莊按照1,2,3...m編號. 已知第i種物資有Ai個. 第i個村莊只需某一種物資Bi個. 物資按照村莊編號升序發放. 問第i個村莊能否

HDU-4054.Hexadecimal View模擬,十六進位制轉換

M - M Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Hexadecimal is very important and

今年暑假不AC HDU - 2037 -簡單貪心

簡單貪心:每次選擇結束時間最早的,因此先用sort函式排序,之後就簡單了 #include <stdio.h> #include <algorithm> #include <stdlib.h> using namespace std; t

CCF CSP 2015年12月第3 畫圖 模擬+DFS或BFS

問題描述 試題編號: 201512-3 試題名稱: 畫圖 時間限制: 1.0s 記憶體限制: 256.0MB 問題描述: 問題描述   用 ASCII 字元來畫圖是一件有趣的事情,並形成了一門被稱為 ASCII Art 的藝術。例如,下圖是用 AS

HDU 2899搜尋,二分

Strange fuction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 80    Accepted Sub