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.
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 ;
}