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整合Struts2  注解版
			
1.jar包 <!--spring配置--> <dependency> <groupId>org.springframework</groupId> & ...
 - 字符串json转成json对象
			
方法1 JsonSerializer serializer = new JsonSerializer(); TextReader tr = new StringReader(text); JsonTe ...
 - 消息中间件之MQ详解及四大MQ比较
			
一.消息中间件相关知识 1.概述 消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流 ...
 - 《Head First 设计模式》之策略模式——鸭子行为
			
策略模式(Strategy Pattern) ——定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. (每个功能的多种实现成为一个算法族,这些算法族被分别封装 ...
 - css高度已知,左右定宽,中间自适应三栏布局
			
css高度已知,左右定宽,中间自适应三栏布局: <!DOCTYPE html> <html lang="en"> <head> <meta ...
 - 使用C#版OpenCV进行圆心求取
			
OpenCVSharp是OpenCV的.NET wrapper,是一名日本工程师开发的,项目地址为:https://github.com/shimat/opencvsharp. 该源码是 BSD开放协 ...
 - sk-learning(2)
			
sk-learning 学习(2) sklearing 训练评估 针对kdd99数据集使用逻辑回归分类训练 然后进行评估 发觉分数有点高的离谱 取出10%数据494021条,并从中选择四分之一作为测试 ...
 - 【洛谷】CYJian的水题大赛 解题报告
			
点此进入比赛 \(T1\):八百标兵奔北坡 这应该是一道较水的送分题吧. 理论上来说,正解应该是DP.但是,.前缀和优化暴力就能过. 放上我比赛时打的暴力代码吧(\(hl666\)大佬说这种做法的均摊 ...
 - vuejs属性绑定和双向绑定
			
属性绑定 html <div v-bind:title="title">hello world</div> js new Vue({ el:'#root', ...
 - fread, fwrite - 二进制流的输入/输出
			
总览 (SYNOPSIS) #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stre ...