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. 崩溃恢复(crash recovery)与 AUTORESTART参数

    关于这个参数设置的影响,在生产系统中经历过两次:        第一次是有套不太重要的系统安装在虚拟机,这套系统所有应用(DB2 WAS IHS)都配置到/etc/rc.local中,每次启动机器会自 ...

  2. POJ 1321 简单dfs

    1.POJ 1321  棋盘问题 2.总结: 题意:给定棋盘上放k个棋子,要求同行同列都不重. #include<iostream> #include<cstring> #in ...

  3. U-Boot命令大全(功能参数及用法)

    U-Boot上电启动后,按任意键可以退出自动启动状态,进入命令行. U-Boot 2010.03 (Sep 25 2011 - 16:18:50)     DRAM: 64 MB     Flash: ...

  4. be supposed to

    be supposed to 期望; 認為必須, 認為應該; 認為...... 期望; 認為必須, 認為應該; 認為...必要 Am I supposed to clean all the rooms ...

  5. Node.js执行存储过程

    直接上代码 var sql = require('mssql'); var config = {     user: 'sa',     password: '123456',     server: ...

  6. StoryBoard--看上去很美

    StoryBoard--看上去很美 介绍 StoryBoard 是苹果在 2011 年的 WWDC Session 309<Introducing Interface Builder Story ...

  7. CAS单点登录配置

    见http://download.csdn.net/detail/u010786672/6942715下载.

  8. HDU2955 背包DP

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. JSP页面以及简单的指令

    —JSP(Java Server Pages)是指: —在HTML中嵌入Java脚本语言 —由应用服务器中的JSP引擎来编译和执行嵌入的Java脚本语言命令 —然后将生成的整个页面信息返回给客户端 页 ...

  10. java中hashCode方法与equals方法的用法总结

    首先,想要明白hashCode的作用,必须要先知道Java中的集合. 总的来说,Java中的集合(Collection)有两类,一类是List,再有一类是Set. 前者集合内的元素是有序的,元素可以重 ...