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来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出 ...
随机推荐
- Java基础总结大全
一.基础知识: 1.JVM.JRE和JDK的区别: JVM(Java Virtual Machine):java虚拟机,用于保证java的跨平台的特性. java语言是跨平台,jvm不是跨平台的. J ...
- SQL Server临时表的使用方案
文章来源:http://www.codesky.net/article/201007/145241.html 我们今天是要和大家一起讨论的是SQL Server临时表的实用大全,如果你对SQL S ...
- 利用DAC(Data-tier Application)实现数据库结构迁移
从一个存在的库,抽取其表结构,对象,权限等,再部署成一个不包含数据的"空库"的方法有很多种.如自带的Generate Scripts功能,自定义脚本提取创建脚本等. 在实际使用中, ...
- 学习动态性能表(5)--v$session
学习动态性能表 第五篇--V$SESSION 2007.5.29 在本视图中,每一个连接到数据库实例中的session都拥有一条记录.包括用户session及后台进程如DBWR,LGWR,arcch ...
- 洛谷【P1064】金明的预算方案
浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html 题目传送门:https://www.luogu.org/problemnew/show/P ...
- Chrome 的审查元素功能有哪些奇技淫巧
学习地址: https://www.zhihu.com/question/34682699
- Instantiate实例化的注意事项
_obj= Resources.Load("xxx") as GameObject;Instantiate(_obj); 这里的_obj对象和 _obj= Instantiate( ...
- tp5 快速接入扫码支付
前提是申请好微信支付,同时配置好key,以及支付回调地址 1.composer composer require yansongda/pay 2.引入 use Yansongda\Pay\Pay; / ...
- CVE-2017-11882复现配合koadic
项目地址:https://github.com/iBearcat/CVE-2017-11882 首先开启koadic,然后配置一下 复制这句代码 mshta http://192.168.220.13 ...
- js发送windows提示信息
js发送windows提示信息 效果图 代码 Notification.requestPermission(function() { if(Notification.permission === 'g ...