吐槽:此题就是一点一点卡过去的
警告:
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. MAC OS X常用命令总结2

    1. dir:显示某个目录下的子目录与文件. 格式:dir [x:] [Path] [filename][ parameter] 参数解释: /a      显示所有文件夹与文件. /p     分页 ...

  2. openfaas 私有镜像配置

    备注: 此项目是使用nodejs  生成唯一id 的\ 预备环境 docker harbor faas-cli openfaas k8s 1. 项目初始化 faas-cli new node --la ...

  3. fn project 数据库配置

    Databases We currently support the following databases and they are passed in via the DB_URL environ ...

  4. 简述MVC模式

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  5. (转)RadioButton左侧显示文字,右侧显示按钮时文字不靠边的问题解决

    作者:  发布日期:2014-02-13 21:00:45 我来说两句(0) 0 Tag标签:RadioButton  左侧  显示   项目中有一个这样的需求: 下面三行点击某行即选中,颜色变深.自 ...

  6. OpenCL™ 2.0 – Pipes

    copy from http://developer.amd.com/community/blog/2014/10/31/opencl-2-0-pipes/ OpenCL™ 2.0 – Pipes I ...

  7. WebService简单实现

    1. WebService SOAP.WSDL.UDDISOAP(Simple Object Access Protocal,简单对象访问协议),是在分散或在分布式环境中交换信息的简单协议.WSDL( ...

  8. Linux yum操作时出现Error: xz compression not available

    yum升级PHP版本的时候出现这个问题 由于CentOS6的系统安装了epel-release-latest-7.noarch.rpm 导致在使用yum命令时出现Error: xz compressi ...

  9. contiki学习心路历程【转】xukai871105 大神

    https://blog.csdn.net/xukai871105/article/details/9072993

  10. thinkphp中的session的使用和理解!

    session的作用:session可以长时间的保存数据,不丢失. session的常用于: 1.登录,保存登录信息 2.保存购物车信息 3.保存验证码信息 定义session常量 define('W ...