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 ...
随机推荐
- 【Eclipse】修改 编码格式
eclipse 默认编码居然是GBK,js文件默认编码是ISO-....怎么可以这样呢? 都修改成UTF8的方法: 1.windows->Preferences...打开"首选项&qu ...
- jQuery 基本过滤选择器注意点
$(".add_shuxing_ul > li:first") 选择class为add_shuxing_ul 的子级li元素的第一个li $(".add_shux ...
- iOS9 升级XCode7遇到的问题收集
开发环境运行 各位可能会觉得,笔者在此还要讲开发环境的运行,是不是多此一举.其实并非如此,综合笔者这几年iOS开发经验的总结,运行新版本,特别是测试版本的Xcode是一个需要格外小心的事情, ...
- Android 图片三级缓存
图片缓存的原理 实现图片缓存也不难,需要有相应的cache策略.这里采用 内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存(SoftReference),其实网络不算cac ...
- [CareerCup] 18.4 Count Number of Two 统计数字2的个数
18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如 ...
- ubuntu上安装Eclipse时遇到的一个错误
A Java Runtime Environment (JRE) or Java Development Kit (JDK)must be available in order to run Ecli ...
- AOP 学习
学习 Spring.Net 的AOP 的时候,在做一个简单的测试例子的时候,配置文件和代码逻辑都是没问题的,但始终报这样一个异常: 无法将类型为“CompositionAopProxy_1e76f37 ...
- Aspose Cells 添加数据验证(动态下拉列表验证)
参考 :http://www.componentcn.com/kongjianjishu/kongjianjishu/2015-06-04/2781.html Aspose Cells是一款操作和处理 ...
- Bootstrap 固定定位(Affix)
来自:慕课网 http://www.imooc.com/code/5396 Affix 效果常见的有以下三种: ☑ 顶部固定 ☑ 侧边栏固定 ☑ 底部固定 固定定位--声明式触发固定定位 Affix ...
- sphinx网址
http://www.phperz.com/article/14/0615/95.htmlhttp://www.kuqin.com/shuoit/20141101/342963.htmlhttp:// ...