HDU 4801 Pocket Cube BFS

链接:http://vjudge.net/contest/view.action?cid=51289#problem/K

题意:给一个小魔方(每面只有四个小块),每个小块有自己的颜色(一共六种颜色,每种颜色四块),问经过最多N次操作(N<=7),每次操作是将魔方某一面转动90度,最多能使多少个面的四个小块颜色都相同。

思路:每次操作产生6种状态,(向前,向后,向左,向右,向上,向下,左半魔方向前和右半魔方向后的操作产生的状态本质是一样的)直接BFS搜索,可能产生的状态一共有6^7种,中间比较麻烦的是状态的变化。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <set>
#define PI acos(-1.0)
#define maxn 100005
#define INF 0x7fffffff
#define eps 1e-8
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
struct Matrix
{
    int step;
    int a[26];
}st,s1,s;
int n;
int BFS()
{
    int ans=0;
    queue <Matrix> q;
    while(!q.empty())
        q.pop();
    q.push(st);
    while(!q.empty())
    {
        s1=q.front();
        int res=(s1.a[7]==s1.a[6]&&s1.a[7]==s1.a[13]&&s1.a[7]==s1.a[12])+
                (s1.a[0]==s1.a[1]&&s1.a[0]==s1.a[2]&&s1.a[0]==s1.a[3])+
                (s1.a[16]==s1.a[17]&&s1.a[16]==s1.a[18]&&s1.a[16]==s1.a[19])+
                (s1.a[20]==s1.a[21]&&s1.a[20]==s1.a[22]&&s1.a[20]==s1.a[23])+
                (s1.a[4]==s1.a[5]&&s1.a[4]==s1.a[10]&&s1.a[4]==s1.a[11])+
                (s1.a[8]==s1.a[9]&&s1.a[8]==s1.a[14]&&s1.a[8]==s1.a[15]);
            if(res>ans)
            {
                ans=res;
                if(ans==6)
                    return ans;
            }
        q.pop();
        if(s1.step<n)
        {
        int a,b;
        s=s1;a=s.a[1];b=s.a[3];
        s.a[1]=s.a[7];s.a[3]=s.a[13];s.a[7]=s.a[17];s.a[13]=s.a[19];
        s.a[17]=s.a[21];s.a[19]=s.a[23];s.a[21]=a;s.a[23]=b;
        a=s.a[9];
        s.a[9]=s.a[8];s.a[8]=s.a[14];s.a[14]=s.a[15];s.a[15]=a;
        s.step=s1.step+1;
        q.push(s);
        s=s1;a=s.a[1];b=s.a[3];
        s.a[1]=s.a[21];s.a[3]=s.a[23];s.a[21]=s.a[17];s.a[23]=s.a[19];
        s.a[17]=s.a[7];s.a[19]=s.a[13];s.a[7]=a;s.a[13]=b;
        a=s.a[8];
        s.a[8]=s.a[9];s.a[9]=s.a[15];s.a[15]=s.a[14];s.a[14]=a;
        s.step=s1.step+1;
        q.push(s);
        s=s1;a=s.a[6];b=s.a[7];
        s.a[6]=s.a[8];s.a[7]=s.a[9];s.a[8]=s.a[23];s.a[9]=s.a[22];
        s.a[23]=s.a[4];s.a[22]=s.a[5];s.a[4]=a;s.a[5]=b;
        a=s.a[0];
        s.a[0]=s.a[2];s.a[2]=s.a[3];s.a[3]=s.a[1];s.a[1]=a;
        s.step=s1.step+1;
        q.push(s);
        s=s1;a=s.a[6];b=s.a[7];
        s.a[6]=s.a[4];s.a[7]=s.a[5];s.a[4]=s.a[23];s.a[5]=s.a[22];
        s.a[23]=s.a[8];s.a[22]=s.a[9];s.a[8]=a;s.a[9]=b;
        a=s.a[0];
        s.a[0]=s.a[1];s.a[1]=s.a[3];s.a[3]=s.a[2];s.a[2]=a;
        s.step=s1.step+1;
        q.push(s);
        s=s1;a=s.a[3];b=s.a[2];
        s.a[3]=s.a[5];s.a[2]=s.a[11];s.a[5]=s.a[16];s.a[11]=s.a[17];
        s.a[16]=s.a[14];s.a[17]=s.a[8];s.a[14]=a;s.a[8]=b;
        a=s.a[6];
        s.a[6]=s.a[12];s.a[12]=s.a[13];s.a[13]=s.a[7];s.a[7]=a;
        s.step=s1.step+1;
        q.push(s);
        s=s1;a=s.a[3];b=s.a[2];
        s.a[3]=s.a[14];s.a[2]=s.a[8];s.a[14]=s.a[16];s.a[8]=s.a[17];
        s.a[16]=s.a[5];s.a[17]=s.a[11];s.a[5]=a;s.a[11]=b;
        a=s.a[6];
        s.a[6]=s.a[7];s.a[7]=s.a[13];s.a[13]=s.a[12];s.a[12]=a;
        s.step=s1.step+1;
        q.push(s);
        }
    }
    return ans;
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<24;i++)
            scanf("%d",&st.a[i]);
        st.step=0;
        int a=BFS();
        printf("%d\n",a);
    }
    return 0;
}

HDU 4801 Pocket Cube BFS

时间: 2025-01-04 06:33:25

HDU 4801 Pocket Cube BFS的相关文章

HDU 4801 Pocket Cube(模拟题——转魔方)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4801 题面: 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

HDU 4801 Pocket Cube

题目链接 去年现场,虎哥1Y的,现在刷刷题,找找状态... 一共6种转法,把3个面放到顶部,左旋和右旋,感觉写的还不错....都写成常数了. #include <stdio.h> #include <math.h> #include <string.h> #include <queue> #include <algorithm> #define LL long long using namespace std; struct node { int

HDU 5983 Pocket Cube

Pocket Cube Problem Description The Pocket Cube, also known as the Mini Cube or the Ice Cube, is the 2 × 2 × 2 equivalence of a Rubik's Cube.The cube consists of 8 pieces, all corners.Each piece is labeled by a three dimensional coordinate (h, k, l)

2013区域赛长沙赛区现场赛 K - Pocket Cube

K - Pocket Cube Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4801 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.

hdu 1429 状压bfs

#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f #define ll __in

HDU-3309-Roll The Cube(BFS)

Problem Description This is a simple game.The goal of the game is to roll two balls to two holes each. 'B' -- ball 'H' -- hole '.' -- land '*' -- wall Remember when a ball rolls into a hole, they(the ball and the hole) disappeared, that is , 'H' + 'B

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

[dfs] zoj 3736 Pocket Cube

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3736 Pocket Cube Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223 Accepted: 6331 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visit