吐槽:此题就是一点一点卡过去的
警告:
1、千万不能用dfs搜这种东西(dfs需要遍历所有状态才能找到最优解), 分分钟爆炸
2、写结构体的时候要综合判断&的加和不加

Code:

// luogu-judger-enable-o2
#include <cstdio>
#include <map>
#include <queue>
using namespace std;
typedef long long ll; const int N = ;
const int M = 1e6 + ;
const int dx[] = {, -, , };
const int dy[] = {, , , -}; struct Node {
int s[N][N], nx, ny, stp; inline ll has() {
ll res = , base = ;
for(int i = ; i >= ; i--, base *= 1ll * ) {
int x = i / + , y = i % + ;
res += 1ll * s[x][y] * base;
}
return res;
} /* inline void print() {
for(int i = 1; i <= 3; i++, printf("\n"))
for(int j = 1; j <= 3; j++)
printf("%d ", s[i][j]);
system("pause");
} */ }f[M], tar;
map <ll, int> vis; inline bool chk(const Node &now) {
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
if(now.s[i][j] != tar.s[i][j])
return ;
return ;
} inline bool vivid(int x, int y) {
return x >= && x <= && y >= && y <= ;
} inline void swap(int &x, int &y) {
int t = x;
x = y;
y = t;
} /* void dfs(int x, int y, int stp) {
if(chk()) {
printf("%d\n", stp - 1);
exit(0);
} ll h = in.has();
if(vis.count(h)) return;
vis[h] = 1;
for(int k = 0; k < 4; k++) {
int tox = x + dx[k], toy = y + dy[k];
if(vivid(tox, toy)) {
swap(in.s[x][y], in.s[tox][toy]); // in.print(); dfs(tox, toy, stp + 1); swap(in.s[x][y], in.s[tox][toy]);
}
}
vis[h] = 0;
} */ int bfs() {
if(chk(f[])) return ;
vis[f[].has()] = ; for(int head = , tail = ; head <= tail; ) {
Node out = f[++head]; for(int k = ; k < ; k++) {
int tox = out.nx + dx[k], toy = out.ny + dy[k]; if(vivid(tox, toy)) {
Node in = out;
in.nx = tox, in.ny = toy, in.stp++;
swap(in.s[out.nx][out.ny], in.s[tox][toy]); ll inh = in.has();
if(!vis.count(inh)) {
vis[inh] = ;
if(chk(in)) return in.stp;
f[++tail] = in;
} } }
}
} inline void init() {
tar.s[][] = , tar.s[][] = , tar.s[][] = ;
tar.s[][] = , tar.s[][] = , tar.s[][] = ;
tar.s[][] = , tar.s[][] = , tar.s[][] = ;
} int main() {
char str[N << ];
scanf("%s", str);
for(int i = ; i <= ; i++) {
if(str[i] - == ) f[].nx = i / + , f[].ny = i % + ;
f[].s[i / + ][i % + ] = str[i] - ;
}
f[].stp = ;
// printf("%lld\n", f[1].has());
init(); // dfs(sx, sy, 1);
printf("%d\n", bfs()); return ;
}

Luogu 1379 八数码难题的更多相关文章

  1. [luogu]P1379 八数码难题[广度优先搜索]

    八数码难题 ——!x^n+y^n=z^n 我在此只说明此题的一种用BFS的方法,因为本人也是初学,勉勉强强写了一个单向的BFS,据说最快的是IDA*(然而蒟蒻我不会…) 各位如果想用IDA*的可以看看 ...

  2. luogu P1379 八数码难题

    题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了 ...

  3. luogu P1379 八数码难题(A*算法入门详细讲解)

     代码实现细节 #include<cstdio> #include<cstring> #include<iostream> using namespace std; ...

  4. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

  5. Codevs 1225 八数码难题

    1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的 ...

  6. 洛谷P1379八数码难题

    题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中. 要求解的问题是:给出一种初始布局(初始状态)和目标布局(为 ...

  7. 洛谷 P1379 八数码难题 解题报告

    P1379 八数码难题 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初 ...

  8. 【洛谷P1379】八数码难题(广搜、A*)

    八数码难题 题目描述 一.广搜: 首先要考虑用什么存每一个状态 显然每个状态都用一个矩阵存是很麻烦的. 我们可以考虑将一个3*3的矩阵用一个字符串或long long 存. 每次扩展时再转化为矩阵. ...

  9. 习题:八数码难题(双向BFS)

    八数码难题(wikioi1225) [题目描述] 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出 ...

随机推荐

  1. 【spring源码学习】spring的IOC容器之BeanFactoryPostProcessor接口学习

    [一]org.springframework.beans.factory.config.BeanFactoryPostProcessor接口==>该接口实现方法的执行时机:该接口void pos ...

  2. ACM学习历程—51NOD 1412 AVL树的种类(递推)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1770 这是这次BSG白山极客挑战赛的B题.设p(i, j)表示节点个数为 ...

  3. 洛谷 P2920 [USACO08NOV]时间管理Time Management

    传送门 题目大意: 每个工作有截至时间和耗费时间,n个工作求最小开始时间. 题解: 贪心 从n-1安排,让结束时间尽量的晚. 注意:优先级 cout<<st<0?-1:st;  (X ...

  4. dataView 工具栏

    option = { tooltip : { trigger: 'axis' }, legend: { data:['最高','最低'] }, toolbox: { show : true, orie ...

  5. shell中字体变色

    在linux中给字体使用数字代码变色 字体颜色代码:重置0 ,黑色30,红色31,绿色32,黄色33,蓝色34,洋红35,青色36,浅灰37 效果代码:1m加粗  2m加下划线  5m闪动效果  7m ...

  6. UBUNTU readelf的安装

    大多数情况下,linux环境上默认可能都装有readelf,但是也有少数情况可能没有装,我自己用的ubuntu的linux虚拟机就没有装readelf.readelf本身是一个分析elf很好用的工具. ...

  7. Spring Cloud Eureka 注册,发布,调用服务

    Spring Cloud为服务治理做了一层抽象接口,所以在Spring Cloud应用中可以支持多种不同的服务治理框架,如Netflix Eureka.Consul和Zookeeper.在Spring ...

  8. 查询mysql 哪些表正在被锁状态

    查询mysql 哪些表正在被锁状态 show OPEN TABLES where In_use > 0; 参考链接:http://zhidao.baidu.com/link?url=tCQ70t ...

  9. Unity3D版本之我见

    关心Unity版本的变化以及了解未来版本的内容是专业做Unity的同学必备的功课,下面我来说一下我对4.0以后版本的一些见解. v4.0: 这个版本比3.5有较大的跳跃,首先最大卖点是新的动作系统Me ...

  10. ll | wc -l的陷阱

    在平时我们可能经常使用ls和wc命令来统计某个文件夹下指定类型文件的个数.今天在使用的时候发现ls和ll出来的结果不一样. dwapp@pttest1:/home/dwapp/joe.wangh/te ...