HDUOJ---1195Open the Lock
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
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.
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.
#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的更多相关文章
- 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 ...
- HDU——1195Open the Lock(双向BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent
看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...
- 多线程同步工具——Lock
本文原创,转载请注明出处. 参考文章: <"JUC锁"03之 公平锁(一)> <"JUC锁"03之 公平锁(二)> 锁分独占锁与共享锁, ...
- java 线程 Lock 锁使用Condition实现线程的等待(await)与通知(signal)
一.Condition 类 在前面我们学习与synchronized锁配合的线程等待(Object.wait)与线程通知(Object.notify),那么对于JDK1.5 的 java.util.c ...
- InnoDB:Lock & Transaction
InnoDB 是一个支持事务的Engine,要保证事务ACID,必然会用到Lock.就像在Java编程一下,要保证数据的线程安全性,必然会用到Lock.了解Lock,Transaction可以帮助sq ...
- 使用四元数解决万向节锁(Gimbal Lock)问题
问题 使用四元数可以解决万向节锁的问题,但是我在实际使用中出现问题:我设计了一个程序,显示一个三维物体,用户可以输入绕zyx三个轴进行旋转的指令,物体进行相应的转动. 由于用户输入的是绕三个轴旋转的角 ...
- 万向节锁(Gimbal Lock)的理解
[TOC] 结论 我直接抛出结论: Gimbal Lock 产生的原因不是欧拉角也不是旋转顺序,而是我們的思维方式和程序的执行逻辑没有对应,也就是说是我们的观念导致这个情况的发生. 他人解释 首先我们 ...
- 在多线程编程中lock(string){...}隐藏的机关
常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...
- 谈谈 Lock
上来先看MSDN关于lock的叙述: lock 关键字将语句块标记为临界区,方法是获取给定对象的互斥锁,执行语句,然后释放该锁. 下面的示例包含一个 lock 语句. lock 关键字可确保当一 ...
随机推荐
- Android图片加载框架最全解析(八),带你全面了解Glide 4的用法
本篇将是我们这个Glide系列的最后一篇文章. 其实在写这个系列第一篇文章的时候,Glide就推出4.0.0的RC版了.那个时候因为我一直研究的都是Glide 3.7.0版本,再加上RC版本还不太稳定 ...
- Rational Rose、PowerDesign、Visio的一些比较
就目前建模软件来说,Rational Rose.PowerDesign.Visio三个是比较常用的系列了,在这里对它们做一些比较,我只用过PowerDesign.Visio和一个跟Rose很像的免费工 ...
- 如何利用启明星Portal门户系统的Page模块构建工作流表单
启明星门户网站的Pages模块支持构建自定义表单系统.这使得对于使用表单收集用户数据的需求来说非常有用. 本文介绍如何构建一个简单的“出差系统”. 1.在页面里增加Pages模块,建立人事部部门,然后 ...
- 【BZOJ】【3612】【HEOI 2014】平衡
DP 唉我还是too naive 这是个整数划分题…… 我想的DP方式是f[i][j][k]表示前 i 个数拼出 j 用了 k 个数的方案数…… 转移当然是比较直观…… 但是只能得30分QAQ 正确的 ...
- UI_UITabBarController
建立控制器 // 普通控制器 GroupViewController *groupVC = [[GroupViewController alloc] init]; SecondViewControll ...
- OpenCV学习(36) 人脸识别(1)
本文主要参考OpenCV人脸识别教程:http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html 1.OpenCV ...
- NLP 依存分析
NLP 依存分析 https://blog.csdn.net/sinat_33741547/article/details/79258045
- 第十四章 Executors源码解析
前边两章介绍了基础线程池ThreadPoolExecutor的使用方式.工作机理.参数详细介绍以及核心源码解析. 具体的介绍请参照: 第十二章 ThreadPoolExecutor使用与工作机理 第十 ...
- scala 学习笔记三 闭包
闭包是一个函数,返回值依赖于声明在函数外部的一个或多个变量. 闭包通常来讲可以简单的认为是可以访问一个函数里面局部变量的另外一个函数. 如下面这段匿名的函数: val multiplier = (i: ...
- GO语言基础之并发concurrency
并发Concurrency 很多人都是冲着 Go 大肆宣扬的高并发而忍不住跃跃欲试,但其实从源码的解析来看,goroutine 只是由官方实现的超级“线程池”而已.不过话说回来,每个实例 4-5KB的 ...