https://www.luogu.org/problemnew/show/P1379

long long ago

暴力bfs

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <queue>
#include <cstring>
#include <string> using namespace std;
const string s_end = ""; struct Node {
string s;
int step;
};
queue <Node> Q1;
map <string, bool> mp;
string s_start;
int Step; inline void pd(string ss, int answer) {
if(ss == s_end) {
printf("%d",answer);
exit();
}
} inline void bfs() {
while(!Q1.empty()) {
Node topp = Q1.front();
Q1.pop();
string s1 = topp.s;
Step = topp.step;
int f = s1.find('');
Node nxt;
if(f == ) {
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
continue;
}
if(f == ) {
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
continue;
}
if(f == ) {
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
continue;
}
if(f == ) {
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
continue;
}
if(f == ) {
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
continue;
}
if(f == ) {
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
continue;
}
if(f == ) {
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
continue;
}
if(f == ) {
//4 7 6 7 8 7
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
continue;
}
if(f == ) {
//58 78
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
swap(s1[], s1[]);
swap(s1[], s1[]);
pd(s1, topp.step + );
if(!mp[s1]) {
nxt.s = s1;
nxt.step = topp.step + ;
Q1.push(nxt);
mp[s1] = ;
}
continue;
}
}
} int main() {
cin >> s_start;
Node now;
now.s = s_start;
now.step = ;
Q1.push(now);
bfs();
return ;
}
//

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

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

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

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

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

  3. Codevs 1225 八数码难题

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

  4. 洛谷P1379八数码难题

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

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

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

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

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

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

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

  8. 「LuoguP1379」 八数码难题(迭代加深

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

  9. 洛谷——P1379 八数码难题

    P1379 八数码难题 双向BFS 原来双向BFS是这样的:终止状态与起始状态同时入队,进行搜索,只不过状态标记不一样而已,本题状态使用map来存储 #include<iostream> ...

随机推荐

  1. MacBook使用HHKB键盘设置

    问题: macbook上使用hhkb的时候,很纠结档位4要不要开,开启4号DIP开关后,虽然会使HHKB键盘上减少一个“◇(Win键)”键,但是会在键盘左侧多出一个“FN”键.多出来的左“FN”键,不 ...

  2. vs2019 扩展工具

    这里只是做个记录,没啥技术含量 本人代码上有些强迫症,所以我的本地代码一定不可以丢,之前用vs2013开始,就安装了localhistory这个插件,十分方便,觉得不用了,清了即可,也不占地方. 但是 ...

  3. CentOS7/RHEL6下,如何查看目录与子目录大小

    原文:CentOS7/RHEL6下,如何查看目录与子目录大小 通过强大的[du]命令,可以帮助我们快捷的查看目录的大小,非常实用. du命令用来查看目录或文件所占用磁盘空间的大小.常用选项组合为:du ...

  4. ThinkPad T420i 上 Ubuntu 12.04 实现指纹识别登录

    ThinkPad T420i 上 Ubuntu 12.04 实现指纹识别登录 # add ppa add-apt-repository ppa:fingerprint/fprint # update ...

  5. Navicat远程连接centos上mysql出错

    原因1:mysql账户是不允许远程连接 参考:centos安装mysql(for 小白) 打开远程连接 原因2:3306端口未开启 开启端口:iptables -I INPUT -p tcp --dp ...

  6. 货币转换B

    描述 人民币和美元是世界上通用的两种货币之一,写一个程序进行货币间币值转换,其中:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮ ...

  7. 常用的bug管理工具

    1. QC(Quality Center)是原Mercury Interactive公司(现已被HP收购)生产的企业级基于WEB測试管理工具,须要安装配置IIS和数据库.系统资源消耗比較 大:功能非常 ...

  8. zabbix主动模式和被动模式

    目录 一. 理论概述 二.总结 一. 理论概述 整个zabbix监控过程当中,对于agent来说有两种递交监控项信息的模式:主动模式和被动模式 主动模式 agent向server主动请求对应监控项列表 ...

  9. 检测jquery是否正确引入

    if(typeof(jQuery)=="undefined"){ alert("jQuery is not imported"); }else{ alert(& ...

  10. DoD与TCP/IP

    DoD与TCP/IP都是协议栈. 什么是协议栈? 就是一套软件,默认安装完Windows就有,可以卸载再安装.把他卸载了,你就不能上网. 数据的封装以及解封装有网卡以及绑定的TCP/IP协议栈完成 A ...