Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3400    Accepted Submission(s): 1507

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
 
Author
YE, Kai
 
Source
 
搜索....bfs  这道题很经典..
规则为 +1 ,-1,两个对调,若 9+1=1,1-1=9,同时最右边不能和最左边数字不能相邻..比如 3***4,就不行
单项搜索 
算法..

 #include<iostream>
#include<queue>
#include<set>
using namespace std;
typedef struct
{
int data;
int step;
}go;
int dir[]={,,,};
void bfs(int st,int en)
{
int i;
queue<go>mat;
set<int>visit; //用来存储是否产生这个数
go q,tem;
q.data=st;
q.step=;
mat.push(q);
visit.insert(st);
while(!mat.empty())
{
tem=mat.front();
mat.pop();
if(tem.data==en)
{
printf("%d\n",tem.step);
return ;
}
/*先进行+1oper*/
for(i= ;i<;i++)
{
q=tem;
if((q.data/dir[i])%==) q.data-=*dir[i];
else
q.data+=dir[i];
q.step++;
if(visit.find(q.data)==visit.end()) //说明该状态没有
{
visit.insert(q.data);
mat.push(q);
}
}
/*进行-1 oper*/
for(i=; i<;i++)
{
q=tem ;
if((q.data/dir[i])%==) q.data+=*dir[i];
else q.data-=dir[i];
q.step++;
if(visit.find(q.data)==visit.end()) //说明该状态没有
{
visit.insert(q.data);
mat.push(q);
}
}
/*对调旋转*/
int aa,bb;
for(i=,q=tem;i<;i++)
{
q=tem;
aa=(q.data/dir[i])%;
bb=(q.data/dir[i+])%;
q.data=(q.data+(bb-aa)*dir[i]+(aa-bb)*dir[i+]);
q.step++;
if(visit.find(q.data)==visit.end()) //说明该状态没有
{
visit.insert(q.data);
mat.push(q);
}
}
}
}; int main()
{
int st,en,t;
cin>>t;
while(t--)
{
scanf("%d%d",&st,&en);
bfs(st,en);
}
return ;
}

采用双向广度搜索..

其实所谓双向广度,就是对于两边,每一次扩展下一层,就去扫一下,看对面有没有与之匹配的,状态

代码:

 #include<cstdio>
#include<iostream>
#include<queue>
#include<set>
using namespace std; typedef struct
{
int data;
int step;
/* void clr(){
step=0; }*/
}go;
const int dir[]={,,,};
void Dbfs(int const st ,int const en)
{
go tem1,tem2,q;
tem1.data=st;
tem2.data=en;
/* tem1.clr();
tem2.clr();
*/
tem1.step=tem2.step=;
set<int>sav1,sav2;
sav1.insert(st);
sav2.insert(en);
queue<go> beg,end;
int i,a,b,cnt1,cnt2;
beg.push(tem1);
end.push(tem2);
cnt1=cnt2=;
while(!beg.empty()&&!end.empty())
{
//+1oper
while(!beg.empty()&&cnt1==beg.front().step)
{
q=beg.front();
beg.pop();
if(sav2.find(q.data)!=sav2.end())
{
//说明有交集
while(end.front().data!=q.data)
end.pop();
printf("%d\n",q.step+end.front().step);
return ;
}
for(i=;i<;i++)
{
tem1=q;
if((tem1.data/dir[i])%==) tem1.data-=*dir[i];
else
tem1.data+=dir[i];
tem1.step++;
if(sav2.find(tem1.data)!=sav2.end())
{
//说明有交集
while(end.front().data!=tem1.data)
end.pop();
printf("%d\n",tem1.step+end.front().step);
return ;
}
if(sav1.find(tem1.data)==sav1.end()) //标记
{
sav1.insert(tem1.data);
beg.push(tem1);
}
}
//-1oper
for(i=;i<;i++)
{
tem1=q;
if((tem1.data/dir[i])%==) tem1.data+=*dir[i];
else
tem1.data-=dir[i];
tem1.step++;
if(sav2.find(tem1.data)!=sav2.end())
{
//说明有交集
while(end.front().data!=tem1.data)
end.pop();
printf("%d\n",tem1.step+end.front().step);
return ;
}
if(sav1.find(tem1.data)==sav1.end()) //标记
{
sav1.insert(tem1.data);
beg.push(tem1);
}
}
//旋转
for(i=;i<;i++)
{
tem1=q;
a=(tem1.data/dir[i])%;
b=(tem1.data/dir[i+])%;
tem1.data+=(a-b)*(dir[i+]-dir[i]);
tem1.step++;
if(sav2.find(tem1.data)!=sav2.end())
{
//说明有交集
while(end.front().data!=tem1.data)
end.pop();
printf("%d\n",tem1.step+end.front().step);
return ;
}
if(sav1.find(tem1.data)==sav1.end()) //标记
{
sav1.insert(tem1.data);
beg.push(tem1);
}
}
}
cnt1++;
while(!end.empty()&&cnt2==end.front().step)
{
q=end.front();
end.pop();
//+1oper
for(i=;i<;i++)
{
tem2=q;
if((tem2.data/dir[i])%==) tem2.data-=*dir[i];
else
tem2.data+=dir[i];
tem2.step++;
if(sav1.find(tem2.data)!=sav1.end())
{
//说明有交集
while(beg.front().data!=tem2.data)
beg.pop();
printf("%d\n",tem2.step+beg.front().step);
return ;
}
if(sav2.find(tem2.data)==sav2.end()) //标记
{
sav2.insert(tem2.data);
end.push(tem2);
}
}
//-1oper
for(i=;i<;i++)
{
tem2=q;
if((tem2.data/dir[i])%==) tem2.data+=*dir[i];
else
tem2.data-=dir[i];
tem2.step++; if(sav1.find(tem2.data)!=sav1.end())
{
//说明有交集
while(beg.front().data!=tem2.data)
beg.pop();
printf("%d\n",tem2.step+beg.front().step);
return ;
}
if(sav2.find(tem2.data)==sav2.end()) //标记
{
sav2.insert(tem2.data);
end.push(tem2);
}
}
//旋转
for(i=;i<;i++)
{
tem2=q;
a=(tem2.data/dir[i])%;
b=(tem2.data/dir[i+])%;
tem2.data+=(a-b)*(dir[i+]-dir[i]);
tem2.step++;
if(sav1.find(tem2.data)!=sav1.end())
{
//说明有交集
while(beg.front().data!=tem2.data)
beg.pop();
printf("%d\n",tem2.step+beg.front().step);
return ;
}
if(sav2.find(tem2.data)==sav2.end()) //标记
{
sav2.insert(tem2.data);
end.push(tem2);
}
}
}
cnt2++;
}
} int main()
{
int st,en,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&st,&en);
Dbfs( st , en );
}
return ;
}

HDUOJ---1195Open the Lock的更多相关文章

  1. hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...

  2. HDU——1195Open the Lock(双向BFS)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent

    看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...

  4. 多线程同步工具——Lock

    本文原创,转载请注明出处. 参考文章: <"JUC锁"03之 公平锁(一)> <"JUC锁"03之 公平锁(二)> 锁分独占锁与共享锁, ...

  5. java 线程 Lock 锁使用Condition实现线程的等待(await)与通知(signal)

    一.Condition 类 在前面我们学习与synchronized锁配合的线程等待(Object.wait)与线程通知(Object.notify),那么对于JDK1.5 的 java.util.c ...

  6. InnoDB:Lock & Transaction

    InnoDB 是一个支持事务的Engine,要保证事务ACID,必然会用到Lock.就像在Java编程一下,要保证数据的线程安全性,必然会用到Lock.了解Lock,Transaction可以帮助sq ...

  7. 使用四元数解决万向节锁(Gimbal Lock)问题

    问题 使用四元数可以解决万向节锁的问题,但是我在实际使用中出现问题:我设计了一个程序,显示一个三维物体,用户可以输入绕zyx三个轴进行旋转的指令,物体进行相应的转动. 由于用户输入的是绕三个轴旋转的角 ...

  8. 万向节锁(Gimbal Lock)的理解

    [TOC] 结论 我直接抛出结论: Gimbal Lock 产生的原因不是欧拉角也不是旋转顺序,而是我們的思维方式和程序的执行逻辑没有对应,也就是说是我们的观念导致这个情况的发生. 他人解释 首先我们 ...

  9. 在多线程编程中lock(string){...}隐藏的机关

    常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...

  10. 谈谈 Lock

    上来先看MSDN关于lock的叙述: lock  关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁.  下面的示例包含一个 lock 语句. lock  关键字可确保当一 ...

随机推荐

  1. OpenCV学习(36) 人脸识别(1)

    本文主要参考OpenCV人脸识别教程:http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html 1.OpenCV ...

  2. C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数

    在编程中经常需要用到数字与字符串的转换,下面就总结一下. 1.atoi() C/C++标准库函数,用于字符串到整数的转换. 函数原型:int atoi (const char * str); #inc ...

  3. 【计算机网络】详解网络层(二)ARP和RARP

    ARP ARP(Address Resolution Protocol,地址解析协议)是将IP地址解析为以太网MAC地址(物理地址)的协议.在局域网中,当主机或其他网络设备有数据要发送给另一个主机或设 ...

  4. Android中Intent的显示和隐式使用

    Android应用程序中组件之间的通信都少不了Intent的使用,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件, ...

  5. Adapter 适配器模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. spark streaming的理解和应用

    1.Spark Streaming简介 官方网站解释:http://spark.apache.org/docs/latest/streaming-programming-guide.html 该博客转 ...

  7. mybatis的mapper返回map结果集(springboot)

    通过MapKey指定map的key值 @MapKey("id") Map<Long, UserInfo> getUserInfoMap(); @MapKey(" ...

  8. 消息队列的使用场景(转载c)

    作者:ScienJus链接:https://www.zhihu.com/question/34243607/answer/58314162来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  9. oracle错误处理之ORA-00054:资源正忙,要求指定NOWAIT

    查询所有会话 select t2.username, t2.sid, t2.serial#, t2.logon_time from v$locked_object t1, v$session t2 w ...

  10. Java从零开始学二十九(大数操作(BigIntger、BigDecimal)

    一.BigInteger 如果在操作的时候一个整型数据已经超过了整数的最大类型长度long的话,则此数据就无法装入,所以,此时要使用BigInteger类进行操作. 不可变的任意精度的整数.所有操作中 ...