【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 ...
随机推荐
- BZOJ——1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
http://www.lydsy.com/JudgeOnline/problem.php?id=1607 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 2 ...
- spark学习(二)
Spark是一个通用的并行计算框架,由UCBerkeley的AMP实验室开发. Spark和Hadoop有什么不同呢? Spark是基于map reduce算法实现的分布式计算,拥有Hadoop Ma ...
- Spring Boot集成Spring Data Reids和Spring Session实现Session共享
首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...
- Angular2.X 笔记
本文为原创,转载请注明出处: cnzt 文章:cnzt-p http://www.cnblogs.com/zt-blog/p/7762590.html 前提: angular-cli (前 ...
- Android Studio 1.3RC版 build加速
Android Studio 确实是好用.但build的速度却是奇慢无比!.! ! 我上网找了非常多build加速的设置,却不能适配到我的1.3RC版... . .心塞.无耐,忍着超级无敌慢的速度硬是 ...
- Solidworks工程图如何使用,替换图纸格式模板文件
1 如果你有了图纸模板(SLDDRW文件),比如下面的这个文件,则以后把零件的三维图拖放到里面就可以了.注意,这是最简单,最有效的方法,后面另存为的slddrt文件不好使,所以还不如把模板文件另存为S ...
- CF 568A(Primes or Palindromes?-暴力推断)
A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input st ...
- 【分享】利用Apache的Htaccess Files命令限制訪问文件类型,Files正则
假设你在你的模板目录中有非常多PSD HTML模板,那么用接下来这个htaccess文件能够保护限制訪问: 文件D:\WebSite\ZBPHP.COM\www\Tpl\.htaccess 所有源代码 ...
- 【转载】C#调用国家气象局天气预报接口
一.需求 最近,刚好项目中有天气预报查询功能的需求,要求录入城市名称,获取该城市今日天气信息及相关气象生活辅助信息等. 例如:查询北京市天气 结果为: 今日北京天气:15℃~5℃ 阵雨转阴,北风3-4 ...
- 使用session来存储用户的登录信息
对存在cookie端数据进行加密处理,具体代码如下: <?php session_start(); //假设用户登录成功获得了以下用户数据 $userinfo = array( 'uid' =& ...