【BFS/DBFS】Open the Lock【双向广搜】

题目链接: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;
}



你可能感兴趣的:(【BFS/DBFS】Open the Lock【双向广搜】)