计蒜客 密码锁(BFS)
https://www.jisuanke.com/course/1797/121114
Description
现在一个紧急的任务是打开一个密码锁。密码由四位数字组成,每个数字从 1 到 9 进行编号。每次可以对任何数字加 1 或减 1。当将9加 1 时,数字将变为1,当1减 1 的时,数字将变为9。您也可以与邻居交换数字,每一个行动记做一步。现在你的任务是使用最小的步骤来打开锁。
注意:最左边的数字不是最右边数字的邻居。
Input
第一行输入四位数字,表示密码锁的初始状态。
第二行输入四位数字,表示开锁的密码。
Output
输出一个整数,表示最小步骤。
样例输入
样例输出
这道题最开始想不到用搜索来做。
不过这种题目还是很容易使用bfs来完成。
对于这道题目我们采用的标记方法和平时的标记也不太一样,我们是标记我们到达过的状态。
我们可以就是通过四维数组来完成标记,把由4个数字组成的一组密码记为一种状态,即:当密码为abcd时,对应的标记就应该记为vis[a][b][c][d]=1。
每次我们可以的操作有三种,第一种对于四位数字中的某一位加一,第二种对于四位数字中的某一位减一,第三种对于四位数字进行交换。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
using namespace std; struct node
{
int num[];
int step;
}first,last;
int vis[][][][]; void BFS()
{
queue<node> qe;
node t;
t=first;
qe.push(t);
vis[t.num[]][t.num[]][t.num[]][t.num[]]=;
while(!qe.empty())
{
t=qe.front();
qe.pop();
if(t.num[]==last.num[]&&t.num[]==last.num[]&&t.num[]==last.num[]&&t.num[]==last.num[])
{ //BFS出口
printf("%d\n",t.step);
return ;
}
for(int i=;i<;i++)//+1
{
node next=t;
next.num[i]++;
if(next.num[i]==) next.num[i]=;
if(!vis[next.num[]][next.num[]][next.num[]][next.num[]])
{
vis[next.num[]][next.num[]][next.num[]][next.num[]]=;
next.step++;
qe.push(next);
}
}
for(int i=;i<;i++)//-1
{
node next=t;
next.num[i]--;
if(next.num[i]==) next.num[i]=;
if(!vis[next.num[]][next.num[]][next.num[]][next.num[]])
{
vis[next.num[]][next.num[]][next.num[]][next.num[]]=;
next.step++;
qe.push(next);
}
}
for(int i=;i<;i++)//交换
{
node next=t;
swap(next.num[i],next.num[i+]);
if(!vis[next.num[]][next.num[]][next.num[]][next.num[]])
{
vis[next.num[]][next.num[]][next.num[]][next.num[]]=;
next.step++;
qe.push(next);
}
}
}
} int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif char str1[];
char str2[];
scanf("%s %s",str1,str2);
for(int i=;i<;i++)
{
first.num[i]=str1[i]-'';
last.num[i]=str2[i]-'';
}
BFS(); return ;
}
-
计蒜客 密码锁(BFS)的更多相关文章
- [计蒜客] 矿石采集【记搜、Tarjan缩点+期望Dp】
Online Judge:计蒜客信息学3月提高组模拟赛 Label:记搜,TarJan缩点,树状数组,期望Dp 题解 整个题目由毫无关联的两个问题组合成: part1 问题:对于每个询问的起点终点,求 ...
- 计蒜客 作弊揭发者(string的应用)
鉴于我市拥堵的交通状况,市政交管部门经过听证决定在道路两侧安置自动停车收费系统.当车辆驶入车位,系统会通过配有的摄像头拍摄车辆画面,通过识别车牌上的数字.字母序列识别车牌,通过连接车管所车辆信息数据库 ...
- 计蒜客的一道题dfs
这是我无聊时在计蒜客发现的一道题. 题意: 蒜头君有一天闲来无事和小萌一起玩游戏,游戏的内容是这样的:他们不知道从哪里找到了N根不同长度的木棍, 看谁能猜出这些木棍一共能拼出多少个不同的不等边三角形. ...
- 计蒜客模拟赛5 D2T1 成绩统计
又到了一年一度的新生入学季了,清华和北大的计算机系同学都参加了同一场开学考试(因为两校兄弟情谊深厚嘛,来一场联考还是很正常的). 不幸的是,正当老师要统计大家的成绩时,世界上的所有计算机全部瘫痪了. ...
- 计蒜客 等边三角形 dfs
题目: https://www.jisuanke.com/course/2291/182238 思路: 1.dfs(int a,int b,int c,int index)//a,b,c三条边的边长, ...
- 计蒜客 方程的解数 dfs
题目: https://www.jisuanke.com/course/2291/182237 思路: 来自:https://blog.csdn.net/qq_29980371/article/det ...
- 计蒜客 买书 dfs
题目: https://www.jisuanke.com/course/2291/182236 思路: 递归解决,从第一本书开始,每本书都有两种选择: //index是book里面每本书价格的下标, ...
- 计蒜客:Entertainment Box
Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...
- 爬虫acm比赛成绩(多页成绩整合在一起、获取复制不了的数据)(hihocoder、计蒜客)
https://github.com/congmingyige/web-crawler_rank-of-competition-in-JiSuanKe-and-hihocoder 1. 计蒜客(获取复 ...
随机推荐
- 十一、JavaScript之两种注释方法
一.代码如下 二.运行效果如下
- Pyinstaller的安装及简单使用
(1)安装: 用传统的pip install pyinstaller出错,在https://pypi.org/project/PyInstaller/#files上下载PyInstaller-3.4. ...
- 第七篇 Django-认证系统
Django-认证系统 阅读目录(Content) 1 Cookie 与 Session 概念 查看cookie 登陆应用 Django中操作Cookie 1.获取Cookie 2.设置Cookie ...
- springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面
1.总览 2.代码 1).pom.xml 这里注意:springboot 2.2.0以后默认的freemarker文件后缀为:ftlh.本例用的是2.2.1,所以后缀为ftlh <depende ...
- cf 1241 D. Sequence Sorting(思维)
题意: 一个序列有n个数,有一种操作,你可以选一个数x,使这个序列中等于x的数都移到序列头或尾. 问最少几次操作后,可以使这个序列非降序. 思路: (以下说bi移动到哪里,其实就是指a1……an中等于 ...
- 面向对象-static关键字实战案例
面向对象-static关键字实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.static关键字概述 1>.static的功能 static关键字用于修饰成员变量和 ...
- ajax 跨域webapi 最简单的demo(只介绍Get)
这几天遇到一个nodejs的项目,使用VSCode开发,需要连接数据库的,但是用nodejs连接数据库比较繁琐,需要安装很多东西,本人也懒得去研究了.后来想到建一个WebAPI然后用ajax来调用,避 ...
- bugku-杂项 听首音乐
下载文件,是个wav文件,用Audacity打开,发现有 放大后记录下来:(每一组后面加上空格) ..... -... -.-. ----. ..--- ..... -.... ....- ----. ...
- mariadb主从
实验环境: 两台centos7 master:192.168.1.6 slave:192.168.1.7 一.安装mariadb服务 [root@master ~]# yum -y install m ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...