题目传送门1 2

题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0

分析:八数码经典问题。POJ是一次,HDOJ是多次。因为康托展开还不会,也写不了什么,HDOJ需要从最后的状态逆向搜索,这样才不会超时。判重康托展开,哈希也可。

POJ

//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include <cstring>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>
using namespace std; const int N = 362880 + 5;
const int MOD = 1e6 + 7;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char dir[4] = {'u', 'd', 'l', 'r'};
struct Point {
int s, d;
string str;
Point () {}
Point (int s, int d, string str) : s (s), d (d), str (str) {}
};
struct Hash_table {
struct Edge {
int v, nex;
}edge[MOD];
int head[MOD], e;
void init(void) {
memset (head, -1, sizeof (head));
e = 0;
}
bool insert(int x) {
int u = (x % MOD + MOD) % MOD;
for (int i=head[u]; ~i; i=edge[i].nex) {
if (edge[i].v == x) return false;
}
edge[e].v = x; edge[e].nex = head[u];
head[u] = e++;
return true;
}
}ha;
int vis[N], fact[9]; void decode(int x, int *b) {
for (int i=8; i>=0; --i) {
b[i] = x % 10;
x /= 10;
}
} int encode(int *b) {
int ret = 0;
for (int i=0; i<9; ++i) {
ret = ret * 10 + b[i];
}
return ret;
} int find_0(int *b) {
for (int i=0; i<9; ++i) {
if (b[i] == 0) return i;
}
return -1;
} bool check(int x, int y) {
if (x < 0 || x >= 3 || y < 0 || y >= 3) return false;
else return true;
} void print(int *b) {
for (int i=0; i<9; ++i) {
printf ("%d ", b[i]);
if (i == 2 || i == 5 || i == 8) puts ("");
}
} void init(void) {
fact[0] = 1;
for (int i=1; i<9; ++i) fact[i] = fact[i-1] * i;
memset (vis, false, sizeof (vis));
} bool can_insert(int *b) {
int code = 0;
for (int i=0; i<9; ++i) {
int cnt = 0;
for (int j=i+1; j<9; ++j) if (b[j] < b[i]) cnt++;
code += fact[8-i] * cnt;
}
if (vis[code]) return false;
else {
vis[code] = true;
return true;
}
} void BFS(int *a) {
init ();
int ans[9] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
int s = encode (a);
queue<Point> que; que.push (Point (s, 0, ""));
while (!que.empty ()) {
Point u = que.front (); que.pop ();
int b[9];
decode (u.s, b);
if (memcmp (ans, b, sizeof (b)) == 0) {
int len = u.str.length ();
for (int i=0; i<len; ++i) {
printf ("%c", u.str[i]);
}
puts ("");
return ;
}
int p = find_0 (b);
int x = p / 3, y = p % 3;
for (int i=0; i<4; ++i) {
int tx = x + dx[i], ty = y + dy[i];
if (!check (tx, ty)) continue;
int p2 = tx * 3 + ty;
int t[9];
memcpy (t, b, sizeof (b));
t[p] = t[p2]; t[p2] = 0;
int v = encode (t);
//if (!ha.insert (v)) continue;
if (!can_insert (t)) continue;
que.push (Point (v, u.d + 1, u.str + dir[i]));
}
}
puts ("unsolvable");
} int main(void) {
char c[9];
int a[9];
while (scanf ("%c %c %c %c %c %c %c %c %c", &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7], &c[8]) == 9) {
for (int i=0; i<9; ++i) {
if (c[i] >= '1' && c[i] <= '9') {
a[i] = c[i] - '0';
}
else a[i] = 0;
}
BFS (a);
} return 0;
}

  

HDOJ

#include <bits/stdc++.h>
using namespace std; const int N = 362880 + 5;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char dir[4] = {'d', 'u', 'r', 'l'};
struct Point {
int s;
int b[9];
};
struct Ans {
char dir;
int fa;
}ans[N];
int fact[9]; int find_0(int *b) {
for (int i=0; i<9; ++i) {
if (b[i] == 9) return i;
}
return -1;
} bool check(int x, int y) {
if (x < 0 || x >= 3 || y < 0 || y >= 3) return false;
else return true;
} void init(void) {
fact[0] = 1;
for (int i=1; i<9; ++i) fact[i] = fact[i-1] * i;
for (int i=0; i<N; ++i) ans[i].fa = -1;
} int Cantor(int *b) {
int code = 0;
for (int i=0; i<9; ++i) {
int cnt = 0;
for (int j=i+1; j<9; ++j) if (b[j] < b[i]) cnt++;
code += fact[8-i] * cnt;
}
return code;
} void BFS() {
init ();
Point sta;
for (int i=0; i<9; ++i) {
sta.b[i] = i + 1;
}
sta.s = 0; ans[sta.s].fa = 0;
queue<Point> que; que.push (sta);
while (!que.empty ()) {
Point u = que.front (); que.pop ();
int p = find_0 (u.b);
int x = p / 3, y = p % 3;
for (int i=0; i<4; ++i) {
Point v = u;
int tx = x + dx[i], ty = y + dy[i];
if (!check (tx, ty)) continue;
int p2 = tx * 3 + ty;
swap (v.b[p], v.b[p2]);
v.s = Cantor (v.b);
if (ans[v.s].fa != -1) continue;
ans[v.s].dir = dir[i];
ans[v.s].fa = u.s;
que.push (v);
}
}
} int main(void) {
BFS ();
char c[55];
int a[9];
while (gets (c)) {
int j = 0;
for (int i=0; c[i]; ++i) {
if (c[i] >= '0' && c[i] <= '8') {
a[j++] = c[i] - '0';
}
else if (c[i] == 'x') a[j++] = 9;
}
int s = Cantor (a);
if (ans[s].fa == -1) puts ("unsolvable");
else {
while (s != 0) {
printf ("%c", ans[s].dir);
s = ans[s].fa;
}
puts ("");
}
} return 0;
}

  

BFS(八数码) POJ 1077 || HDOJ 1043 Eight的更多相关文章

  1. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  2. POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3

    http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...

  3. POJ 1077 HDU 1043 Eight (IDA*)

    题意就不用再说明了吧......如此经典 之前想用双向广搜.a*来写,但总觉得无力,现在用IDA*感觉其他的解法都弱爆了..............想法活跃,时间,空间消耗很小,给它跪了 启发式搜索关 ...

  4. Poj 1077 eight(BFS+全序列Hash解八数码问题)

    一.题意 经典的八数码问题,有人说不做此题人生不完整,哈哈.给出一个含数字1~8和字母x的3 * 3矩阵,如: 1  2  X            3 4  6            7  5  8 ...

  5. hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...

  6. poj 1077 Eight (八数码问题——A*+cantor展开+奇偶剪枝)

    题目来源: http://poj.org/problem?id=1077 题目大意: 给你一个由1到8和x组成的3*3矩阵,x每次可以上下左右四个方向交换.求一条路径,得到12345678x这样的矩阵 ...

  7. HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  8. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  9. HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

随机推荐

  1. CSS设置超出指定宽度自动换行

    一.背景 最近项目中有用到在div中显示用户反馈的信息,是指定宽度的div,超出要自动换行,开始写好后感觉应该没什么问题,后来自己随便输入测试数据的时候发现:如果是纯字母或者是纯数字就会出现超出了也不 ...

  2. [Android Pro] Android下toolbox简介

    toolbox是Android 自带的提供shell命令的软件.有点类似于busybox,但功能上好像弱很多.其源码可以从Android source code 中system/core/toolbo ...

  3. 创建DLL、Lib以及使用DLL、Lib

    1.要在生成DLL文件的同时生成Lib文件,函数声明时前面要加__declspec(dllexport). 可在头文件中如下定义: #ifndef __MYDLL_H#define __MYDLL_H ...

  4. Android中shell命令语句

    最近学习了Android中碰到了shell命令,故收集终结了一下 Ccat zdd 浏览文件zdd的内容cat zdd1 zdd2 浏览多个文件的内容cat -n zdd浏览文件zdd的内容并显示行号 ...

  5. elk安装(这个是初级的可以把这个套件安上)

    http://udn.yyuap.com/doc/logstash-best-practice-cn/index.html ELK其实并不是一款软件,而是一整套解决方案,是三个开源软件Elastics ...

  6. 用#define来实现多份近似代码 - map,set中的应用

    在stl中map,set内部都是使用相同的红黑树实现,map对应模板参数key_type,mapped_type,而set对应模板参数没有mapped_type 两者都支持insert操作 pair& ...

  7. C# 使用Conditional特性而不是#if条件编译

    概述 #if/#endif 语句常用来基于同一份源码生成不同的编译结果,其中最常见的就是debug版和release版.但是这些工具在实际应用中并不是非常友好,因为它们容易被滥用,其代码页进而难以理解 ...

  8. python生成RSS(PyRSS2Gen)

    既然能够用python解析rss,那么也顺带研究下生成rss. 其实很简单,只是生成一个比较特殊点的xml文档而已. 这里我使用了PyRss2Gen,用法很简单,看代码就知道了,如下: import ...

  9. hdu 4756 MST+树形dp ****

    题意:给你n(n = 1000)个二维点,第一个点是power plant,还有n - 1个点是dormitories.然后现在知道有一条寝室到寝室的边是不能连的,但是我们不知道是哪条边,问这种情况下 ...

  10. Win7下用IIS发布网站

    安装IIS控制面板->程序->程序和功能, 点击左侧的“打开或关闭Windows功能”把这几项都勾上吧,虽然有些不是必须的,多勾无碍. 进入IIS管理器控制面板-> 系统和安全-&g ...