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. Java数据结构和算法(二)--队列

    上一篇文章写了栈的相关知识,而本文会讲一下队列 队列是一种特殊的线性表,在尾部插入(入队Enqueue),从头部删除(出队Dequeue),和栈的特性相反,存取数据特点是:FIFO Java中queu ...

  2. Java BufferedReader文件读取 带缓冲区的字符流

    package org.jimmy.autosearch2019.test; import java.io.BufferedReader; import java.io.FileInputStream ...

  3. CF1179D Fedor Runs for President [DP,斜率优化]

    Codeforces 思路 考虑把连的那两个点中间的链提出来,那么就会变成一条链,链上的每个点挂着一棵子树的形式. 设那些子树的大小为\(S_1,S2,\cdots\),那么新加的简单路径个数就是 \ ...

  4. DI:Defect Index(缺陷率)

    DI:Defect Index(缺陷率) 定义:DI值是衡量软件质量的高低的指标之一. 公式:DI= 致命级别的问题个数*10+严重级别的问题个数*3+一般级别的问题个数*1+提示级别的问题个数*0. ...

  5. MyBatis的动态sql小练习,小回顾

    关键字if+trim trim可以去除多余的关键字,是where和set的组合 trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码: <trim prefix=& ...

  6. 21. SCHEMATA

    21. SCHEMATA 在MySQL中,SCHEMA是数据库,因此SCHEMATA表提供有关数据库的信息. SCHEMATA表有以下列: CATALOG_NAME :SCHEMA所属目录的名称.该值 ...

  7. systemverilog:task

    1.task declaration 个人喜欢ANSI C格式的声明: task mytask(output int x,input logic y); ...... endtask 注意端口列表的方 ...

  8. LeetCode(21)Merge Two Sorted Lists

    题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  9. NBUT 1651 - Red packet (求运气王的钱数)(二分法)

    Description New Year is coming! Our big boss Wine93 will distribute some “Red Package”, just like Al ...

  10. luoguT21777

    #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...