首页 技术 正文
技术 2022年11月7日
0 收藏 747 点赞 1,097 浏览 4863 个字

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 DescriptionNow 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.

 InputThe 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.

 OutputFor each test case, print the minimal steps in one line. Sample Input2 12342144 11119999 Sample Output24 AuthorYE, 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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902