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. sklearn之SVC

    sklearn.svm.SVC(C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, probability= ...

  2. 收集的WEB前端程序员需要的网站整理

    前端学习资源实在是又多又广,在这样的一个知识的海洋里,我们像一块海绵一样吸收,想要快速提高效率,平时的总结不可缺少,以下总结了一些,排版自我感觉良好,推送出来. 一.插件类网站 jQuery插件库:h ...

  3. jvm 内存溢出 在myeclipse中加大tomcat的jvm内存 java.lang.OutOfMemoryError: PermGen space

    使用myeclipse启动两个SSH2的部署在tomcat6下的项目 报出java.lang.OutOfMemoryError: PermGen space 解决办法: 在myeclipse中加大jv ...

  4. 解决浏览器自动填充input

    浏览器输入框自动填充解决办法 emmmmm:今天处理公司后台系统遇到的:登录页面浏览器保存账号密码后:浏览器会自动在其他页面进行填充:解决如下图: 浏览器会默认填充input type值为passwo ...

  5. Node.js实现简单的爬取

    学习[node.js]也有几天时间了,所以打算写着练练手:索然我作为一个后端的选手,写起来还有那么一丝熟悉的感觉.emmm~~  ‘货’不多讲 ,开搞........ 首先是依赖选择: 代码块如下: ...

  6. [题解] cogs 1669 神秘的咒语

    http://cogs.pro:8080/cogs/problem/problem.php?pid=1669 "The Real Incantation is Their Common In ...

  7. 用户管理命令--useradd

    用户管理命令--useradd 作用:用于添加一个新的用户 格式:useradd [ 选项 ] 用户名 选项的常用介绍 -u: UID指定用户id,必须是唯一的,并且大于499 -c: 添加注释,可以 ...

  8. 18. PROFILING

    18. PROFILING PROFILING表提供语句分析信息. 其内容对应于SHOW PROFILE和SHOW PROFILES语句生成的信息(参见"SHOW PROFILE语法&quo ...

  9. vue 项目规范

    1, 组件化 2, css 分清单独和通用的 3, 封装请求 4, 命名原则 1: 尽量和后端保持一致 2: 简单常见的单词 3: 全部小写

  10. sorted倒序

    ''' sorted 可以排列list, reverse=True 可以倒序排列 ''' # # def list_Dict(x): # m = {} # for k,v in enumerate(x ...