[HUD 1195] Open the Lock
Open the Lock
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
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.
1234
2144
1111
9999
4
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 10000 struct Node{
int n,t;
Node(){}
Node(int _n,int _t):n(_n),t(_t){}
};
struct Vis{
int d,t;
Vis(){}
Vis(int _d,int _t):d(_d),t(_t){}
}; int s,e;
Vis vis[N]; inline void getd(int n,int *a)
{
int k=;
while(n){
a[k++]=n%;
n/=;
}
}
inline int getn(int *a)
{
int n=;
for(int i=;i>=;i--){
n=n*+a[i];
}
return n;
}
int bfs()
{
int sp=; //层数控制
int t1[],t2[];
Node now,next;
memset(vis,,sizeof(vis));
queue<Node> p,q;
p.push(Node(s,));
q.push(Node(e,));
vis[s]=Vis(,);
vis[e]=Vis(,);
while(!p.empty() && !q.empty()){
while(p.front().t==sp){
Node now=p.front();
p.pop();
//加减1
getd(now.n,t1);
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
if(i<) t2[i]=t1[i]+>?:t1[i]+;
else t2[i-]=t1[i-]-<?:t1[i-]-;
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
p.push(next);
}
//交换相邻
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+]);
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
p.push(next);
}
}
while(q.front().t==sp){
Node now=q.front();
q.pop();
//加减1
getd(now.n,t1);
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
if(i<) t2[i]=t1[i]+>?:t1[i]+;
else t2[i-]=t1[i-]-<?:t1[i-]-;
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
q.push(next);
}
//交换相邻
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+]);
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
q.push(next);
}
}
sp++;
}
return -;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&s,&e);
printf("%d\n",bfs());
}
return ;
}
[HUD 1195] Open the Lock的更多相关文章
- hdu 1195 Open the Lock
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1195 Open the Lock (BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
- hdu 1195 Open the Lock(广搜,简单)
题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...
- HDU 1195 Open the Lock (双宽搜索)
意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- BFS && DFS
HDOJ 1312 Red and Black http://acm.hdu.edu.cn/showproblem.php?pid=1312 很裸的dfs,在dfs里面写上ans++,能到几个点就调了 ...
- CREATE A ENERGY / HEALTH BAR HUD
Now then, let's get started. 1. Open the Play scene which you had created in the previous post. If y ...
随机推荐
- tomcat内存溢出,设置
tomcat/bin/catalina.bat里找到echo Using CATALINA_BASE: "%CATALINA_BASE%" ,在上方设置: set JAV ...
- Unity3D 游戏开发构架篇 ——角色类的设计与持久化
在游戏开发中,游戏角色占了很大的篇幅,可以说游戏中所有的内容都是由主角所带动.这里就介绍一下角色类的设计和持久化. 一.角色类应用场景和设计思想 游戏中的角色类型不一而足,有不同的技能,有不同的属性等 ...
- CentOS下使用cmake编译安装mysql
一.下载安装所必需的依赖包 1.因为高版本mysql都用cmake安装,所以下载cmake wget http://www.cmake.org/files/v3.0/cmake-3.0.1.tar.g ...
- cf div2 234 E
E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...
- php数组函数序列之array_unshift() 在数组开头插入一个或多个元素
array_unshift() 函数在数组开头插入一个或多个元素.被加上的元素作为一个整体添加,这些元素在数组中的顺序和在参数中的顺序一样 array_unshift()定义和用法 array_uns ...
- 在AngularJS中学习javascript的new function意义及this作用域的生成过程
慢慢入门吧,不着急. 至少知道了controller和service的分工. new function时,隐含有用this指向function的prototype之意. 这样,两个JAVASCRIPT ...
- [Hibernate]dynamic-insert和dynamic-update属性
这二个属性默认情况均为false,你可以通过以下二种方式进行配置使用: 1.Annotation @Entity @Table(name = "stock_transaction" ...
- Use windows batch script to create menu
Background Recently, I find that we need to type some very long gradle commands to run build, chec ...
- *[hackerrank]Consecutive Subsequences
https://www.hackerrank.com/contests/w6/challenges/consecutive-subsequences 求数组中被k整除的子段和有几个.这个要利用sum[ ...
- Wireshark抓包分析HTTPS与HTTP报文的差异
一.什么是HTTPS: HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议 它是一个安全通信通道,它基于HTTP开发,用于在客户计算机和服务器之间交换 ...