http://acm.hdu.edu.cn/showproblem.php?pid=3459

Rubik 2×2×2

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others)

Total Submission(s): 654    Accepted Submission(s): 234

Special Judge

Problem Description
Sonny is probably the only computer science Ph.D. student who cannot solve a Rubik's cube. One day, he came across a neat little 2×2×2 Rubik's cube, and thought, "Finally, here's a cube that's easy enough for me to do!" Nope, wrong! He got pwned, hardcore. How embarrassing.To ensure that this does not happen again, he decides to write a computer program to solve the cube. Then he had this brilliant idea: Why not have the students at the programming contest do the work instead? So, given an initial conguration of the 2×2×2 Rubik's cube, your task for this problem is to write a program that solves it. The mini-cube has 6 faces, each with 4 painted tiles on it. The faces are labeled Front (F), Back (B),Up (U), Down (D), Left (L), and Right (R), according to the diagram below. Each of the tiles on the faces can be colored Red (R), Green (G), Blue (B), Yellow (Y), Orange (O), or White (W), and there are exactly 4 instances of each color. The cube is considered solved when the colors of all tiles on each distinct face of the cube match.You may use any combination of three distinct moves to transform the cube: a turn about the X-axis,a turn about the Y-axis, or a turn about the Z-axis. Each turn is exactly 90 degrees of all tiles on half the cube, in the directions illustrated below. Note that the back-down-left corner is fixed with respect to all valid transforms.Can you come up with a sequence of moves that will solve a given conguration of the Rubik's cube?
 
Input
You will be given maps of an "unwrapped" cubes showing colors on each of the faces, in the following format:The letters in the above diagram shows you where to find the colors on each face (as shown in the first diagram) from the map only { it is not valid input! The front face is oriented as in the diagram, with the other faces on the map attached to it so that it wraps to cover the cube. The letters on the faces may be any of R, G, B, Y, O, or W to indicate the color. Dot (.) characters serve to pad the map to a 6 × 8 grid,and are of no other signicance.The input consists of several conguration maps in the format described, separated by blank lines. You may assume that each conguration is both valid and solvable. The end of input is denoted by an "empty" conguration consisting solely of `.' characters; do not process this map.
 
Output
For each cube, output on a single line a sequence of moves that will solve the cube. Output `X' for a turn about the X-axis, `Y' for a turn about the Y-axis, and `Z' for a turn about the Z-axis. Any sequence of moves (that is reasonably finite) which solves the given configuration will do. (After all, Sonny does need to execute your commands to verify that your program works!) A blank line will suffice for an input cube that is already solved.
 
Sample Input
..WO....
..WO....
BBOYGGWR
BBOYGGWR
..YR....
..YR....

 
..GY....
..BY....
ROYWRRBB
GWOWRBOW
..YG....
..OG....

 
........
........
........
........
........
........
 
 
Sample Output
X
YZXXXZYZXYXYZZYZZYZXYY
 
Source

分析:

题目不难理解,就是求还原的最小步数。

从图中不难看出,无论魔方怎么旋转,与原点相接的那个小方块是不动的,那么我们可以由原点的小方块得出三个面的最终颜色,然后再通过这三个面去确定其他三个面的颜色

然后就是IDA的剪枝估测函数的h值,由于每次旋转能够改变8个小面,那么只要求出现在不在其位的面总数SUM,得出(sum+7)/8即可,加7是保证sum+7>=8得出步数。

AC代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct node
{
int x,y;
} cube[][],side[][]; char color[],rubik[][];
int ans[];
int flag,step; void init()//cube代表每个小立方体的3个面对应字符数字的哪个位置,side则是6个面,每个面的四个元素分别是什么
{
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
cube[][].x=,cube[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
side[][].x=,side[][].y=;
} char get_color(int A,int B,int C) //通过小格子的颜色获得每个面的颜色
{
for(int i=; i<; i++)
{
if(rubik[cube[i][].x][cube[i][].y]==color[A]&&rubik[cube[i][].x][cube[i][].y]==color[B]&&rubik[cube[i][].x][cube[i][].y]!=color[C])
return rubik[cube[i][].x][cube[i][].y];
if(rubik[cube[i][].x][cube[i][].y]==color[A]&&rubik[cube[i][].x][cube[i][].y]==color[B]&&rubik[cube[i][].x][cube[i][].y]!=color[C])
return rubik[cube[i][].x][cube[i][].y];
if(rubik[cube[i][].x][cube[i][].y]==color[A]&&rubik[cube[i][].x][cube[i][].y]==color[B]&&rubik[cube[i][].x][cube[i][].y]!=color[C])
return rubik[cube[i][].x][cube[i][].y];
if(rubik[cube[i][].x][cube[i][].y]==color[A]&&rubik[cube[i][].x][cube[i][].y]==color[B]&&rubik[cube[i][].x][cube[i][].y]!=color[C])
return rubik[cube[i][].x][cube[i][].y];
if(rubik[cube[i][].x][cube[i][].y]==color[A]&&rubik[cube[i][].x][cube[i][].y]==color[B]&&rubik[cube[i][].x][cube[i][].y]!=color[C])
return rubik[cube[i][].x][cube[i][].y];
if(rubik[cube[i][].x][cube[i][].y]==color[A]&&rubik[cube[i][].x][cube[i][].y]==color[B]&&rubik[cube[i][].x][cube[i][].y]!=color[C])
return rubik[cube[i][].x][cube[i][].y];
}
} void turn_x(char maze[][]) //x轴
{
char tmp;
tmp=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=tmp;
tmp=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=tmp;
tmp=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=tmp;
}
void turn_y(char maze[][]) //y轴
{
char tmp;
tmp=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=tmp;
tmp=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=tmp;
tmp=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=tmp;
}
void turn_z(char maze[][]) //z轴
{
char tmp;
tmp=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=tmp;
tmp=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=tmp;
tmp=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=maze[][];
maze[][]=tmp;
} int get_h(char mid[][])
{
int i,j,sum = ;
for(i = ; i<; i++)
{
for(j = ; j<; j++)
{
if(mid[side[i][j].x][side[i][j].y]!=color[i])
sum++;
}
}
return (sum+)/;
} int IDA(char mid[][],int cnt)
{
if(cnt+get_h(mid)>step)
return ;
if(cnt == step)
return ;
for(int i = ; i<; i++)
{
char tem[][];
for(int x = ; x<; x++)
for(int y = ; y<; y++)
tem[x][y]=mid[x][y];
if(i == )
turn_x(tem);
else if(i == )
turn_y(tem);
else
turn_z(tem);
ans[cnt] = i;
if(IDA(tem,cnt+))
return ;
}
return ;
}
int main()
{
int i;
init();
while(~scanf("%s",rubik[]))
{
for(i = ; i<; i++)
scanf("%s",rubik[i]);
if(!strcmp(rubik[],"........"))
break;
color[]=rubik[][];
color[]=rubik[][];
color[]=rubik[][];
color[]=get_color(,,);
color[]=get_color(,,);
color[]=get_color(,,);
step = ;
while()
{
if(IDA(rubik,)) break;
step++;
}
for(i = ; i<step; i++)
printf("%c",ans[i]+'X');
printf("\n");
} return ;
}

hduoj 3459 Rubik 2×2×2的更多相关文章

  1. 【HDOJ】3459 Rubik 2×2×2

    模拟+DFS. /* 3459 */ #include <cstdio> #include <cstring> #include <cstdlib> #define ...

  2. HDU3459:Rubik 2&#215;2&#215;2(IDA)

    Problem Description Sonny is probably the only computer science Ph.D. student who cannot solve a Rub ...

  3. Android Weekly Notes Issue #222

    Android Weekly Issue #222 September 11th, 2016 Android Weekly Issue #222 ARTICLES & TUTORIALS Fo ...

  4. PIC10F200/202/204/206/220/222/320/322芯片解密程序复制多少钱?

    PIC10F200/202/204/206/220/222/320/322芯片解密程序复制多少钱? PIC10F单片机芯片解密型号: PIC10F200解密 | PIC10F202解密 | PIC10 ...

  5. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  6. Acadia Lab 228 + Lab 222

    又是一对串烧实验,布好线后非常方便就可以一起完成. 连线方案一模一样: Lab 228 数码管骰子 核心代码如下: def loop() : global cnt global btn_read,se ...

  7. 微软Nokia 222:可拍照可上网 售价37美元 32GB的microSD卡扩展

    腾讯科技讯 8月27日,在几乎所有厂商都在智能手机领域大肆拼杀的时候,微软日前却悄悄地发布了一款功能手机Nokia 222. 目前,尽管全球许多发达国家的居民都对互联网已经再熟悉不过了,但事实上全球依 ...

  8. 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

    package com.lw.HomeWork1;//包名 2 import java.util.Scanner; public class Demo18 { /** * @param args */ ...

  9. sdutoj 2606 Rubik’s cube

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2606 Rubik’s cube Time Li ...

随机推荐

  1. iphone H5 input type="search" 不显示搜索 解决办法

    H5 input type="search" 不显示搜索 解决办法 H5 input type="search" 不显示搜索 解决方法 在IOS(ipad iP ...

  2. js 图片处理 Jcrop.js API

    引入jquery.Jcrop.min.css和jquery.Jcrop.min.js 参数/接口说明 options 参数说明 名称 默认值 说明 allowSelect true 允许新选框 all ...

  3. IOS 蓝牙相关-基础知识(1)

    蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...

  4. 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke

    今天关闭一个窗体,报出这样的一个错误"在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke.",这个不用多想,肯定是那个地方没有释放掉.既然碰到这个问题, ...

  5. Java数组转成list,list转数组

    下面介绍一下Java中数组和List集合如何互相转换. 数组转成list 第一种: String[] userid = {"aa","bb","cc& ...

  6. [CareerCup] 17.11 Rand7 and Rand5 随机生成数字

    17.11 Implement a method rand7() given rand5(). That is, given a method that generates a random numb ...

  7. django向数据库添加数据

    url.py views.py host.html (样式) (展示部分) (添加信息界面) (js部分) 展示添加数据:

  8. jQuery获取cookie

    之前一直以为获取cookie的方法封装在了jQuery包中...没想到还得单独下jquery.cookie.js插件,不太好找,备份一份: /*! * jQuery Cookie Plugin v1. ...

  9. mongoDB01 介绍

    MongoDB是一个开源文档型数据库,能够提供高性能.高可用性以及自动拓展. 文档数据库 MongoDB中的一条记录就是一个文档,是一个数据结构,由字段和值对组成.MongoDB文档与JSON对象类似 ...

  10. (转)MySQL优化实例

    在Apache, PHP,MySQL的体系架构中,MySQL对于性能的影响最大,也是关键的核心部分.对于Discuz!论坛程序也是如此,MySQL的设置是否合理优化,直接影响到论坛的速度和承载量!同时 ...