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 ...
随机推荐
- spring-boot 配置jsp
sring-boot 集成 jsp spring-boot默认使用的页面展示并不是jsp,若想要在项目中使用jsp还需要配置一番. 虽然spring-boot中也默认配置了InternalResou ...
- The sventh day
call it a day 是个相当古老的习惯用语,沿用至今已经有一百五十多年了,但是人民仍然常常用到她. call it a day 可不是“叫一天”的意思哦, 这里是收工的,下班的意思 I thi ...
- It does not do to dwell on dreams and forget to live.
It does not do to dwell on dreams and forget to live.不要过于依赖梦想,却忘了生活.
- 初学者:Git常用命令总结
git init 在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹. git clone 获取一个u ...
- PPPOE+FREERADIUS+MYSQL+LINUX
环境: OS:Linux Centos 6.9 x86_x64 PPPOE : ppp-2.4.7.tar.gz rp-pppoe-3.12.tar.gz FreeRadius : V3.0.X ...
- 项目移动后报error LNK1123
VS20101.解决方案窗口 项目|项目属性|配置属性|清单工具|输入和输出|嵌入清单 “是”改为“否”:2.项目|项目属性|配置属性|连接器|清单文件|嵌入清单 “是”改为“否”:3.对于64位的操 ...
- javascript模块化---requirejs
requirejs是异步执行 为什么会出现模块化1.不定什么时候,自己就将全局变量改变了2.函数名的冲突3.依赖关系不好管理如果b.js依赖a.js那么b必须放在a的下面解决的办法1.自执行函数来包装 ...
- PC:各大主板开机启动项快捷键
组装机主板 品牌笔记本 品牌台式机 主板品牌 启动按键 笔记本品牌 启动按键 台式机品牌 启动按键 华硕主板 F8 联想笔记本 F12 联想台式机 F12 技嘉主板 F12 宏基笔记本 F12 惠普台 ...
- IOS 核心动画(Core Animation)
Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它 能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就 可以实现非常强大的功能. Core ...
- Json的本地写入和读取,也可以方便在开发中数据的调试
不知道小伙伴们,在开发中,数据调试的过程中,尤其是很多状态的情况下调试,是不是总是麻烦后台的小哥改变不同的状态,总感觉这样太麻烦了, 那么就可以,把数据写入到本地,然后去沙盒中,找到这个写入的文件,直 ...