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 con guration 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 con guration 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 fi nd 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 signi cance.The input consists of several con guration maps in the format
described, separated by blank lines. You may assume that each con guration is both valid and solvable. The end of input is denoted by an "empty" con guration 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 confi guration 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

题目虽长,可是题意非常easy。就是一个拼魔方的游戏,要求输出步骤

事实上代码的思路也不难,仅仅是写起来非常麻烦蛋疼

从图中不难看出。不管魔方怎么旋转,与原点相接的那个小方块是不动的,那么我们能够由原点的小方块得出三个面的终于颜色,然后再通过这三个面去确定其它三个面的颜色

然后就是IDA的剪枝估測函数的h值,因为每次旋转可以改变8个小面,那么仅仅要求出如今不在其位的面总数SUM,得出(sum+7)/8就可以,加7是保证sum+7>=8得出步数

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

HDU3459:Rubik 2&#215;2&#215;2(IDA)的更多相关文章

  1. winform 窗体最大化 分类: WinForm 2014-07-17 15:57 215人阅读 评论(0) 收藏

    1:窗体首次加载时最大化 (1):主窗体 this.WindowState = FormWindowState.Maximized; //窗体显示中间部分,不显示窗体名称和最小化.最大化.关闭按钮   ...

  2. 烂泥:apache虚拟主机的学习与应用

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 要配置apache的虚拟主机,我们需要分以下几步进行: 1. 检查apache虚拟主机模块 2. 开启apache虚拟主机功能 3. httpd-vho ...

  3. Facebook存储技术方案:找出“暖性BLOB”数据

    Facebook公司已经在其近线存储体系当中彻底弃用RAID与复制机制,转而采用分布式擦除编码以隔离其所谓的“暖性BLOB”. 暖性?BLOB?这都是些什么东西?大家别急,马上为您讲解: BLOB—— ...

  4. Win10系列:C#应用控件基础10

    PasswordBox控件 在应用程序的登录界面中,有时需要用户输入用户名和密码进行身份验证,应用程序中的密码框可以通过使用PasswordBox控件来实现.PasswordBox控件与TextBox ...

  5. elasticsearch系列三:索引详解(分词器、文档管理、路由详解(集群))

    一.分词器 1. 认识分词器  1.1 Analyzer   分析器 在ES中一个Analyzer 由下面三种组件组合而成: character filter :字符过滤器,对文本进行字符过滤处理,如 ...

  6. elasticsearch最全详细使用教程:入门、索引管理、映射详解、索引别名、分词器、文档管理、路由、搜索详解

    一.快速入门1. 查看集群的健康状况http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 状 ...

  7. 75.Python中ORM聚合函数详解:Sum

    Sum:某个字段的总和. 1. 求图书的销售总额,示例代码如下: from django.http import HttpResponse from django.db import connecti ...

  8. Prometheus(一):Web服务环境监控

    写在前面 现每个后端的同学的日常都在跟服务(接口)打交道,维护老的比较大单体应用.按业务拆得相对比较细的新服务.无论企业内部用的,面向用户的前端的服务.流量大的有流量小的,有重要的有不那么重要的. 但 ...

  9. 学习 IOC 设计模式前必读:依赖注入的三种实现

    一直以来就是越难的东西越值钱! 嘿嘿,这篇博文章转载自:http://www.cnblogs.com/liuhaorain/p/3747470.html 摘要 面向对象设计(OOD)有助于我们开发出高 ...

随机推荐

  1. JetBrains系列产品激活

    注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.codebeta.cn https://s.tuzhi ...

  2. 项目中常用的js方法(持续更新)

    <script> var utils = { //时间戳转日期(timestamp:时间戳 默认当前时间) dateFormat: function(timestamp = new Dat ...

  3. 前端打包--source-map=false作用

    参考:http://www.cnblogs.com/axl234/p/6500534.htmlng serve默认会产生.map文件,该文件保存有原始代码与运行代码的映射关系, 浏览器可以通过它找到原 ...

  4. POJ-1724 深搜剪枝

    这道题目如果数据很小的话.我们通过这个dfs就可以完成深搜: void dfs(int s) { if (s==N) { minLen=min(minLen,totalLen); return ; } ...

  5. Hibernate的持久化对象配置

    定义Pojo对象和**.hbm.xml文件 -1 对于每一个需要持久化的对象都需要创建一个Pojo类定义,Hibernate要求POJO类定义中必须有一个no-argument的构造方法,便于Hibe ...

  6. 谈一谈flex布局使用中碰到的一些问题

    起因 工作以后由于大量使用到了flex布局而碰到了一些尚不清楚的问题,以及一些有意思的特性,在此写篇博客记录一下. flex三个值的含义 众所周知,flex布局所有的属性有两种:一种作用在弹性容器(F ...

  7. Dev Express中Dock panel的使用

    使用DockManager,添加DockPanel. 1,DockManager位于“导航和布局”分类中. 添加一个DockManager控件到窗体中以后,即是在当前窗体类中,添加一个DockMana ...

  8. JavaEE JDBC 了解数据库连接池

    了解数据库连接池 @author ixenos 数据库连接是有限的资源,如果用户需要离开应用一段时间,那么他占用的连接就不应该保持开放状态: 另一方面,每次查询都获取连接并在随后关闭它的代价也很高. ...

  9. [Istio]流量管理API v1alpha3路由规则

    Istio提供一个API进行流量管理,该API允许用户将请求路由到特定版本的服务,为弹性测试注入延迟和失败,添加断路器等,所有这些功能都不必更改应用程序本身的代码.Istio 1.0中引入新的流量管理 ...

  10. BZOJ2662[BeiJing wc2012]冻结【SPFA】

    “我要成为魔法少女!” “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切,封印于卡片之中„„”        在这个愿望被实现以后的世界里,人们享受着魔法卡片(SpellCard ...