【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 ...
随机推荐
- Maven生成项目文档
Maven项目可以通过maven-site-plugin插件生成项目文档,无论什么项目都可以生成. 执行命令: mvn site 生成完成的输出目录在${basedir}/target/site文件夹 ...
- [置顶] 内存管理一点也不神秘————手绘iOS内存管理细节
今天给大家带来的一篇手绘风格博文<内存管理一点也不神秘> 每当我们程序执行alloc/new/copy/mutableCopy的时候,当我们执行release的时候,当我们执行retain ...
- Swift下调用Touch ID实现指纹识别
iOS开发交流群:484884085 欢迎大家增加. 例如以下代码是实现调用5S以上设备指纹识别的代码,依照官网给出的代码,会出现识别成功后非常久才运行成功后调用的代码,逛了非常久的.才知道要将其放入 ...
- Spring4+SpringMVC+Hibernate4整合入门与实例
配置web.xml <? xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...
- 设计模式入门之原型模式Prototype
//原型模式:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象 //简单来说,当进行面向接口编程时,假设须要复制这一接口对象时.因为不知道他的详细类型并且不能实例化一个接口 //这时就须要 ...
- 【转载】.NET Remoting学习笔记(二)激活方式
目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科 ♂风车车.Net 激活方式概念 在访 ...
- Cocos2d-x 精灵碰撞检測(方法一)
声明函数碰撞检測函数,两个精灵和重写update bool isCollision( CCPoint p1,CCPoint p2,int w1,int h1,int w2,int h2 ); CCSp ...
- 李洪强iOS开发之-实现点击单行View显示和隐藏Cell
李洪强iOS开发之-实现点击单行View显示和隐藏Cell 实现的效果: .... ....
- 在vs2005中添加lib库的方法
方法一:在用到lib的地方加上 //强烈推荐这种方法.#pragma comment(lib,"libname.lib") 方法二: 点击菜单命令 “项目/属性”, ...
- 修复Xcode升级错误 — PCH File Error
http://www.rockia.net/2013/03/fix-xcode-update-pch-file-error Error:PCH File Built From A Different ...