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 ...
随机推荐
- 工作经验(Unity篇)
我的工作是C++开发,主要是做底层,其中绝大部分是给Unity调用的,以下是我的脚印,希望不会重蹈覆辙 Unity具有强大的跨平台性,但是使用到库文件不尽相同,例如Android中就使用so库文件,W ...
- 关于wav文件fft处理后x,y轴坐标数据的问题
1.关于横坐标的频率的最大值是采样频率,那么每个点对应的频率值就很好算了:f(n) = [Fs/(N/2)]*n (Fs是采样频率,常见的是44.1KHz(44100),N是采样点数,k表是第k个点 ...
- 处理 wait millis 60009, active 50 ,maxactive 200 异常 过程
处理 wait millis 60009, active 50 ,maxactive 200 异常 过程2018年04月19日 16:48:46 守望dfdfdf 阅读数:1910 标签: druid ...
- Java虚拟机,类文件结构深度解析
Java类文件结构 Java虚拟机不和包括Java在内的任何语言绑定,只与 "Class文件" 这种特定的二进制文件所关联, Class文件中包含了Java虚拟机指令集合符号表以及 ...
- nodejs入门学习笔记二——解决阻塞问题
在最开始,我们要弄清楚node会什么会存在阻塞? node是这么标榜自己的:“在node中除了代码,所有一切都是并行执行的!” 意思是,Node.js可以在不新增额外线程的情况下,依然可以对任务进行并 ...
- Servlet的生命周期以及线程安全问题
一:Servlet生命周期图,以及注意事项 二:代码演示 LifeCycleServlet.java package cn.woo.servlet; import java.io.IOExceptio ...
- 【mysql】mysql 配置
安装完mysql后, 要及得配置一下 /etc/mysql/my.cnf 配置字符编码为utf8 [client] default-character-set = utf8 [mysqld] defa ...
- redis空间键详解
前言 redis的空间键通知是在2.8.0版本以后加入的,客户端通过发布订阅的方式,订阅某个频道,接收通过某种方式影响redis中数据的事件. 目录: 1.空间键事件分类 2.如何启用redis的空间 ...
- 父类和子类以及super关键字
super和this关键字的特点类似:super代表的是父类对象的引用. 当子父类的成员出现同名时,可以通过super来进行区分. 子类的构造方法中,通过super关键字调用父类的构造方法. publ ...
- 重置 file input
有时用户上传相同附件时也需要触发input[type='file']的change事件,除了将form重置外,还可以将input的value设为空 <input type="file& ...