[HDOJ1043]Eight(康托展开 BFS 打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043
八数码问题,因为固定了位置所以以目标位置开始搜索,把所有情况(相当于一个排列)都记录下来,用康托展开来完成序列的记录问题开始BFS。打好表后按给出序列的康托数确定开始位置,逆向查找。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef struct Node1 {
char s;
int pre;
Node1() { pre = -; }
}Node1;
typedef struct Node2 {
int status[];
int n, son;
}Node2; Node1 path[];
int dx[] = {, -, , };
int dy[] = {, , , -};
int fac[]; void init() {
fac[] = fac[] = ;
for(int i = ; i <= ; i++) {
fac[i] = fac[i-] * i;
}
} int ecantor(int* s, int n = ) {
int num = ;
for(int i = ; i < n; i++) {
int tmp = ;
for(int j = i + ; j < n; j++) {
if(s[j] < s[i]) {
tmp++;
}
}
num += fac[n--i] * tmp;
}
return num;
} void bfs() {
queue<Node2> q;
Node2 a, b;
int t = ;
for(int i = ; i < ; i++) {
a.status[i] = i + ;
}
a.n = ; a.son = ;
path[a.son].pre = ;
q.push(a);
while(!q.empty()) {
a = q.front(); q.pop();
for(int i = ; i < ; i++) {
b = a;
int xx = a.n % + dx[i];
int yy = a.n / + dy[i];
if(!(xx >= && xx < && yy >= && yy < )) continue;
b.n = yy * + xx;
swap(b.status[b.n], b.status[a.n]);
b.son = ecantor(b.status);
if(path[b.son].pre == -) {
path[b.son].pre = a.son;
if(i == ) path[b.son].s = 'l';
if(i == ) path[b.son].s = 'r';
if(i == ) path[b.son].s = 'u';
if(i == ) path[b.son].s = 'd';
q.push(b);
}
}
}
} int main() {
// freopen("in", "r", stdin);
init();
int s, ss[];
char ch[];
bfs();
while(gets(ch)) {
int cnt = ;
for(int i = ; ch[i]; i++) {
if(ch[i] == 'x') {
ss[cnt++] = ;
}
else if(ch[i] >= '' && ch[i] <= '') {
ss[cnt++] = ch[i] - '';
}
}
s = ecantor(ss);
if(path[s].pre == -) {
printf("unsolvable\n");
continue;
}
while(s != ) {
printf("%c", path[s].s);
s = path[s].pre;
}
printf("\n");
}
return ;
}
[HDOJ1043]Eight(康托展开 BFS 打表)的更多相关文章
- HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3
http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu1043Eight (经典的八数码)(康托展开+BFS)
建议先学会用康托展开:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description The 15-puzzle ...
- poj1077(康托展开+bfs+记忆路径)
题意:就是说,给出一个三行三列的数组,其中元素为1--8和x,例如: 1 2 3 现在,需要你把它变成:1 2 3 要的最少步数的移动方案.可以右移r,左移l,上移u,下移dx 4 6 4 5 67 ...
- HDU1043 八数码(BFS + 打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...
- 转换地图 (康托展开+预处理+BFS)
Problem Description 在小白成功的通过了第一轮面试后,他来到了第二轮面试.面试的题目有点难度了,为了考核你的思维能量,面试官给你一副(2x4)的初态地图,然后在给你一副(2x4)的终 ...
- hdu-1043(八数码+bfs打表+康托展开)
参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...
随机推荐
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
- iTunes Connect TERMS OF SERVICE
iTunes Connect TERMS OF SERVICE THESE TERMS OF SERVICE CONSTITUTE A LEGAL AGREEMENT BETWEEN YOU AND ...
- .NET设计模式(7):创建型模式专题总结(Creational Pattern)(转)
概述 创建型模式,就是用来创建对象的模式,抽象了实例化的过程.它帮助一个系统独立于如何创建.组合和表示它的那些对象.本文对五种常用创建型模式进行了比较,通过一个游戏开发场景的例子来说该如何使用创建型模 ...
- android中PreferenceScreen类的用法
PreferenceScreen preference是偏好,首选的意思,PreferenceScreen个人翻译成 “偏好显示”,明白这个意思就好,就是说根据特点灵活的定义显示内容风格,一个屏幕可以 ...
- 遭遇Asp.Net长文件名下载的问题和解决办法
在Asp.Net中写了一个附件上传和下载的程序,附件上传到数据库中,然后将附件的GUID保存起来,我们可以根据GUID来找到数据库中的附件,一般附件下载的代码是: <!--<br /> ...
- jQuery中的Deferred-详解和使用
首先,为什么要使用Deferred? 先来看一段AJAX的代码: var data; $.get('api/data', function(resp) { data = resp.data; }); ...
- C# Task的使用---Task的启动
.NET 4.0包含的新名称空间System.Threading.Tasks,它包含的类抽象出了线程功能.任务表示应完成的某个单元的工作.这个单元的工作可以在单独的线程中运行,也可以以同步的方式启动一 ...
- Spark源码分析(一)-Standalone启动过程
原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3858065.html 为了更深入的了解spark,现开始对spark源码进行分析,本系列文章以spark ...
- C# Socket 入门2(转)
现在来传一个图片看看, 改改程序, 看看服务端 图片为 140K, 1.jgp 1. 服务端 1 using System; 2 using System.Collections.Generic; ...
- 【PSR规范专题(3)】PSR-2 代码风格规范
[PSR规范专题(3)]PSR-2 代码风格规范 标签(空格分隔): PHP 转载自:https://github.com/PizzaLiu/PHP-FIG/blob/master/PSR-2-cod ...