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. 【bzoj1455】罗马游戏 可并堆

    2016-05-31  10:04:41 可并堆的裸题. 左偏树(小根堆为例 性质 1.满足堆的性质,每个节点权值小于左右儿子权值 2.每个节点有dis值,表示子树最浅的叶子深度加1 3.左子树dis ...

  2. CodeForces 670D1 暴力或二分

    今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1   This problem is given in two versions that diff ...

  3. XML 参考:XML基础 XML 简介

    XML 参考:XML基础 -- XML简介和用途 转:http://www.cnblogs.com/Dlonghow/archive/2009/01/22/1379799.html XML 参考:XM ...

  4. Java_Eclipse安装Git插件

    一.从官网选择系统版本下载Git并安装 地址:https://git-scm.com/downloads/ 二.打开Eclipse 1. 第一种安装方法: help-->Install New ...

  5. jQuery获取Ajax函数的返回值

    参考自: http://blog.csdn.net/crx05/article/details/7362252 function test() { var myText = ""; ...

  6. lightetreeview

    http://www.16css.com/menu/905.html 其他树形菜单:很好--1.http://www.cnblogs.com/zhhh/archive/2011/11/25/22637 ...

  7. Mysql日期时间大全

    MySQL日期时间函数大全 DAYOFWEEK(date)  返回日期date是星期几(1=星期天,2=星期一,--7=星期六,ODBC标准)mysql> select DAYOFWEEK('1 ...

  8. SQLiteDatabase浅谈

    (一).简介: Android通过 SQLite 数据库引擎来实现结构化数据的存储.在一个数据库应用程序中,任何类都可以通过名字对已经创建的数据库进行访问,但是在应用程序之外就不可以. SQLite  ...

  9. IOS第11天(3:UIPickerView省市联动)

    ********* #import "ViewController.h" #import "Province.h" @interface ViewControl ...

  10. profile

    项目开发中,使用git最为版本控制工具,往往会建立开发分支.测试分支和生产分支. 各个分支的数据库url,所依赖的接口url可能不同,直接配置的话,在合并分支时往往出现冲突.使用profile可以有效 ...