解体心得:

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(一个数的逐位变化问题的搜索)的更多相关文章

  1. IP地址与子网掩码逐位相与

    逐位相与说的其实就是子网掩码与网络地址相同位置的数字相加,当和为2的时候该位置写作1,否则的话写作0

  2. Js 分别取一个数的百位,十位,个位

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  3. 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 ...

  4. 删除一个数的K位使原数变得最小

    原创 给定一个n位正整数a, 去掉其中k个数字后按原左右次序将组成一个新的正整数.对给定的a, k寻找一种方案,使得剩下的数字组成的新数最小. 提示:应用贪心算法设计求解 操作对象为n位正整数,有可能 ...

  5. java实现串逐位和(C++)

    给定一个由数字组成的字符串,我们希望得到它的各个数位的和. 比如:"368" 的诸位和是:17 这本来很容易,但为了充分发挥计算机多核的优势,小明设计了如下的方案: int f(c ...

  6. 深入理解KMP算法

    前言:本人最近在看<大话数据结构>字符串模式匹配算法的内容,但是看得很迷糊,这本书中这块的内容感觉基本是严蔚敏<数据结构>的一个翻版,此书中给出的代码实现确实非常精炼,但是个人 ...

  7. IM通信协议逆向分析、Wireshark自定义数据包格式解析插件编程学习

    相关学习资料 http://hi.baidu.com/hucyuansheng/item/bf2bfddefd1ee70ad68ed04d http://en.wikipedia.org/wiki/I ...

  8. 数据结构(复习)---------字符串-----KMP算法(转载)

    字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...

  9. PC/FORTH 判定

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

随机推荐

  1. android下的异步任务

    异步任务一般用在加载一些网络资源的时候用,主要的实现方法是新建一个类来继承AsyncTask这个父类,然后复写该类下面的一些方法,其中doInBackground方法是必须要的,下面看代码 packa ...

  2. windows下使用VM虚拟机安装linux

    转载地址:http://blog.csdn.net/u013142781/article/details/50529030 安装过程中发现与下面的顺序有点不同,遇到的问题是: 在选择中文进行安装时,一 ...

  3. Consider everything deeply but still remain fearless.

    Consider everything deeply but still remain fearless.愿你能深思熟虑,但始终无所畏惧.

  4. fancyBox高级进阶用法

    最近给客户做的一个项目中,客户要求弹窗的边界与页面某个区块边界平齐,但平齐之后,弹出的窗口就不是居中的情况了,研究之后,认为需要改写fancyBox的fancybox-wrap类中的top属性才能达到 ...

  5. 数据类型 -- uint32_t 类型

    整型的每一种都有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如果需声明无符号类型 ...

  6. nginx对不存在的文件进行404处理

    location / { try_files $uri $uri/ /?$args 404; } location / { try_files $uri $uri/ /index.html 404; ...

  7. COGS 2769. mk去撸串

    [题目描述] 今天 mk 去撸串 ,恰逢店里活动 ,如果吃一种串串超过记录, 可以 赠送 328, 所以 mk 想知道他吃的串串中吃的最多的种类是什么. [输入格式] 第一行一个整数 1<=n& ...

  8. 域名设置A记录或CNAME记录,但无法被解析,可能是因为状态为:clientHold

    解决方案: 访问https://whois.aliyun.com/查询域名状态是否为“注册商禁止解析”: 若是,联系注册商根据对方要求进行操作以便解除. https://icann.org/epp#c ...

  9. linux 命令——33 df(转)

    linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...

  10. IOS 设置子控件的frame(layoutSubviews and awakeFromNib)

      如果控件是通过xib或者storyboard创建出来的就会调用该方法 - (void)awakeFromNib :该方法只会调用一次 // 如果控件是通过xib或者storyboard创建出来的就 ...