poj 1077(BFS预处理+康托展开)
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 29935 | Accepted: 13029 | Special Judge | ||
Description
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 x
where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->
The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.
Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).
In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.
Input
1 2 3
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
Output
Sample Input
2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr 题意:经典八数码
题解:预处理终点到所有状态的路径。康拓展开保存状态
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N = ;
int fab[] = {,,,,,,,,};
bool vis[N];
struct Node{
int a[];
int Hash;
int _x; ///x所在位置
};
struct Way{
char c;
int pre;
}way[N];
int contor(Node s){
int sum = ;
for(int i=;i>=;i--){
int cnt = ;
for(int j=i-;j>=;j--){
if(s.a[i]<s.a[j]) cnt++;
}
sum+=fab[i-]*cnt;
}
return sum;
}
int dir[][] = {{-,},{,},{,-},{,}}; ///上下左右
bool change(Node &s,int _x,int k){
int x = (_x-)/+;
int y = _x%==?:_x%;
int nextx = x+dir[k][];
int nexty = y+dir[k][];
if(nextx<||nexty>||nexty<||nexty>) return false;
swap(s.a[_x],s.a[(nextx-)*+nexty]);
s._x = (nextx-)*+nexty;
return true;
}
void bfs(){
for(int i=;i<N;i++){
way[i].pre = -;
}
memset(vis,false,sizeof(vis));
Node s;
for(int i=;i<=;i++){
s.a[i] = i;
}
s.Hash = ,s._x = ;
vis[] = ;
queue<Node> q;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
Node next;
next = now;
if(change(next,next._x,)){
int k = contor(next);
if(!vis[k]){
vis[k] = true;
next.Hash = k;
way[k].pre = now.Hash;
way[k].c = 'd';
q.push(next);
}
}
next = now;
if(change(next,next._x,)){
int k = contor(next);
if(!vis[k]){
vis[k] = true;
next.Hash = k;
way[k].pre = now.Hash;
way[k].c = 'u';
q.push(next);
}
}
next = now;
if(change(next,next._x,)){
int k = contor(next);
if(!vis[k]){
vis[k] = true;
next.Hash = k;
way[k].pre = now.Hash;
way[k].c = 'r';
q.push(next);
}
}
next = now;
if(change(next,next._x,)){
int k = contor(next);
if(!vis[k]){
vis[k] = true;
next.Hash = k;
way[k].pre = now.Hash;
way[k].c = 'l';
q.push(next);
}
}
}
}
char str[];
char ans[];
int t = ;
void dfs(int k){
if(way[k].pre==-) return;
dfs(way[k].pre);
ans[t++]=way[k].c;
}
int main()
{
bfs();
while(scanf("%s",str)!=EOF){
Node s;
s.a[] = (str[]=='x')?:str[]-'';
for(int i=;i<=;i++){
scanf("%s",str);
s.a[i] = (str[]=='x')?:str[]-'';
}
int k = contor(s);
ans;
t = ;
dfs(k);
if(t==){
printf("unsolvable\n");
continue;
}
for(int i=t-;i>=;i--){
printf("%c",ans[i]);
}
printf("\n");
}
return ;
}
poj 1077(BFS预处理+康托展开)的更多相关文章
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1043 Eight 【经典八数码输出路径/BFS/A*/康托展开】
本题有写法好几个写法,但主要思路是BFS: No.1 采用双向宽搜,分别从起始态和结束态进行宽搜,暴力判重.如果只进行单向会超时. No.2 采用hash进行判重,宽搜采用单向就可以AC. No.3 ...
- HDU_1430——魔板,预处理,康托展开,置换,string类的+操作
Problem Description 在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板.魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示.任一时刻魔板的状态可 ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)
思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...
- HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二
类似HDU1430,不过本题需要枚举X的九个位置,分别保存状态,因为要保证最少步数.要保证字典序最小的话,在扩展节点时,方向顺序为:down, left, right, up. 我用c++提交1500 ...
- POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...
- POJ 1077 Eight (BFS+康托展开)详解
本题知识点和基本代码来自<算法竞赛 入门到进阶>(作者:罗勇军 郭卫斌) 如有问题欢迎巨巨们提出 题意:八数码问题是在一个3*3的棋盘上放置编号为1~8的方块,其中有一块为控制,与空格相邻 ...
随机推荐
- 洛谷 P4503 [CTSC2014]企鹅QQ 解题报告
P4503 [CTSC2014]企鹅QQ 题目背景 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即 ...
- 安装redis环境
1 安装redis至 /home/www/redis目录下 [root@cuijian www]# tar zxf redis-2.8.13.tar.gz [root@cuijian www]# cd ...
- 【翻译】InterlockedIncrement内部是如何实现的?
Interlocked系列函数可以对内存进行原子操作,它是如何实现的? 它的实现依赖于底层的CPU架构.对于某些CPU来说,这很简单,例如x86可以通过LOCK前缀直接支持Interl ...
- eclipse+myeclipse 使用技巧备忘
myeclipse 导入多模块maven项目 https://blog.csdn.net/jack85986370/article/details/51371853 maven项目在eclipse的l ...
- GYM 101128 G.Game of Cards(博弈论) 或者 UVALIVE 7278
题目链接:http://codeforces.com/gym/101128/my 如果可以,就看这个人的代码吧,我还不是很懂唉:http://blog.csdn.net/loy_184548/arti ...
- CF767 C.Garland DFS
LINK 题意:给定一棵树,每个节点拥有权值,问能否找到两个点,断开它们与父节点的边能使树分成权值和相等的三部分.权值可以为负 思路:进行两遍DFS,第一遍找最深的子树和为sum/3的节点,标记掉找到 ...
- [洛谷P2745] [USACO5.3]窗体面积Window Area
洛谷题目链接:[USACO5.3]窗体面积Window Area 题目描述 你刚刚接手一项窗体界面工程.窗体界面还算简单,而且幸运的是,你不必显示实际的窗体.有 5 种基本操作: 创建一个新窗体 将窗 ...
- .net core 中 identity server 4 之Topic --定义API资源
想要让客户端能够访问API资源,就需要在Identity Server中定义好API的资源. Scope作用域:即API资源的访问范围限制. 作用域是一个资源 (通常也称为 Web API) 的标识符 ...
- 理解 CSS 中的伪元素 :before 和 :after
CSS 的主要目的是给 HTML 元素添加样式,然而,在一些案例中给文档添加额外的元素是多余的或是不可能的.事实上 CSS 中有一个特性允许我们添加额外元素而不扰乱文档本身,这就是“伪元素”. 你一定 ...
- bzoj 2213: [Poi2011]Difference
Description A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We w ...