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 关键字可确保当一 ...
随机推荐
- 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper
atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...
- Zabbix iostat 监控配置
## zabbix iostat 监控模板安装与配置 配置定时任务,用于生成iostat的统计数据 crontab -e * * * * * /usr/local/zabbix327/bin/iost ...
- EMC ViPR all in one page
EMC ViPR 2.0 Product Documentation Index https://community.emc.com/docs/DOC-35557
- Maximal Rectangle leetcode java
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- Valid Parentheses leetcode java
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- 倒计时 总结 Timer Handler CountDownTimer RxJava MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- IOS之导航控制器
UINavigationController是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图.任何类型的视图控制器都可放入栈中.在设计导航控制器时需要指定根视图即用户看到的第一个视图.根视图 ...
- scala 学习笔记五 foreach, map, reduce
例子 val v = Vector(,,,) ) println(s) //输出:Vector(2, 4, 6, 8) val v2 = Vector(,,,) var v3 = v2.reduce( ...
- 网络结构设计——负载均衡之LVS学习笔记(二)
LVS按个人理解的说就是将一台Linux服务器当作路由器等功能的技术.LVS---Linux虚拟服务器. LVS实现了三种IP负载均衡技术VS/NAT.VS/TUN.VS/DR. 今天简单分享一下我在 ...
- 25个iptables常用示例
本文将给出25个iptables常用规则示例,这些例子为您提供了些基本的模板,您可以根据特定需求对其进行修改调整以达到期望. 格式 iptables [-t 表名] 选项 [链名] [条件] [-j ...