【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 ...
随机推荐
- windows下安装python、环境设置、多python版本的切换、pyserial与多版本python安装、windows命令行下切换目录
1.windows下安装python 官网下载安装即可 2.安装后的环境设置 我的电脑--属性--高级--设置path的地方添加python安装目录,如C:\Python27;C:\Python33 ...
- 无法启动INTERNET连接共享。一个已经用IP地址配置的LAN连接需要自动IP地址
提问者采纳 很简单 只要把你要共享的连接 192.168.0.1 0改成1 然后就可以共享了 共享后再改回来 就能上了 但是会出现IP冲突的~
- 某考试 T1 sigfib
设 g(x) = f(x) * x ,多项式 A = Σ g(i) * x^i , 多项式 B = Σ f(i) * x^i. 首先,g(x) = g(x-1) + g(x-2) + f(x-1) ...
- Ubuntu 16.04安装IntelliJ IDEA时快捷键冲突设置
解决快捷键冲突可以有如下方法: 1.直接修改IDEA的,但是不建议这么干,因为多平台时,或者去到另外一台电脑时,统一的快捷键能更快的适应新的开发环境. 2.通过修改系统默认的快捷键. 3.就这两种方式 ...
- 【面试 spring】【第七篇】spring的问题
1.spring你熟悉么?两大特色 spring 主要有IOC和AOP两大特色. =========================================================== ...
- Angularjs: call other scope which in iframe
Angularjs: call other scope which in iframe -------------------------------------------------------- ...
- 转:某运维DBA的mysql学习心得
转自:http://www.cnblogs.com/lyhabc/p/3691555.html 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心 ...
- HDU 5335 Walk Out(多校)
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- atitit.窗口静听esc退出本窗口java swing c# .net php
atitit.窗口静听esc退出本窗口java swing c# .net php 1. 监听esc 按键 1 1.1. 监听一个组件 1 1.2. 监听加在form上 1 2. 关闭窗口 2 1. ...
- java设计模式----其他模式
1.桥接:使用桥接模式不只改变你的实现,也改变你的抽象 优点: 将实现予以解耦,让它和界面之间不再永久绑定 抽象和实现可以独立扩展,不会影响到对方 对于“具体的抽象类”所做的改变,不会影响到客户 用途 ...