hduoj 3459 Rubik 2×2×2
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
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?
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.分析:
题目不难理解,就是求还原的最小步数。
从图中不难看出,无论魔方怎么旋转,与原点相接的那个小方块是不动的,那么我们可以由原点的小方块得出三个面的最终颜色,然后再通过这三个面去确定其他三个面的颜色
然后就是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的更多相关文章
- 【HDOJ】3459 Rubik 2×2×2
模拟+DFS. /* 3459 */ #include <cstdio> #include <cstring> #include <cstdlib> #define ...
- HDU3459:Rubik 2×2×2(IDA)
Problem Description Sonny is probably the only computer science Ph.D. student who cannot solve a Rub ...
- Android Weekly Notes Issue #222
Android Weekly Issue #222 September 11th, 2016 Android Weekly Issue #222 ARTICLES & TUTORIALS Fo ...
- PIC10F200/202/204/206/220/222/320/322芯片解密程序复制多少钱?
PIC10F200/202/204/206/220/222/320/322芯片解密程序复制多少钱? PIC10F单片机芯片解密型号: PIC10F200解密 | PIC10F202解密 | PIC10 ...
- hduoj 1455 && uva 243 E - Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...
- Acadia Lab 228 + Lab 222
又是一对串烧实验,布好线后非常方便就可以一起完成. 连线方案一模一样: Lab 228 数码管骰子 核心代码如下: def loop() : global cnt global btn_read,se ...
- 微软Nokia 222:可拍照可上网 售价37美元 32GB的microSD卡扩展
腾讯科技讯 8月27日,在几乎所有厂商都在智能手机领域大肆拼杀的时候,微软日前却悄悄地发布了一款功能手机Nokia 222. 目前,尽管全球许多发达国家的居民都对互联网已经再熟悉不过了,但事实上全球依 ...
- 求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 */ ...
- sdutoj 2606 Rubik’s cube
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2606 Rubik’s cube Time Li ...
随机推荐
- 【BZOJ2821】作诗(Poetize) 分块
Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次, ...
- 【JAVA】JMX简单使用方法
[BEAN] 配置 <!-- JMX 对应的接口服务--> <bean id="emailInterfaceServer" class="com.s ...
- SQLSERVER的NULL
判断数据库中某个值是否为null(而不是'null',空字符串'',若干个空格' ') 一定不能用=null 或 !=null,而要用is null 或 is not null. 在sqlserver ...
- Linux安装卸载查看vsftpd
Linux & vsftpd 相关的命令: 查看---rpm -qa | grep vsftpd 卸载---rpm -e vsftpd 安装---rpm -ivh /media/(在此tab ...
- Ubuntu root密码修改
安装完Ubuntu后忽然意识到没有设置root密码,不知道密码自然就无法进入根用户下.到网上搜了一下,原来是这麽回事.Ubuntu的默认root密码是随机的,即每次开机都有一个新的root密码.我们可 ...
- Linux下memcached安装和启动方法
Linux下memcached安装和启动方法 1. 首先下载memcached 和 libevent 包. Memcached用到了libevent这个库用于Socket的处理.下面是下载的两个包文件 ...
- 用atom写LaTeX文档
下载并安装Tex Live: 下载页面 下载并安装atom:下载页面 打开atom File -> Settings -> Install 搜索并安装: language-latex la ...
- linux shell 获取进程pid
1.通过可执行程序的程序名称 a.运行程序 b.获取进程id号 c.pidof相关知识:http://www.cnblogs.com/yunsicai/p/3675938.html 2.有些程序需要在 ...
- EasyUI组件(窗口组件)
注意首先要在title后面导入配置文件,前后顺序不能乱 <!-- 1.jQuery的js包 --><script type="text/javascript" s ...
- EF框架中加子类后出现列名 'Discriminator' 无效问题