HDU 4801 Pocket Cube

本文介绍了一个2x2魔方的算法问题,旨在通过深度搜索算法找到在限定步数内能还原魔方的最大面数。文章提供了一段C++实现代码,通过预设的旋转步骤表来追踪每一步旋转后的魔方状态。

题目链接

Pocket Cube

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

题意

一个2阶魔方,给出初始状态和最大步数N,问在旋转N次及以内的情况下,最多能还原魔方的几个面。

题解

直接进行深搜枚举所有情况,然后求个最大值。魔方一个面有一种旋转方式(不考虑顺逆),六个面有六种,打表来记叙每一次旋转之后的魔方变化。

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int ans;
int mov[7][25]=
{
    {6,1,12,3,5,11,16,7,8,9,4,10,18,13,14,15,20,17,22,19,0,21,2,23},
    {20,1,22,3,10,4,0,7,8,9,11,5,2,13,14,15,6,17,12,19,16,21,18,23},
    {1,3,0,2,23,22,4,5,6,7,10,11,12,13,14,15,16,17,18,19,20,21,9,8},
    {2,0,3,1,6,7,8,9,23,22,10,11,12,13,14,15,16,17,18,19,20,21,5,4},
    {0,1,8,14,4,3,7,13,17,9,10,2,6,12,16,15,5,11,18,19,20,21,22,23},
    {0,1,11,5,4,16,12,6,2,9,10,17,13,7,3,15,14,8,18,19,20,21,22,23}
};
void judge(int *p)
{
    int sum=0;
    if (p[0]==p[1]&&p[1]==p[2]&& p[2]==p[3])
        sum++;
    if (p[4]==p[5]&&p[5]==p[10]&&p[10]==p[11])
        sum++;
    if (p[6]==p[7]&&p[7]==p[12]&&p[12]==p[13])
        sum++;
    if (p[8]==p[9]&&p[9]==p[14]&&p[14]==p[15])
        sum++;
    if (p[16]==p[17]&&p[17]==p[18]&&p[18]==p[19])
        sum++;
    if (p[20]==p[21]&&p[21]==p[22]&&p[22]==p[23])
        sum++;
    ans=max(ans,sum);
}
void dfs(int step,int *p)
{
    judge(p);
    if(step==0||ans==6)
        return ;
    int temp[30];
    for(int i=0; i<6; i++)
    {
        for(int j=0; j<24; j++)
            temp[j]=p[mov[i][j]];
        dfs(step-1,temp);
    }
    return ;
}
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
        ans=0;
        int mag[30];
        for(int i=0; i<24; i++)
            scanf("%d",&mag[i]);
        dfs(t,mag);
        cout<<ans<<endl;
    }
    return 0;
}

01、数据简介 出口韧性是地级市在面对外部震荡和压力时,能够承受并迅速适应、应对变化的能力。这种能力体现在地级市经济结构的灵活性、创新能力和竞争力,以及地方政府的政策支持和产业调整能力等多个方面。 城市出口韧性对于城市的经济发展、就业稳定、国际贸易地位以及风险抵御能力等方面都具有重要影响。因此,城市应加强出口韧性的建设,提高应对外部冲击的能力,以推动其经济的可持续发展。 数据名称:地级市-城市出口韧性数据 数据年份:2011-2022年 02、相关数据 代码 年份 地区 城市 省份 城市出口韧性 距离港口的最近距离 最终进口额_百万人民币2 最终出口额_百万人民币2 人均道路面积2 年末金融机构各项贷款余额万元2 地区生产总值万元2 科学支出万元2 地方财政一般预算内支出万元2 城镇居民人均可支配收入元2 固定资产投资2 实际使用外商投资额百万美元2 城镇化率2 外贸依存度 出口贸易 年平均汇率 实际使用外商投资额百万人民币2 外资依存度 金融发展水平 财政投资力度 科学技术水平 出口偏离度 x_地区生产总值万元2 x_城镇化率2 x_人均道路面积2 x_外贸依存度 x_出口贸易 x_出口偏离度 x_金融发展水平 x_城镇居民人均可支配收入元2 x_财政投资力度 x_科学技术水平 x_距离港口的最近距离 x_外资依存度 地区生产总值万元2_sum y_地区生产总值万元2 城镇化率2_sum y_城镇化率2 人均道路面积2_sum y_人均道路面积2 外贸依存度_sum y_外贸依存度 出口贸易_sum y_出口贸易 出口偏离度_sum y_出口偏离度 金融发展水平_sum y_金融发展水平 城镇居民人均可支配收入元2_sum y_城镇居民人均可支配收入元2 财政投资力度_sum y_财政投资力度 科学技术水平_sum y_科学技术水平
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值