吐槽:此题就是一点一点卡过去的
警告:
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. 使用appassembler-maven-plugin插件生成启动脚本

    appassembler-maven-plugin可以自动生成跨平台的启动脚本,省去了手工写脚本的麻烦,而且还可以生成jsw的后台运行程序. 首先pom引入相关依赖 <build> < ...

  2. [转]HTTP 协议中的 Transfer-Encoding

    本文作为我的博客「HTTP 相关」专题新的一篇,主要讨论 HTTP 协议中的 Transfer-Encoding.这个专题我会根据自己的理解,以尽量通俗的讲述,结合代码示例和实际场景来说明问题,欢迎大 ...

  3. bzoj 1499 [NOI2005]瑰丽华尔兹——单调队列优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 简单的单调队列优化dp.(然而当时却WA得不行.今天总算填了坑) 注意滚动数组赋初值应 ...

  4. RK3288 dts文件中背光配置参数

    backlight { // 设备名(用于匹配) compatible = "pwm-backlight"; // pwm编号 通道 频率(ns) pwms = <& ...

  5. Volley请求图片

    ImageRequest imageRequest = new ImageRequest(Config.USER_ASSETS_URL + md5(userid) + "/images/av ...

  6. MySQL error : Deadlock found when trying to get lock; try restarting transaction

    在使用 MySQL 时,我们有时会遇到这样的报错:“Deadlock found when trying to get lock; try restarting transaction”. 在 14. ...

  7. java图形用户界面练习。j2se

    总结:主要是如何布局.还是存在很大问题 package clientFrame; import java.awt.FlowLayout; import javax.swing.JButton; imp ...

  8. java代码求输入的三个数的最大值

    package com.c2; import java.util.Random; import java.util.Scanner; //输入10个数,找出最大一个数,并打印出来. public cl ...

  9. python开发socket套接字:粘包问题&udp套接字&socketserver

    一,发生粘包 服务器端 from socket import * phone=socket(AF_INET,SOCK_STREAM) #套接字 phone.setsockopt(SOL_SOCKET, ...

  10. socket通讯实例与TCP/UDP的区别

    一.socket代码实例 1.简单的socket通讯: 服务端代码实例: import socket sock = socket.socket(socket.AF_INET, socket.SOCK_ ...