Let's play a puzzle using eight cubes placed on a 3 x 3 board leaving one empty square.
Faces of cubes are painted with three colors. As a puzzle step, you can roll one of the cubes to the adjacent
empty square. Your goal is to make the specified color pattern visible from above by a number of such steps.
The rules of this puzzle are as follows.
Coloring of Cubes: All the cubes are colored in the same way as shown in Figure 3. The opposite
faces have the same color.
Figure 3: Coloring of a cube


1.Initial Board State: Eight cubes are placed on the 3 x 3 board leaving one empty square. All the
cubes have the same orientation as shown in Figure 4. As shown in the figure, squares on the board
are given x and y coordinates, (1, 1), (1, 2), ..., and (3, 3). The position of the initially empty square
may vary.

Figure 4: Initial board state
2.Rolling Cubes: At each step, we can choose one of the cubes adjacent to the empty square and roll it
into the empty square, leaving the original position empty. Figure 5 shows an example.
Figure 5: Rolling a cube

3.Goal: The goal of this puzzle is to arrange the cubes so that their top faces form the specified color
pattern by a number of cube rolling steps described above.
4.Your task is to write a program that finds the minimum number of steps required to make the specified color
pattern from the given initial state.
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated
by a space. The number of datasets is less than 16. Each dataset is formatted as follows.
x y
F
11 F21 F31
F
12 F22 F32
F
13 F23 F33
The first line contains two integers x and y separated by a space, indicating the position (x, y) of the initially
empty square. The values of x and y are 1, 2, or 3.
The following three lines specify the color pattern to make. Each line contains three characters F1j, F2j, and
F
3j, separated by a space. Character Fij indicates the top color of the cube, if any, at position (i, j) as follows:
B:
Blue,
W:
White,
R:
Red,
E:
the square is Empty.
There is exactly one `E' character in each dataset.
Output
For each dataset, output the minimum number of steps to achieve the goal, when the goal can be reached
within 30 steps. Otherwise, output ``-1'' for the dataset.
3618 - Cubic Eight-Puzzle 2/3
Sample Input
1 2
W W W
E W W
W W W
2 1
R B W
R W W
E W W
3 3
W B W
B R E
R B R
3 3
B W R
B W R
B E R
2 1
B B B
B R B
B R E
1 1
R R R
W W W
R R E
2 1
R R R
B W B
R R E
3 2
R R R
W E W
R R R
0 0
Sample Output
0
3
13
23
29
30
-1
-1

解题报告

难度。。很大,BFS这个没有疑问,那么究竟是A*还是双广呢,由于本题的状态数很多,且转移方程很麻烦,所以首先考虑A*.

估价函数这里不在多说(其实是我忘了),但提交后TLE...,CODE如下

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
using namespace std;
const int MaxHashSize = ;
const int MaxState = ;
int tot,target[];
char g[];
int dir[][] = {,,,-,-,,,};
int getdia[][] = {,,,,,,,,,,,,,,,,,,,,,,,}; typedef struct status
{
char pos;
char step;
int g;
char h;
friend bool operator < (const status& a ,const status& b)
{
if (a.step + a.h < b.step + b.h)
return false;
if (a.step + a.h == b.step + b.h && a.step < b.step)
return false;
return true;
} }; priority_queue<status> q; status start;
int Head[MaxHashSize],Next[MaxState];
status st[MaxState];
void init_Hash()
{
memset(Head,-,sizeof(Head));
} int GetHashValue(status &x)
{
return x.g % MaxHashSize;
} bool insert_Hash(int id)
{
int h = GetHashValue(st[id]);
int u = Head[h];
while(u != -)
{
if (st[u].g == st[id].g)
return false;
u = Next[u];
}
Next[id] = Head[h];
Head[h] = id;
return true;
} int caculateh(status &x)
{
int res = ;
int ok = ;
for(int i = ; i < ; ++ i)
{
int temp = ;
for(int j = ; j < ; ++ j)
{
int s = (x.g >> (*i + j)) & ;
temp |= (s << j);
}
if ( (temp == || temp == ) && g[i] != 'W')
res ++ ;
else if ( (temp == || temp == ) && g[i] !='R')
res ++;
else if ( (temp == || temp == ) && g[i] != 'B')
res ++;
else if (temp == && g[i] != 'E' )
{
res ++;
ok = ;
} }
if (ok)
return res;
else
return res - ; } int bfs()
{
int front = ,rear = ;
q.push(start);
while(!q.empty())
{
status ss = q.top();q.pop();
st[front++] = ss;
if (ss.h == )
return ss.step;
if (ss.h + ss.step > )
return -;
int pos = ss.pos;
int x = pos / ;
int y = pos % ;
int step = ss.step;
int g = ss.g;
for(int i = ; i < ; ++ i)
{
int newx = x + dir[i][];
int newy = y + dir[i][];
if (newx >= || newx < || newy >= || newy < )
continue;
int newpos = newx * + newy;
status ns = ss;
int temp = ;
for(int j = ; j < ; ++ j)
{
int s = (g >> (newpos * + j) ) & ;
temp |= (s << j);
}
int sis = getdia[temp][i];
for(int j = ; j < ;++ j)
ns.g |= ( << (newpos* + j));
for(int j = ; j < ; ++ j)
if (sis >> j & )
ns.g |= ( << pos * + j);
else
ns.g &= ~( << pos * + j);
ns.step = step + ;
ns.pos = newpos;
ns.h = caculateh(ns);
st[rear] = ns;
if (insert_Hash(rear))
{
q.push(ns);
rear++;
}
}
}
return -;
} int main(int argc, char * argv[])
{
int kk = clock();
int s1,s2;
while(scanf("%d%d%*c",&s1,&s2) && s1)
{
tot = ;
for(int i = ; i < ; ++ i)
scanf("%c%*c",&g[i]);
int fp = (s2-) * + s1 - ;
start.pos = fp;
start.g = ;
init_Hash();
for(int i = ; i < ; ++ i)
start.g |= ( << (*fp+i));
st[] = start;
insert_Hash();
start.h = caculateh(start);
start.step = ;
while(!q.empty())
q.pop();
cout << bfs() << endl;
}
cout << "Time use " << clock() - kk << endl;
return ;
}

这里的代码中有Clock(),大家可以试试几组数据,30步的极限或者-1,速度非常慢。。(无法忍受)

那么,我们只能改用双广了,因为末状态有256种,所以我们用一个dfs把末状态压进去,提交后AC~

顺便多题一句双广复杂度是 2*n^(a/2) ,而单向的是n^a,code 如下

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int MaxHashSize = ;
const int MaxState = ;
int tot,target[],ssl;
char g[];
int dir[][] = {,,,-,-,,,};
int getdia[][] = {,,,,,,,,,,,,,,,,,,,,,,,}; typedef struct status
{
char pos;
char step;
int g;
}; status start;
int Head[MaxHashSize],Next[MaxState];
int Head2[MaxHashSize],Next2[MaxState];
status st[MaxState];
status st2[MaxState]; void init_Hash()
{
memset(Head,-,sizeof(Head));
memset(Head2,-,sizeof(Head));
} int GetHashValue(status &x)
{
return x.g % MaxHashSize;
} bool insert_Hash(int id ,int l)
{
int h;
if (l == )
h = GetHashValue(st[id]);
else
h = GetHashValue(st2[id]);
int u;
if (l == )
u = Head[h];
else
u = Head2[h]; while(u != -)
{
if (l == )
{
if (st[u].g == st[id].g)
return false;
u = Next[u];
}
else
{
if (st2[u].g == st2[id].g)
return false;
u = Next2[u];
}
}
if (l == )
{
Next[id] = Head[h];
Head[h] = id;
}
else
{
Next2[id] = Head2[h];
Head2[h] = id;
}
return true;
} int meet(int id,int l)
{
int u,h;
if (l == )
{
h = GetHashValue(st[id]);
int u = Head2[h];
while(u != -)
{
if (st2[u].g == st[id].g)
return st[id].step + st2[u].step;
u = Next2[u];
} return -;
}
else
{
h = GetHashValue(st2[id]);
int u = Head[h];
while(u != -)
{
if (st[u].g == st2[id].g)
return st[u].step + st2[id].step;
u = Next[u];
} return -;
}
} int bfs()
{
int front = ,rear = ;
int front2 = , rear2 = ;
while(front < rear && front2 < rear2)
{
if ()
{
status ss = st[front++] ;
int ans = meet(front-,);
if (ans != -)
return ans > ? - : ans;
if (ss.step >= )
return -;
int pos = ss.pos;
int x = pos / ;
int y = pos % ;
int step = ss.step;
int g = ss.g;
for(int i = ; i < ; ++ i)
{
int newx = x + dir[i][];
int newy = y + dir[i][];
if (newx >= || newx < || newy >= || newy < )
continue;
int newpos = newx * + newy;
status ns = ss;
int temp = ;
for(int j = ; j < ; ++ j)
{
int s = (g >> (newpos * + j) ) & ;
temp |= (s << j);
}
int sis = getdia[temp][i];
for(int j = ; j < ;++ j)
ns.g |= ( << (newpos* + j));
for(int j = ; j < ; ++ j)
if (sis >> j & )
ns.g |= ( << pos * + j);
else
ns.g &= ~( << pos * + j);
ns.step = step + ;
ns.pos = newpos;
st[rear] = ns;
if (insert_Hash(rear,))
{
rear++;
}
}
}
status ss = st2[front2++] ;
int ans = meet(front2-,);
if (ans != -)
return ans > ? - : ans;
if (ss.step >= )
return -;
int pos = ss.pos;
int x = pos / ;
int y = pos % ;
int step = ss.step;
int g = ss.g;
for(int i = ; i < ; ++ i)
{
int newx = x + dir[i][];
int newy = y + dir[i][];
if (newx >= || newx < || newy >= || newy < )
continue;
int newpos = newx * + newy;
status ns = ss;
int temp = ;
for(int j = ; j < ; ++ j)
{
int s = (g >> (newpos * + j) ) & ;
temp |= (s << j);
}
int sis = getdia[temp][i];
for(int j = ; j < ;++ j)
ns.g |= ( << (newpos* + j));
for(int j = ; j < ; ++ j)
if (sis >> j & )
ns.g |= ( << pos * + j);
else
ns.g &= ~( << pos * + j);
ns.step = step + ;
ns.pos = newpos;
st2[rear2] = ns;
if (insert_Hash(rear2,))
{
rear2++;
}
}
}
return -;
} void dfs(int n,int temp)
{
if (n == )
{
st2[tot].g = temp;
st2[tot].step = ;
insert_Hash(tot,);
st2[tot++].pos = ssl;
}
else
{
int f;
if (g[n] == 'W')
{
f = temp;
dfs(n+,f);
f |= ( << (*n));
dfs(n+,f);
}
else if(g[n] == 'R')
{
f = temp;
f |= ( << (*n+));
dfs(n+,f);
f |= ( << *n);
dfs(n+,f);
}
else if(g[n] == 'B')
{
f = temp;
f |= ( << *n+);
dfs(n+,f);
f |= ( << *n);
dfs(n+,f);
}
else if(g[n] == 'E')
{
f = temp;
for(int j = ; j < ; ++ j)
f |= ( << *n + j);
dfs(n+,f);
}
}
} int main(int argc, char * argv[])
{
int s1,s2;
while(scanf("%d%d%*c",&s1,&s2) && s1)
{
tot = ;
for(int i = ; i < ; ++ i)
{
scanf("%c%*c",&g[i]);
if (g[i] == 'E')
ssl = i;
}
int fp = (s2-) * + s1 - ;
start.pos = fp;
start.g = ;
init_Hash();
dfs(,);
for(int i = ; i < ; ++ i)
start.g |= ( << (*fp+i));
st[] = start;
insert_Hash(,);
start.step = ;
cout << bfs() << endl;
}
return ;
}

UVA_Cubic Eight-Puzzle UVA 1604的更多相关文章

  1. UVA_Digit Puzzle UVA 12107

    If you hide some digits in an integer equation, you create a digit puzzle. The figure below shows tw ...

  2. uva 227 Puzzle (UVA - 227)

    感慨 这个题实在是一个大水题(虽然说是世界决赛真题),但是它给出的输入输出数据,标示着老子世界决赛真题虽然题目很水但是数据就能卡死你...一直pe pe直到今天上午AC...无比感慨...就是因为最后 ...

  3. Puzzle UVA - 227 PE代码求大佬指点

    ​ A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smal ...

  4. UVA 1604:Cubic Eight-Puzzle(模拟,BFS Grade C)

    题意: 3*3方格,有一个是空的.其他的每个格子里有一个立方体.立方体最初上下白色,前后红色,左右蓝色.移动的方式为滚.给出初态空的位置,终态上面颜色情况,问最少多少步能到达.如果超过30步不能到达, ...

  5. uva 227 Puzzle

     Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...

  6. UVA 227 Puzzle - 输入输出

    题目: acm.hust.edu.cn/vjudge/roblem/viewProblem.action?id=19191 这道题本身难度不大,但输入输出时需要特别小心,一不留神就会出问题. 对于输入 ...

  7. UVA 277 Puzzle

    题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串. 注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句. ②除了最后一次的输出外,其他输出均要在后面空一行. ③操作 ...

  8. UVA 227 Puzzle(基础字符串处理)

    题目链接: https://cn.vjudge.net/problem/UVA-227 /* 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串 的指令,如果指令合法,能够得 ...

  9. uva live 12846 A Daisy Puzzle Game

    假设下一个状态有必败.那么此时状态一定是必胜,否则此时状态一定是必败 状压DP #include<iostream> #include<map> #include<str ...

随机推荐

  1. 45 个非常有用的 Oracle 查询语句(转)

    这里我们介绍的是 40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有 Oracle 开发者都必备的技能,所以快快收 ...

  2. Android FragmentActivity onActivityResult冲突问题

    场景:假设fragment的填充数据是ListView,ListView中的每一项都有button,点击每一项的button跳转到B界面.在B界面处理完业务需返回fragment中更新数据. 解决方式 ...

  3. 解决一个maven在eclipse中M2_HOME不能调整的问题

    在坚持了许久之后,依然还是没能抵住maven的各种攻击,终于从0开始maven. 开始由于有关小项目,时间紧任务重,没仔细研究maven,便匆匆上手了,导致maven库的位置放在了c盘当前用户的数据目 ...

  4. pthread_t结构的定义

    linux下是这样定义的: 在linux的实现中pthread_t被定义为 "unsigned long int",參考这里 Windows下这样定义: /* * Generic ...

  5. 在Docker中运行torch版的neural style

    相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 在Docker中运行to ...

  6. JSTL学习笔记(核心标签)

    一.JSTL标签分类: 核心标签 格式化标签 SQL标签 XML标签 JSTL函数 二.核心标签       引用方式:<%@ taglib prefix="c" uri=& ...

  7. [Spring入门学习笔记][Spring的AOP原理]

    AOP是什么? 面向切面编程 软件工程有一个基本原则叫做“关注点分离”(Concern Separation),通俗的理解就是不同的问题交给不同的部分去解决,每部分专注于解决自己的问题.这年头互联网也 ...

  8. 基于ADODBX对数据库的CURD

    学asp.net也有一个多星期了,之前对这个一无所知,也不知道怎么去找一些相关的资料去学习,不懂了就问问别人这个怎么做,那个怎么写,要不是有jsp和php的基础,估计还得弄上好长的时间来学习.记录一下 ...

  9. 前端--关于javascript对象

    在javascript中对象是一种基本的数据类型,在数据结构上是一种散列表,可以看作是属性的无序集合,除了原始值其他一切都是对象.它可以用来表示现实世界中或者我们大脑中抽象出来的客体,这和其他面向对象 ...

  10. 学习AJAX(一)