BFS:Open and Lock(一个数的逐位变化问题的搜索)
解体心得:
1、关于定义四维数组的问题,在起初使用时,总是在运行时出错,找了很多方法,最后全部将BFS()部分函数写在主函数中,将四维数组定义在主函数中才解决了问题。运行成功后再次将四维数组定义为全局变量,BFS()函数独立出来没发生运行错误。很纠结,找了三天的BUG!
2、关于一个数的逐位变换,BFS()中有三个主要变换+1循环,-1循环,邻位交换循环。思路清晰,简单。
3、注意step+1的位置与Next = now 的位置的关系!
题目:
Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9.
Each time, you can add or minus 1 to any digit. When add 1 to ‘9’, the digit will change to be ‘1’ and when minus 1 to ‘1’, the digit will change to be ‘9’. You can also exchange the digit with its neighbor. Each action will take one step.
Now your task is to use minimal steps to open the lock.
Note: The leftmost digit is not the neighbor of the rightmost.dgit.
Input
The input file begins with an integer T, indicating the number of test cases.
Each test case begins with a four digit N, indicating the initial state of the password lock. Then followed a line with anotther four dight M, indicating the password which can open the lock. There is one blank line after each test case.
Output
For each test case, print the minimal steps in one line.
Sample Input
2
1234
2144
1111
9999
Sample Output
2
4
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
char now1[4],Next1[4];
int use[10][10][10][10];
struct num1
{
int a[4];
int step;
}now,Next,aim;
void BFS()
{
queue <num1> qu;
qu.push(now);
use[now.a[0]][now.a[1]][now.a[2]][now.a[3]] = 1;
while(!qu.empty())
{
now = qu.front();
qu.pop();
//交换部分
for(int i=0;i<3;i++)
{
Next = now;
Next.step = now.step + 1;
Next.step = now .step + 1;
swap(Next.a[i],Next.a[i+1]);
if(Next.a[0] == aim.a[0] && Next.a[1] == aim.a[1] && Next.a[2] == aim.a[2] && Next.a[3] == aim.a[3])
{
printf("%d\n",Next.step);
return;
}
if(!use[Next.a[0]][Next.a[1]][Next.a[2]][Next.a[3]])
{
qu.push(Next);
use[Next.a[0]][Next.a[1]][Next.a[2]][Next.a[3]] = 1;
}
}
//+1部分
for(int i=0;i<4;i++)
{
Next = now;
Next.step = now.step + 1;
Next.a[i] = now.a[i] + 1;
if(Next.a[i] == 0)
Next.a[i] = 9;
if(Next.a[i] == 10)
Next.a[i] = 1;
if(Next.a[0] == aim.a[0] && Next.a[1] == aim.a[1] && Next.a[2] == aim.a[2] && Next.a[3] == aim.a[3])
{
printf("%d\n",Next.step);
return;
}
if(!use[Next.a[0]][Next.a[1]][Next.a[2]][Next.a[3]])
{
qu.push(Next);
use[Next.a[0]][Next.a[1]][Next.a[2]][Next.a[3]] = 1;
}
}
//-1部分
for(int i=0;i<4;i++)
{
Next = now;
Next.step = now.step + 1;
Next.a[i] = now.a[i] - 1;
if(Next.a[i] == 0)
Next.a[i] = 9;
if(Next.a[i] == 10)
Next.a[i] = 1;
if(Next.a[0] == aim.a[0] && Next.a[1] == aim.a[1] && Next.a[2] == aim.a[2] && Next.a[3] == aim.a[3])
{
printf("%d\n",Next.step);
return;
}
if(!use[Next.a[0]][Next.a[1]][Next.a[2]][Next.a[3]])
{
qu.push(Next);
use[Next.a[0]][Next.a[1]][Next.a[2]][Next.a[3]] = 1;
}
}
}
}
int main()
{
int t;
int sum_step;
scanf("%d",&t);
while(t--)
{
memset(use,0,sizeof(use));
scanf("%s",now1);
getchar();
scanf("%s",Next1);
for(int i=0;i<4;i++)
now.a[i] = now1[i] - '0';
for(int i=0;i<4;i++)
aim.a[i] = Next1[i] - '0';
now.step = 0;
BFS();
}
}
BFS:Open and Lock(一个数的逐位变化问题的搜索)的更多相关文章
- IP地址与子网掩码逐位相与
逐位相与说的其实就是子网掩码与网络地址相同位置的数字相加,当和为2的时候该位置写作1,否则的话写作0
- Js 分别取一个数的百位,十位,个位
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- LightOJ - 1396 :Palindromic Numbers (III)(逐位确定法)
Vinci is a little boy and is very creative. One day his teacher asked him to write all the Palindrom ...
- 删除一个数的K位使原数变得最小
原创 给定一个n位正整数a, 去掉其中k个数字后按原左右次序将组成一个新的正整数.对给定的a, k寻找一种方案,使得剩下的数字组成的新数最小. 提示:应用贪心算法设计求解 操作对象为n位正整数,有可能 ...
- java实现串逐位和(C++)
给定一个由数字组成的字符串,我们希望得到它的各个数位的和. 比如:"368" 的诸位和是:17 这本来很容易,但为了充分发挥计算机多核的优势,小明设计了如下的方案: int f(c ...
- 深入理解KMP算法
前言:本人最近在看<大话数据结构>字符串模式匹配算法的内容,但是看得很迷糊,这本书中这块的内容感觉基本是严蔚敏<数据结构>的一个翻版,此书中给出的代码实现确实非常精炼,但是个人 ...
- IM通信协议逆向分析、Wireshark自定义数据包格式解析插件编程学习
相关学习资料 http://hi.baidu.com/hucyuansheng/item/bf2bfddefd1ee70ad68ed04d http://en.wikipedia.org/wiki/I ...
- 数据结构(复习)---------字符串-----KMP算法(转载)
字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...
- PC/FORTH 判定
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
随机推荐
- SpringBoot | 第二十三章:日志管理之整合篇
前言 在本系列<第四章:日志管理>中,由于工作中日志这块都是走默认配置,也没有深入了解过,因为部署过程中直接使用了linux中的输出重定向功能,如java -jar xx.jar > ...
- maven安装,使用说明,及maven Repository如何使用.
maven的使用方法总结一下 1.首先去apache官网下载maven, http://maven.apache.org/download.cgi2.如果是windows系统,选择 apache-ma ...
- 去除pycharm的波浪线
PyCharm使用了较为严格的PEP8的检查规则,如果代码命名不规范,甚至多出的空格都会被波浪线标识出来,导致整个编辑器里铺满了波浪线,右边的滚动条也全是黄色或灰色的标记线,很是影响编辑.这里给大家分 ...
- Vue2.0搭建脚手架(vue-cli)
一.安装node.js 进入官网下载node.js 二.安装 cnpm 1.说明:npm(node package manager)是nodejs的包管理器,用于node插件管理(包括安装.卸载.管理 ...
- Nobody gives away anything valuable for free.
Nobody gives away anything valuable for free.没人会给你免费的午餐.
- 树莓派-(一)开箱到点亮一些坑(无屏、无wlan、无直连键鼠)
0x00.前期准备: 材料: 树莓派3b+ 板子 * 1,适配电源 * 1,网线 * 2,sd卡16G * 1,读卡器 * 1 安装时注意,3b+三个散热片贴好.小风扇接线要接对 工具: 0x01. ...
- Mybatis介绍(一)
这里介绍的mybatis比较简单, 我做为一个初学者, 记录下个人在学习中方法, 如果那里出错, 希望读者朋友们见谅. 首先这里介绍一下我们下面用的表结构: author表是保存了作者的个人信息, 因 ...
- 开发Maven插件
Mojo: Maven plain Old Java Object 1.插件命名规则:maven-<yourplugin>-plugin是Maven的保留字段,不允许使用,我们可以用< ...
- nginx配置文件语法高亮
下载文件 nginx.vim https://vim.sourceforge.io/scripts/script.php?script_id=1886 安装 下载 nginx.vim 文件到 ~/.v ...
- 绿盟网站安全防护服务(vWAF)
平台: linux 类型: 虚拟机镜像 软件包: basic software devops nsfocus security waf 服务优惠价: 按服务商许可协议 云服务器费用:查看费用 立即部署 ...