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. PHP15 Smarty模板

    学习目标 Smarty基本概念 Smarty安装和配置 Smarty模板设计 Smarty流程控制 Smarty基本概念 一种模板引擎,在系统中进行预处理和过滤数据.是主流的PHP模板引擎,此外PHP ...

  2. 数据库课程设计 PHP web实现

    纪念一下自己写的东西.. 都说很垃圾就是了 直接用XAMPP做的 菜鸟网上学的PHP和HTML <!DOCTYPE html> <html> <head> < ...

  3. 面试奇遇 -- 原生JS

    最近几日去参加一些面试,多多少少有一些收获. 现将遇到的一些面试题,做一下分析和总结. 1.使用原生JS,不能使用递归,查找dom中所有以“<com-”开头的自定义标签tagName. < ...

  4. centos6.7升级python3.6.1

    --安装依赖包 yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel ...

  5. 电源模块PCB设计

    电源模块的PCB设计 电源电路是一个电子产品的重要组成部分,电源电路设计的好坏,直接牵连产品性能的好坏.我们电子产品的电源电路主要有线性电源和高频开关电源.从理论上讲,线性电源是用户需要多少电流,输入 ...

  6. Codeforces 5D Follow Traffic Rules

    [题意概述] 某个物体要从A途经B到达C,在通过B的时候速度不能超过vd.  它的加速度为a,最大速度为vm:AB之间距离为d,AC之间距离为L: 问物体最少花多少时间到达C. [题解] 分情况讨论. ...

  7. 如何查看Laravel版本号的三种方法

    1.PHP artisan --version 2.vim vendor/laravel/framework/src/Illuminate/Foundation/Application.php 3:可 ...

  8. 【转】Quartz.NET

    原文链接:http://www.cnblogs.com/tommyli/archive/2009/02/09/1386644.html Quartz.NET是一个开源的作业调度框架,是OpenSymp ...

  9. 为应用指定多个struts配置文件

    在大部分应用里,随着应用规模的增加,系统中Action的数量也会大量增加,导致struts.xml配置文件变得非常臃肿.为了避免struts.xml文件过于庞大.臃肿,提高struts.xml文件的可 ...

  10. 洛谷P1244 青蛙过河

    P1244 青蛙过河 362通过 525提交 题目提供者该用户不存在 标签 难度普及- 时空限制1s / 128MB 提交 讨论 题解 最新讨论更多讨论 题目什么意思 题目看不懂啊 题目描述 有一条河 ...