UVA_Cubic Eight-Puzzle UVA 1604
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的更多相关文章
- UVA_Digit Puzzle UVA 12107
If you hide some digits in an integer equation, you create a digit puzzle. The figure below shows tw ...
- uva 227 Puzzle (UVA - 227)
感慨 这个题实在是一个大水题(虽然说是世界决赛真题),但是它给出的输入输出数据,标示着老子世界决赛真题虽然题目很水但是数据就能卡死你...一直pe pe直到今天上午AC...无比感慨...就是因为最后 ...
- Puzzle UVA - 227 PE代码求大佬指点
A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smal ...
- UVA 1604:Cubic Eight-Puzzle(模拟,BFS Grade C)
题意: 3*3方格,有一个是空的.其他的每个格子里有一个立方体.立方体最初上下白色,前后红色,左右蓝色.移动的方式为滚.给出初态空的位置,终态上面颜色情况,问最少多少步能到达.如果超过30步不能到达, ...
- uva 227 Puzzle
Puzzle A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...
- UVA 227 Puzzle - 输入输出
题目: acm.hust.edu.cn/vjudge/roblem/viewProblem.action?id=19191 这道题本身难度不大,但输入输出时需要特别小心,一不留神就会出问题. 对于输入 ...
- UVA 277 Puzzle
题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串. 注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句. ②除了最后一次的输出外,其他输出均要在后面空一行. ③操作 ...
- UVA 227 Puzzle(基础字符串处理)
题目链接: https://cn.vjudge.net/problem/UVA-227 /* 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串 的指令,如果指令合法,能够得 ...
- uva live 12846 A Daisy Puzzle Game
假设下一个状态有必败.那么此时状态一定是必胜,否则此时状态一定是必败 状压DP #include<iostream> #include<map> #include<str ...
随机推荐
- Copy Constructor in Java
Reference: TutorialPoints, GeekforGeeks The copy constructor is a constructor which creates an objec ...
- Linux2.6内核 -- 编码风格(3)
9.typedef 内核开发者们强烈反对使用 typedef 语句.他们的理由是: 1> typedef 掩盖了数据的真实类型 2> 由于数据类型隐藏起 ...
- paip.提升性能--- mysql 建立索引 删除索引 很慢的解决.
paip.提升性能--- mysql 建立索引 删除索引 很慢的解决. 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blo ...
- wxpython 布局管理
一个典型的应用程序是由不同的部件.这些小部件被放进容器部件.一个程序员必须管理应用程序的布局.这不是一项容易的任务.在wxPython我们有两个选择. *absolute positioning*si ...
- 使用STS 创建spring配置文件
1.创建一个bean文件 2.输入文件名applicationContext.xml 3.这里会自动显示模板文件 4.创建后,自动填充头不定义 到这里就可以发现,我们创建spring文件时,需要的配置 ...
- Redis + Jedis + Spring 实例(对象的操作)
目录(?)[+] 不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 一.预期 接上 ...
- [转]Laravel 4之URL生成
Laravel 4之URL生成 http://dingjiannan.com/2013/laravel-url/ 获取当前URL 获取当前URL有两种方式,URL::current()或URL::fu ...
- Android窗口管理服务WindowManagerService计算窗口Z轴位置的过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8570428 通过前面几篇文章的学习,我们知道了 ...
- MP3文件结构及解码概述
MP3文件结构概述 Layer-3音频文件.MPEG(MovingPicture Experts Group)在汉语中译为活动图像专家组,特指活动影音压缩标准,MPEG音频文件是MPEG1标准中的声音 ...
- Android 打造自己的个性化应用(一):应用程序换肤主流方式的分析与概述
Android平台api没有特意为换肤提供一套简便的机制,这可能是外国的软件更注重功能和易用,不流行换肤.系统不提供直接支持,只能自行研究. 换肤,可以认为是动态替换资源(文字.颜色.字体大小.图片. ...