HDU 1195 Open the Lock(bfs or 双向bfs) - dblank

HDU 1195 Open the Lock(bfs or 双向bfs)

Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5613    Accepted Submission(s): 2492

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

 

Author
YE, Kai

 

Source
题意:给你一个4位数的密码锁,每次可以滑动一位上面的数字,或者交换相邻位置的数,问到达目标数字的最小步数,直接bfs可以过,不过的直接bfs耗时400+ms,而双向bfs只要0ms,可见双向bfs对搜索的优化。
直接bfs:
#include<cstdio>  
#include<algorithm>  
#include<iostream>  
#include<cstring>  
#include<cmath>  
#include<queue>  
  
using namespace std;  
  
char a[6], b[6];  
int vis[10010];  
  
struct P  
{  
    char x[6];  
    int t;  
};  
  
void bfs()  
{  
    int z;  
    queue<struct P>q;  
    struct P in , out;  
    strcpy(in.x, a);  
    in.t = 0;  
    q.push(in);  
    while(!q.empty())  
    {  
        in = q.front();  
        q.pop();  
        if(!strcmp(in.x, b))  
        {  
            printf("%d\n", in.t);  
            return ;  
        }  
        for(int i=0; i<3; i++)  
        {  
            if(in.x[i] != in.x[i+1])  
            {  
                out = in;  
                swap(out.x[i], out.x[i+1]);  
                out.t++;  
                sscanf(out.x, "%d", &z);  
                if(!vis[z])  
                {  
                    vis[z] = 1;  
                    q.push(out);  
                }  
            }  
        }  
        for(int i=0; i<4; i++)  
        {  
            out = in;  
            if(out.x[i] + 1 > '9')  
                out.x[i] = '1';  
            else  
                out.x[i]++;  
            out.t ++;  
            sscanf(out.x, "%d", &z);  
            if(!vis[z])  
            {  
                vis[z] = 1;  
                q.push(out);  
            }  
        }  
        for(int i=0; i<4; i++)  
        {  
            out = in;  
            if(out.x[i] - 1 < '1')  
                out.x[i] = '9';  
            else  
                out.x[i]--;  
            out.t ++;  
            sscanf(out.x, "%d", &z);  
            if(!vis[z])  
            {  
                vis[z] = 1;  
                q.push(out);  
            }  
        }  
    }  
}  
int main()  
{  
    int T;  
    while(scanf("%d", &T)==1)  
    {  
        while(T--)  
        {  
            memset(vis, 0, sizeof(vis));  
            scanf("%s%s", a, b);  
            bfs();  
        }  
    }  
    return 0;  
}

双向bfs(写的太长了,细节没处理好)

#include<iostream>  
#include<cstdio>  
#include<algorithm>  
#include<map>  
#include<cstring>  
#include<map>  
#include<set>  
#include<queue>  
#include<stack>  
#include<cmath>  
using namespace std;  
const double eps = 1e-10;  
const int inf = 0x3f3f3f3f, N = 6e4;  
typedef long long ll;  
int step[20000], vis[20000], s,e;  
int change(int *f)  
{  
    int num = 0;  
    for(int i = 3; i>=0; i--)  
        num = num*10 + f[i];  
    return num;  
}  
int d_bfs()  
{  
    queue<int>q[2];  
    int x[4], y;  
    q[0].push(s);  
    q[1].push(e);  
    while(!q[0].empty() || !q[1].empty())  
    {  
        int num = q[0].front(), ber = q[1].front();  
        q[0].pop(), q[1].pop();  
        int temp = num, tmp = ber;  
        for(int i = 0; i<4; i++)  
        {  
            x[i] = num % 10;  
            num /= 10;  
        }  
        for(int i = 0; i<4; i++)  
        {  
            int te = x[i];  
            x[i]++;  
            if(x[i]>9) x[i] = 1;  
            y = change(x);  
            if(!vis[y])  
            {  
                step[y] = step[temp]+1;  
                q[0].push(y);  
                vis[y] = 1;  
            }  
            else if(vis[y] == 2)  
                return step[y] + step[temp];  
            x[i] = te;  
            x[i]--;  
            if(x[i]<1) x[i] = 9;  
            y = change(x);  
            if(!vis[y])  
            {  
                step[y] = step[temp] + 1;  
                q[0].push(y);  
                vis[y] = 1;  
            }  
            else if(vis[y] == 2)  
                return step[y] + step[temp];  
            x[i] = te;  
        }  
        for(int j = 0; j<3; j++)  
        {  
            swap(x[j], x[j+1]);  
            y = change(x);  
            if(!vis[y])  
            {  
                step[y] = step[temp] + 1;  
                q[0].push(y);  
                vis[y] = 1;  
            }  
            else if(vis[y] == 2)  
                return step[y] + step[temp];  
            swap(x[j], x[j+1]);  
        }  
        for(int i = 0; i<4; i++)  
        {  
            x[i] = ber % 10;  
            ber /= 10;  
        }  
        for(int i = 0; i<4; i++)  
        {  
            int te = x[i];  
            x[i]++;  
            if(x[i]>9) x[i] = 1;  
            int y = change(x);  
            if(!vis[y])  
            {  
                step[y] = step[tmp] + 1;  
                q[1].push(y);  
                vis[y] = 2;  
            }  
            else if(vis[y] == 1)  
                return step[y] + step[tmp];  
            x[i] = te;  
            x[i]--;  
            if(x[i]<1) x[i] = 9;  
            y = change(x);  
            if(!vis[y])  
            {  
                step[y] = step[tmp] + 1;  
                q[1].push(y);  
                vis[y] = 2;  
            }  
            else if(vis[y] == 1)  
                return step[y] + step[tmp];  
            x[i] = te;  
        }  
        for(int j = 0; j<3; j++)  
        {  
            swap(x[j], x[j+1]);  
            y = change(x);  
            if(!vis[y])  
            {  
                step[y] = step[tmp] + 1;  
                q[1].push(y);  
                vis[y] = 2;  
            }  
            else if(vis[y] == 1)  
                return step[y] + step[tmp];  
            swap(x[j], x[j+1]);  
        }  
    }  
}  
int main()  
{  
    int t;  
    cin>>t;  
    while(t--)  
    {  
        scanf("%d%d", &s, &e);  
        if(s == e)  
        {  
            cout<<0<<endl;  
            continue;  
        }  
        memset(step, 0, sizeof(step));  
        memset(vis, 0, sizeof(vis));  
        vis[s] = 1;  
        vis[e] = 2;  
        int ans = d_bfs();  
        cout<<ans+1<<endl;  
    }  
    return 0;  
}  

 

相关文章

发表新评论