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的方块,其中有一块为控制,与空格相邻 ...
随机推荐
- splay tree 学习笔记
首先感谢litble的精彩讲解,原文博客: litble的小天地 在学完二叉平衡树后,发现这是只是一个不稳定的垃圾玩意,真正实用的应有Treap.AVL.Splay这样的查找树.于是最近刚学了学了点S ...
- flex的使用实例
之前的随笔从阮一峰老师那里学到了flex的基本用法及作用,现在来把flex具体运用到实例中,看看flex的弹性布局效果. 1. flex设置元素垂直居中对齐 在之前的一篇文章中记载过如何垂直居中对齐 ...
- 【51nod1965】奇怪的式子
Portal --> 51nod1965 Solution 怎么说呢..这题..做的有点痛苦.. 首先看这个式子长得..比较奇怪,指数里面那个加号有点烦人,而且这个函数不是一个积性函数也有点烦人 ...
- 专题训练之区间DP
例题:以下例题部分的内容来自https://blog.csdn.net/my_sunshine26/article/details/77141398 一.石子合并问题 1.(NYOJ737)http: ...
- 简述this,call,apply,bind之间的关系
一.什么是this? this是JavaScript语言的一个关键字,它是函数运行时在函数体内部自动生成的一个对象,只能在函数体内部使用.函数的不同使用场合,this的指向不同. 在ES5中,this ...
- form, table表示表格的时候有什么区别?
http://zhidao.baidu.com/link?url=1DFrMJlzV_fHSyGmKEi77ki6g2IrjrMfRGwVYNHL5Y8iJC9Diu2BoMGEiB3wbnkTCHm ...
- HDU 4584 splay
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- codeforces 691E 矩阵快速幂+dp
传送门:https://codeforces.com/contest/691/problem/E 题意:给定长度为n的序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二 ...
- bootstrap 栅格calss
container container-fluid row col-xs- col-sm- col-md- col-lg- col-md-offset- col-md-push- col-md-pul ...
- linux shell学习四
Shell分支语句 case 值 in 模式1) command1 command2 command3 ;; 模式2) command1 command2 command3 ;; *) command ...