【BFS/DBFS】Open the Lock【双向广搜】
2016-05-03 15:40
363 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195
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
BFS代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
string s1,s2;
int dir[2]={1,-1};
struct node{
string s;
int step;
};
void bfs()
{
queue<node>q;
map<string,int>Map;
Map.clear();
node st;
st.s=s1;
st.step=0;
Map[s1]=1;
q.push(st);
while(!q.empty()){
st=q.front();
q.pop();
if(st.s==s2){
cout<<st.step<<endl;
return ;
}
for(int j=0;j<4;j++){ // 四个方向
if(st.s[j]!=s2[j]){ // 不相等的时候才+1,-1
for(int i=0;i<2;i++){
node next=st;
next.s[j]=(next.s[j]-'0'+dir[i])%10+'0';
if(next.s[j]=='0'&&i) next.s[j]='9';
else if(next.s[j]=='0'&&i==0) next.s[j]='1';
if(!Map[next.s]){
Map[next.s]=1;
next.step+=1;
q.push(next);
}
}
}
}
// 相邻位置交换;
for(int i=0;i<3;i++){
node next=st;
swap(next.s[i],next.s[i+1]);
if(!Map[next.s]){
Map[next.s]=1;
next.step+=1;
q.push(next);
}
}
}
return;
}
int main()
{
int t;
cin>>t;
while(t--){
cin>>s1>>s2;
bfs();
}
return 0;
}
DBFS代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
using namespace std;
string s1,s2;
int ans=-1;
int dir[2]={1,-1};
struct node
{
string s;
int step;
}st;
queue<node>q[2];
map<string,int>Map[2];
void bfs(int k){
if(ans!=-1||q[k].empty()) return;
int stp=q[k].front().step;
while(!q[k].empty()&&stp==q[k].front().step){ // 两向一边一层搜;
st=q[k].front();
q[k].pop();
if(Map[1^k][st.s]){
ans=st.step+Map[1^k][st.s];
//cout<<k<<' '<<st.step<<' '<<Map[1^k][st.s]<<' '<<st.s<<endl;
return;
}
for(int j=0;j<4;j++){
for(int i=0;i<2;i++){
node next=st;
next.s[j]=(next.s[j]-'0'+dir[i])%10+'0';
if(next.s[j]=='0'&&i) next.s[j]='9';
else if(next.s[j]=='0'&&i==0) next.s[j]='1';
if(!Map[k][next.s]){
next.step+=1;
Map[k][next.s]=next.step;
q[k].push(next);
}
}
}
// 相邻位置交换;
for(int i=0;i<3;i++){
node next=st;
swap(next.s[i],next.s[i+1]);
if(!Map[k][next.s]){
next.step+=1;
Map[k][next.s]=next.step;
q[k].push(next);
}
}
}
}
void dbfs(){
while(ans==-1){ // 直到找到相交点为止
bfs(0);
bfs(1);
}
}
void init()
{
ans=-1;
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
Map[0].clear();
Map[1].clear();
}
int main()
{
int t;
cin.sync_with_stdio(false);
cin>>t;
while(t--){
init();
cin>>s1>>s2;
st.s=s1,st.step=0;
Map[0][s1]=0;
q[0].push(st); // 正向
st.s=s2,st.step=0;
Map[1][s2]=0;
q[1].push(st); // 反向
dbfs();
cout<<ans<<endl;
}
return 0;
}
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
BFS代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
string s1,s2;
int dir[2]={1,-1};
struct node{
string s;
int step;
};
void bfs()
{
queue<node>q;
map<string,int>Map;
Map.clear();
node st;
st.s=s1;
st.step=0;
Map[s1]=1;
q.push(st);
while(!q.empty()){
st=q.front();
q.pop();
if(st.s==s2){
cout<<st.step<<endl;
return ;
}
for(int j=0;j<4;j++){ // 四个方向
if(st.s[j]!=s2[j]){ // 不相等的时候才+1,-1
for(int i=0;i<2;i++){
node next=st;
next.s[j]=(next.s[j]-'0'+dir[i])%10+'0';
if(next.s[j]=='0'&&i) next.s[j]='9';
else if(next.s[j]=='0'&&i==0) next.s[j]='1';
if(!Map[next.s]){
Map[next.s]=1;
next.step+=1;
q.push(next);
}
}
}
}
// 相邻位置交换;
for(int i=0;i<3;i++){
node next=st;
swap(next.s[i],next.s[i+1]);
if(!Map[next.s]){
Map[next.s]=1;
next.step+=1;
q.push(next);
}
}
}
return;
}
int main()
{
int t;
cin>>t;
while(t--){
cin>>s1>>s2;
bfs();
}
return 0;
}
DBFS代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
using namespace std;
string s1,s2;
int ans=-1;
int dir[2]={1,-1};
struct node
{
string s;
int step;
}st;
queue<node>q[2];
map<string,int>Map[2];
void bfs(int k){
if(ans!=-1||q[k].empty()) return;
int stp=q[k].front().step;
while(!q[k].empty()&&stp==q[k].front().step){ // 两向一边一层搜;
st=q[k].front();
q[k].pop();
if(Map[1^k][st.s]){
ans=st.step+Map[1^k][st.s];
//cout<<k<<' '<<st.step<<' '<<Map[1^k][st.s]<<' '<<st.s<<endl;
return;
}
for(int j=0;j<4;j++){
for(int i=0;i<2;i++){
node next=st;
next.s[j]=(next.s[j]-'0'+dir[i])%10+'0';
if(next.s[j]=='0'&&i) next.s[j]='9';
else if(next.s[j]=='0'&&i==0) next.s[j]='1';
if(!Map[k][next.s]){
next.step+=1;
Map[k][next.s]=next.step;
q[k].push(next);
}
}
}
// 相邻位置交换;
for(int i=0;i<3;i++){
node next=st;
swap(next.s[i],next.s[i+1]);
if(!Map[k][next.s]){
next.step+=1;
Map[k][next.s]=next.step;
q[k].push(next);
}
}
}
}
void dbfs(){
while(ans==-1){ // 直到找到相交点为止
bfs(0);
bfs(1);
}
}
void init()
{
ans=-1;
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
Map[0].clear();
Map[1].clear();
}
int main()
{
int t;
cin.sync_with_stdio(false);
cin>>t;
while(t--){
init();
cin>>s1>>s2;
st.s=s1,st.step=0;
Map[0][s1]=0;
q[0].push(st); // 正向
st.s=s2,st.step=0;
Map[1][s2]=0;
q[1].push(st); // 反向
dbfs();
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- linux 系统服务
- linux 系统无法启动的基本解决方法
- Linux 简介
- 十大OpenGL教程
- Shell典型应用Nginx之应用日志分析
- 7款应用最广泛的Linux桌面环境盘点
- 利用de Casteljau算法绘制Bezier曲面
- Tomcat中文乱码
- 图文详解linux如何搭建lamp服务环境
- openTLD算法在opencv3版本下运行所需的修改
- XSHELL 5 退格键不能用,删除会有乱码,按截图设置
- Linux sgid功能
- JVM性能监控与故障处理工具 5/3/2016
- Recover after DROP TABLE, innodb_file_per_table is ON
- Recover after DROP TABLE, innodb_file_per_table is OFF
- awstats + tomcat + windows
- linux C++多线程操作文件&输出加锁
- linux C++多线程操作文件&输出加锁
- 目录栈 pushd / popd
- 模仿linux中string命令的实现,实现从文件中提取连续4个以上的可打印字符