【题目链接】

点击打开链接

【算法】

这是经典的八数码问题,据说此题不做人生不完整

这里笔者用的是双向广搜,由于细节较多,笔者花了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的更多相关文章

  1. 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 ...

  2. 【POJ 3080 Blue Jeans】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...

  3. 【原创分享·微信支付】 C# MVC 微信支付教程系列之公众号支付

    微信支付教程系列之公众号支付         今天,我们接着讲微信支付的系列教程,前面,我们讲了这个微信红包和扫码支付.现在,我们讲讲这个公众号支付.公众号支付的应用环境常见的用户通过公众号,然后再通 ...

  4. 【一步一图】:详解IIS日志配置

    打开网站配置:右键点击属性 弹出设置界面     如上图,日志可选4种格式: [Microsoft IIS 日志文件格式]   存放地址如上图 以in开头 年份后两位+月份+日 命名: 示例: //, ...

  5. 【Linux探索之旅】第二部分第二课:命令行,世界尽在掌握

    内容简介 1.第二部分第二课:命令行,世界尽在掌握 2.第二部分第三课预告:文件和目录,组织不会亏待你 命令行,世界尽在掌握 今天的标题是不是有点霸气侧漏呢? 读者:“小编,你为什么每次都要起这么非主 ...

  6. 【C++探索之旅】开宗明义+第一部分第一课:什么是C++?

    内容简介 1.课程大纲 2.第一部分第一课:什么是C++? 3.第一部分第二课预告:C++编程的必要软件 开宗明义 亲爱的读者,您是否对C++感兴趣,但是C++看起来很难,或者别人对你说C++挺难的, ...

  7. Python学习【第十二篇】模块(2)

    序列化 1.什么是python序列化? 把变量从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling 序列化就是将python的数据类型转换成字符串 反序列化就是将字符串转换成 ...

  8. 【CAS单点登录视频教程】 第01集-- 认识CAS

    CAS 是什么? 目录 ----------------------------------------- [CAS单点登录视频教程] 第06集[完] -- Cas认证 学习 票据认证FormsAut ...

  9. 【神经网络与深度学习】卷积神经网络-进化史:从LeNet到AlexNet

    [卷积神经网络-进化史]从LeNet到AlexNet 本博客是[卷积神经网络-进化史]的第一部分<从LeNet到AlexNet> 如需转载,请附上本文链接:http://blog.csdn ...

随机推荐

  1. 社区发现(Community Detection)算法

    作者: peghoty 出处: http://blog.csdn.net/peghoty/article/details/9286905 社区发现(Community Detection)算法用来发现 ...

  2. VM虚拟机

    VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式. 1 ...

  3. 用canal监控binlog并实现mysql定制同步数据的功能

    业务背景 写任何工具都不能脱离实际业务的背景.开始这个项目的时候是因为现有的项目中数据分布太零碎,零零散散的分布在好几个数据库中,没有统一的数据库来收集这些数据.这种情况下想做一个大而全的会员中心系统 ...

  4. 批量修改WORD表格属性

    有时候需要对word中很多表格的属性进行修改,而word无法批量修改属性,所有这里记录一个宏 Sub TableFormatter() Dim oTbl As Table, i As Integer ...

  5. 【Todo】已经打开的页面需要清掉的坑

    下面是当前我浏览器里面打开的技术文章.需要清掉.一个坑一个坑地填吧. 微信文件传输里面也有几篇12.6号的<Akuna Capital电面面经><2016最流行的Java EE服务器 ...

  6. BUPT复试专题—科学计算器(2009)

    题目描述 给你一个不带括号的表达式,这个表达式只包含加.减.乘.除,请求出这个表 达式的最后结果,最后结果一定是整数: 输入 一个数学表达式,只包括数字,数字保证是非负整数,以及五种运算符 " ...

  7. HDU 2009 整除的尾数 题解

    Problem Description 一个整数,仅仅知道前几位,不知道末二位.被还有一个整数除尽了.那么该数的末二位该是什么呢?   Input 输入数据有若干组,每组数据包括二个整数a,b(0&l ...

  8. Odoo HR Payslip

    pay slip 可以录入多条 worked_days_line 和 input_line,用来人工调整薪资变动部分,比如销售提成,扣款等. pay slip 可以包含多个pay slip line ...

  9. SQL模糊查询碰到空值怎么办?

    作者:iamlaosong SQL查询语句用%来做模糊查询.程序中一般要求用户输入部分信息,依据这个信息进行模糊查询. 比如用户输入340104,以下这条语句就是查询昨天客户代码为340104开头的全 ...

  10. 【转载】“菜”鸟理解.NET Framework(CLI,CLS,CTS,CLR,FCL,BCL)

    既然要学.NET,就要先认识认识她,我不喜欢大段大段文字的东西,自己通过理解,画个图,来看看.NET的沉鱼落雁,闭月羞花之容. 最下层蓝色部分是.NET Framework的基础,也是所有应用软件的基 ...