【SOUTH CENTRAL USA 1998】 eight
【题目链接】
【算法】
这是经典的八数码问题,据说此题不做人生不完整
这里笔者用的是双向广搜,由于细节较多,笔者花了3h才通过此题
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h> using namespace std; struct info {
int x,y,h;
int a[];
}; const int dir[][] = {{,},{,-},{,},{-,}};
const int fac[] = {,,,,,,,,,}; int i,d,h1,t1,h2,t2;
int val[],pre1[],pre2[],
step1[],step2[],visit[];
int s[] = {,,,,,,,,,};
vector<int> path1,path2;
info q1[],q2[];
char x; int getcantorHash(int a[]) {
int i,j,s,ret=;
for (i = ; i < ; i++) {
s = ;
for (j = i + ; j <= ; j++) {
if (a[i] > a[j])
s++;
}
ret += s * fac[-i];
}
return ret;
} void getpath1(int Hash) {
int i;
i = t1;
while (i > ) {
path1.push_back(step1[q1[i].h]);
i = pre1[q1[i].h];
}
reverse(path1.begin(),path1.end());
path2.push_back(step2[Hash]);
i = pre2[Hash];
while (i > ) {
path2.push_back(step2[q2[i].h]);
i = pre2[q2[i].h];
}
} void getpath2(int Hash) {
path1.push_back(step1[Hash]);
i = pre1[Hash];
while (i > ) {
path1.push_back(step1[q1[i].h]);
i = pre1[q1[i].h];
}
reverse(path1.begin(),path1.end());
i = t2;
while (i > ) {
path2.push_back(step2[q2[i].h]);
i = pre2[q2[i].h];
}
} bool DBFS() {
int x,y,tx,ty,Hash;
int arr[];
h1 = t1 = h2 = t2 = ;
q1[].x = (d - ) / + ;
q1[].y = (d - ) % + ;
q1[].h = getcantorHash(val);
memcpy((char*)&q1[].a[],(char*)&val[],sizeof(int)*);
step1[getcantorHash(val)] = pre1[getcantorHash(val)] = -;
visit[getcantorHash(val)] = ;
q2[].x = q2[].y = ;
q2[].h = getcantorHash(s);
memcpy((char*)&q2[].a[],(char*)&s[],sizeof(int)*);
step2[getcantorHash(s)] = pre2[getcantorHash(s)] = -;
visit[getcantorHash(s)] = ;
while ((h1 <= t1) || (h2 <= t2)) {
x = q1[h1].x; y = q1[h1].y;
for (i = ; i < ; i++) {
memcpy((char*)&arr[],(char*)&q1[h1].a[],sizeof(int)*);
tx = x + dir[i][];
ty = y + dir[i][];
if ((tx <= ) || (tx > ) || (ty <= ) || (ty > ))
continue;
swap(arr[(x-)*+y],arr[(tx-)*+ty]);
Hash = getcantorHash(arr);
if (visit[Hash] == ) {
visit[Hash] = ;
++t1;
q1[t1].x = tx;
q1[t1].y = ty;
memcpy((char*)&q1[t1].a[],(char*)&arr[],sizeof(int)*);
q1[t1].h = Hash;
step1[Hash] = i;
pre1[Hash] = h1;
}else if (visit[Hash] == ) {
continue;
}else {
++t1;
q1[t1].h = Hash;
step1[Hash] = i;
pre1[Hash] = h1;
getpath1(Hash);
return true;
}
}
x = q2[h2].x; y = q2[h2].y;
for (i = ; i < ; i++) {
memcpy((char*)&arr[],(char*)&q2[h2].a[],sizeof(int)*);
tx = x + dir[i][];
ty = y + dir[i][];
if ((tx <= ) || (tx > ) || (ty <= ) || (ty > ))
continue;
swap(arr[(x-)*+y],arr[(tx-)*+ty]);
Hash = getcantorHash(arr);
if (visit[Hash] == ) {
visit[Hash] = ;
++t2;
q2[t2].x = tx;
q2[t2].y = ty;
q2[t2].h = Hash;
memcpy((char*)&q2[t2].a[],(char*)&arr[],sizeof(int)*);
step2[Hash] = i;
pre2[Hash] = h2;
}else if (visit[Hash] == ) {
continue;
}else {
++t2;
q2[t2].h = Hash;
step2[Hash] = i;
pre2[Hash] = h2;
getpath2(Hash);
return true;
}
}
h1++; h2++;
}
return false;
} void print() {
int i;
for (i = ; i < path1.size(); i++) {
switch(path1[i]) {
case : putchar('r');
break;
case : putchar('l');
break;
case : putchar('d');
break;
default : putchar('u');
}
}
for (i = ; i < path2.size(); i++) {
switch(path2[i]) {
case : putchar('l');
break;
case : putchar('r');
break;
case : putchar('u');
break;
default : putchar('d');
}
}
puts("");
} int main() { for (i = ; i <= ; i++) {
x = getchar();
if (x == 'x') {
d = i;
val[i] = ;
}else
val[i] = x - '';
getchar();
} if (DBFS()) print();
else puts("unsolvable"); return ; }
【SOUTH CENTRAL USA 1998】 eight的更多相关文章
- Eight(South Central USA 1998)(八数码) 分类: bfs 2015-07-05 22:34 1人阅读 评论(0) 收藏
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...
- 【POJ 3080 Blue Jeans】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...
- 【原创分享·微信支付】 C# MVC 微信支付教程系列之公众号支付
微信支付教程系列之公众号支付 今天,我们接着讲微信支付的系列教程,前面,我们讲了这个微信红包和扫码支付.现在,我们讲讲这个公众号支付.公众号支付的应用环境常见的用户通过公众号,然后再通 ...
- 【一步一图】:详解IIS日志配置
打开网站配置:右键点击属性 弹出设置界面 如上图,日志可选4种格式: [Microsoft IIS 日志文件格式] 存放地址如上图 以in开头 年份后两位+月份+日 命名: 示例: //, ...
- 【Linux探索之旅】第二部分第二课:命令行,世界尽在掌握
内容简介 1.第二部分第二课:命令行,世界尽在掌握 2.第二部分第三课预告:文件和目录,组织不会亏待你 命令行,世界尽在掌握 今天的标题是不是有点霸气侧漏呢? 读者:“小编,你为什么每次都要起这么非主 ...
- 【C++探索之旅】开宗明义+第一部分第一课:什么是C++?
内容简介 1.课程大纲 2.第一部分第一课:什么是C++? 3.第一部分第二课预告:C++编程的必要软件 开宗明义 亲爱的读者,您是否对C++感兴趣,但是C++看起来很难,或者别人对你说C++挺难的, ...
- Python学习【第十二篇】模块(2)
序列化 1.什么是python序列化? 把变量从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling 序列化就是将python的数据类型转换成字符串 反序列化就是将字符串转换成 ...
- 【CAS单点登录视频教程】 第01集-- 认识CAS
CAS 是什么? 目录 ----------------------------------------- [CAS单点登录视频教程] 第06集[完] -- Cas认证 学习 票据认证FormsAut ...
- 【神经网络与深度学习】卷积神经网络-进化史:从LeNet到AlexNet
[卷积神经网络-进化史]从LeNet到AlexNet 本博客是[卷积神经网络-进化史]的第一部分<从LeNet到AlexNet> 如需转载,请附上本文链接:http://blog.csdn ...
随机推荐
- LibieOJ 6165 一道水题 (线性筛)
题目链接 LOJ6165 题目意思其实就是求LCM(1, 2, 3, ..., n) 直接用线性筛求出1到1e8之间的所有质数 然后对于每个质数p,他对答案的贡献为$p^{i}$ 其中$p^{i}$小 ...
- linux下eth0 lo wlan0
参考:http://www.cnblogs.com/see7di/archive/2011/06/17/2239722.html 内容如下: 理解linux下的 eth0,eth1,eth2,lo 网 ...
- TOJ 4105
题意:有10万个点,10万个询问,没有更新,求L1<=L<=L2,R1<=R<=R2,有多少个, 其实转换一下:就是求一个矩形 (L1,R1) ----(L2,R2) 中有多少 ...
- @font-face制作小图标的实践
1.为啥要用font-face制作小图标 1)适用性:一个图标字体要比一系列的图像要小,一旦字体图标加载完,图标则会立刻显示出来,不需要去下载一个图像. 2)可扩展性:可以使用font-size对图标 ...
- mysql获取子父级节点
获取所有子节点 DROP FUNCTION IF EXISTS `F_Co29_GetAllChildrenIdsOfTaskevent`;DELIMITER //CREATE FUNCTION `F ...
- DICOM医学图像显示算法改进与实现——LUT
引言 随着Ul(超声成像).CT(计算机断层成像).MRI(核磁共振成像).CR(计算机X线成像).电子内窥镜.盯(正电子发射断层成像)和MI(分子影像)等医学影像设备不断涌现,利用计算机对医学影像设 ...
- hdu2204Eddy's爱好
大概题意是要你输出1到n中,可以表示成a^b的数,a,b都是大于0的整数的个数, 当中b大于1. 由于1到n中.可以全然开平方的个数就是(n^0.5)的整数部分. 以此类推能够得到,全然开立方.全然开 ...
- python 3.4读取输入参数
python 3.4读取输入参数 学习了:https://blog.csdn.net/qq_24815615/article/details/52302615 注意,sys.args[0]是pytho ...
- 【机器学习算法-python实现】协同过滤(cf)的三种方法实现
(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 协同过滤(collaborative filtering)是推荐系统经常使用的一种方法.c ...
- 基于社交网络的情绪化分析IV
基于社交网络的情绪化分析IV By 白熊花田(http://blog.csdn.net/whiterbear) 转载需注明出处,谢谢. 前面进行了微博数据的抓取,简单的处理,类似度分析.后面两篇进行学 ...