#include <stdio.h>
struct  node{int a[4];int step;
}first,last;node num[10000]={0};
int d[10][10][10][10]={0};
int tail=0,head=0;
int g[10];
int h=0;
void en(node E)             //先进栈
{num[tail++]=E;
}node jm()
{return num[head++];   //在出栈
}void fun()
{first.step=0;node source={first.a[0],first.a[1],first.a[2],first.a[3],first.step};d[first.a[0]][first.a[1]][first.a[2]][first.a[3]]=1;en(source);                           //先把数字存入数组中node cur=first;node ns={};while(head<tail){cur=jm();if(cur.a[0]==last.a[0]&&cur.a[1]==last.a[1]&&cur.a[2]==last.a[2]&&cur.a[3]==last.a[3]){g[h++]=cur.step;break;}for(int i=0;i<4;i++){ns=cur;ns.a[i]++;if(ns.a[i]==10)ns.a[i]=1;if(!d[ns.a[0]][ns.a[1]][ns.a[2]][ns.a[3]]){d[ns.a[0]][ns.a[1]][ns.a[2]][ns.a[3]]=1;ns.step++;en(ns);}}for(int i=0;i<4;i++){ns=cur;ns.a[i]--;if(ns.a[i]==0)ns.a[i]=9;if(!d[ns.a[0]][ns.a[1]][ns.a[2]][ns.a[3]]){d[ns.a[0]][ns.a[1]][ns.a[2]][ns.a[3]]=1;ns.step++;en(ns);}}for(int i=0;i<4;i++){ns=cur;int l;l=ns.a[i];ns.a[i]=ns.a[i+1];ns.a[i+1]=l;if(!d[ns.a[0]][ns.a[1]][ns.a[2]][ns.a[3]]){d[ns.a[0]][ns.a[1]][ns.a[2]][ns.a[3]]=1;ns.step++;en(ns);}}}
}
void main()
{char c1[10],c2[10];int t;scanf("%d",&t);while(t--){scanf("%s %s",c1,c2);for(int i=0;i<4;i++){first.a[i]=c1[i]-'0';last.a[i]=c2[i]-'0';}fun();}for(int m=0;m<h;m++){printf("%d\n",g[m]);}}

Problem Description
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 digit.
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

转载于:https://www.cnblogs.com/liunx1109/p/6134067.html

1111---9999的变换相关推荐

  1. Android支付接入(五):机锋网

    前边已经陆续跟大家走了一遍运营商和支付宝付费接入,今天跟大家一起看看机锋网的支付接入.事实上付费接入本身并没有太多须要注意的地方,做的多了以后你会发现套路都是大同小异的.而须要注意的地方在于怎么跟游戏 ...

  2. 仿百度GIF验证码 GIFEncoder 跳动验证码 随机背景色、颜色、字体、子大小、偏移、干扰线等...

    为什么80%的码农都做不了架构师?>>>    <?php session_start ();$SEESIN ['captcha'] = mt_rand ( 1111, 999 ...

  3. hrbust 1616 密码锁(广搜)

    分析:广搜,每个四位数作为一个状态,从每个状态扩展出其他的几种状态并累加步数之后加入队列. 1 #include <stdio.h> 2 #include <string.h> ...

  4. rpython求随机数代码4段5个字符_如何创建一个4位数的随机数并将其存储为变量...

    我正在Python2.7.10中开发一个4位数的猜谜游戏.但我找不到如何随机生成一个4位数的数字,并将其存储为变量.数字必须介于1111和9999之间 困难的是,我想把它作为变量存储,而不是打印出来让 ...

  5. Java 洛谷 P1909 买铅笔讲解

    题目描述 P老师需要去商店买n支铅笔作为小朋友们参加NOIP的礼物.她发现商店一共有 33种包装的铅笔,不同包装内的铅笔数量有可能不同,价格也有可能不同.为了公平起 见,P老师决定只买同一种包装的铅笔 ...

  6. 买铅笔(洛谷-P1909)

    题目描述 P老师需要去商店买n支铅笔作为小朋友们参加NOIP的礼物.她发现商店一共有 3种包装的铅笔,不同包装内的铅笔数量有可能不同,价格也有可能不同.为了公平起 见,P老师决定只买同一种包装的铅笔. ...

  7. NOIP2016普及组第一题:买铅笔

    [题目描述] P老师需要去商店买n支铅笔作为小朋友们参加NOIP的礼物.她发现商店一共有 3种包装的铅笔,不同包装内的铅笔数量有可能不同,价格也有可能不同.为了公平起 见,P老师决定只买同一种包装的铅 ...

  8. thinkphp3.2+cropper上传多张图片剪切图片

    实现效果截图 点加号可以继续上传第二张图片 代码部<--引入cropper相关文件--> <link rel="stylesheet" href="/h ...

  9. php验证码雪花源码,php如何生成雪花背景验证码(附代码)

    本文章来给大家介绍php生成雪花背景验证码程序代码 ,有需要的朋友可进入参考参考. 验证码生成程序<?php session_start(); session_register("lo ...

最新文章

  1. c语言指针尚观嵌入式,C语言-指针
  2. 简明python教程 豆瓣-Python 有哪些入门学习方法和值得推荐的经典教材?
  3. 洛谷 P1034 矩形覆盖
  4. 树莓派 mysql集群_多树莓派集群服务器
  5. 借力阿里云数据中台,日播集团“数”识消费者
  6. Spring入门 IOC
  7. 【强化学习】A3C原理
  8. JavaSE第十五天20160823
  9. mybatis不等于某个字符串的值_MyBatis 的 4 个妙用,别再踩坑了!
  10. 【JVM】javap命令行分析揭示boolean的本质的虚拟机指令
  11. STM32工作笔记0090---ADC基本原理-M3
  12. 我的第一次--我与51CTO的故事
  13. 使用Sharepoint Services 3.0构建基本网站
  14. 全国2009年1月电子商务与电子政务试题
  15. python画正弦函数_python 图像处理画一个正弦函数代码实例
  16. 软件项目管理 --SVN目录结构
  17. 突破SBO屏蔽限制,自由实现快捷键自定义
  18. 自动控制原理(1)-典型环节的传递函数
  19. python蓝桥杯准备
  20. cut out数据增强_谷歌大脑提出自动数据增强方法AutoAugment:可迁移至不同数据集...

热门文章

  1. FFMPEG结构体分析:AVFrame
  2. 图解FFMPEG打开媒体的函数avformat_open_input
  3. python硬件测试开发工程师_硬件测试工程师
  4. 亚马逊出的平板电脑_亚马逊发布Fire HD 8新系列平板电脑,90美元起
  5. mysql批量写入redis_如何高效地向Redis插入大量的数据(推荐)
  6. android开发根据分辨率设置高度,【android】根据屏幕分辨率设置底栏高度
  7. linux 挂起后无法唤醒屏幕
  8. idea插件sonar字体背景颜色修改
  9. 【NOIP2015】【Luogu2669】金币(模拟)
  10. 【Luogu3932】浮游大陆的68号岛(前缀和)