Luogu 1379 八数码难题
吐槽:此题就是一点一点卡过去的
警告:
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 八数码难题的更多相关文章
- [luogu]P1379 八数码难题[广度优先搜索]
八数码难题 ——!x^n+y^n=z^n 我在此只说明此题的一种用BFS的方法,因为本人也是初学,勉勉强强写了一个单向的BFS,据说最快的是IDA*(然而蒟蒻我不会…) 各位如果想用IDA*的可以看看 ...
- luogu P1379 八数码难题
题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了 ...
- luogu P1379 八数码难题(A*算法入门详细讲解)
代码实现细节 #include<cstdio> #include<cstring> #include<iostream> using namespace std; ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
- Codevs 1225 八数码难题
1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的 ...
- 洛谷P1379八数码难题
题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中. 要求解的问题是:给出一种初始布局(初始状态)和目标布局(为 ...
- 洛谷 P1379 八数码难题 解题报告
P1379 八数码难题 题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初 ...
- 【洛谷P1379】八数码难题(广搜、A*)
八数码难题 题目描述 一.广搜: 首先要考虑用什么存每一个状态 显然每个状态都用一个矩阵存是很麻烦的. 我们可以考虑将一个3*3的矩阵用一个字符串或long long 存. 每次扩展时再转化为矩阵. ...
- 习题:八数码难题(双向BFS)
八数码难题(wikioi1225) [题目描述] 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出 ...
随机推荐
- 使用Gson轻松解决复杂结构的Json数据解析
转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50961803 JSON简介 JSON(JavaScript Object Notati ...
- ASP.NET Cache缓存的使用
ASP.NET Cache是提升系统性能的重要方法,它使用了“最近使用”原则(a least-recently-used algorithm).在数据库访问中经常会用到Cache保存数据库数据. 1. ...
- easyui datagrid 单元格加进度条(亲测可用)
{field: 'DataItemNum', title: '数据完整度', width: 100, formatter: function (v, r, i) { var p = (v / 27) ...
- php执行多个存储过程(二)
使用公共连接文件形式 include_once 'inc/conni.php'; $result = mysqli_query($conni, "call proc_test()" ...
- 支付宝RSA签名
1.参考网上相关文章,开放php中的openssl,但使用网上例子调用openssl_pkey_new,一直报100013错误.后改用用支付宝提供的SDKdemo程序 发现使用提供的privkye,可 ...
- 解决使用Qt creator时出现Cannot overwrite file ..Permission denied
前两天在linux下使用Qt creator, 切换到了管理员使用了Qt creator后,再切换为普通用户,发现出现了 Cannot overwrite file ..Permission deni ...
- UE4子弹特效
转自:http://blog.ch-wind.com/ue4-projectile-visual-effects/ 子弹使用抛体就可以实现了,但是要让其看起来更加真实,则可能需要加上一些粒子特效. 当 ...
- Oracle数据库Where条件执行顺序
由于SQL优化起来比较复杂,并且还会受环境限制,在开发过程中,写SQL必须必须要遵循以下几点的原则: 1.ORACLE采用自下而上的顺序解析WHERE子句,根据这个原理,表之间的连接必须写在其他WHE ...
- oracle查看表空间和物理文件大小
查看各表空间的使用情况 select a.tablespace_name,a.bytes/1024/1024 "Sum MB",(a.bytes-b.bytes)/1024/102 ...
- python学习笔记(三):文件操作和集合
对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 文件基本操作: f = open('file.txt','r') #以只读方式打开一个 ...