[HUD 1195] Open the Lock

 

Open the Lock



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
 
第一道双向BFS题、- -
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 10000

struct Node{
    int n,t;
    Node(){}
    Node(int _n,int _t):n(_n),t(_t){}
};
struct Vis{
    int d,t;
    Vis(){}
    Vis(int _d,int _t):d(_d),t(_t){}
};

int s,e;
Vis vis[N];

inline void getd(int n,int *a)
{
    int k=0;
    while(n){
        a[k++]=n%10;
        n/=10;
    }
}
inline int getn(int *a)
{
    int n=0;
    for(int i=3;i>=0;i--){
        n=n*10+a[i];
    }
    return n;
}
int bfs()
{
    int sp=0; //层数控制
    int t1[4],t2[4];
    Node now,next;
    memset(vis,0,sizeof(vis));
    queue<Node> p,q;
    p.push(Node(s,0));
    q.push(Node(e,0));
    vis[s]=Vis(1,0);
    vis[e]=Vis(2,0);
    while(!p.empty() && !q.empty()){
        while(p.front().t==sp){
            Node now=p.front();
            p.pop();
            //加减1
            getd(now.n,t1);
            for(int i=0;i<8;i++){
                memcpy(t2,t1,sizeof(t1));
                if(i<4) t2[i]=t1[i]+1>9?1:t1[i]+1;
                else t2[i-4]=t1[i-4]-1<1?9:t1[i-4]-1;
                next.t=now.t+1;
                next.n=getn(t2);
                if(vis[next.n].d==1) continue;
                if(vis[next.n].d==2) return next.t+vis[next.n].t;
                vis[next.n].d=1;
                vis[next.n].t=next.t;
                p.push(next);
            }
            //交换相邻
            for(int i=0;i<3;i++){
                memcpy(t2,t1,sizeof(t1));
                swap(t2[i],t2[i+1]);
                next.t=now.t+1;
                next.n=getn(t2);
                if(vis[next.n].d==1) continue;
                if(vis[next.n].d==2) return next.t+vis[next.n].t;
                vis[next.n].d=1;
                vis[next.n].t=next.t;
                p.push(next);
            }
        }
        while(q.front().t==sp){
            Node now=q.front();
            q.pop();
            //加减1
            getd(now.n,t1);
            for(int i=0;i<8;i++){
                memcpy(t2,t1,sizeof(t1));
                if(i<4) t2[i]=t1[i]+1>9?1:t1[i]+1;
                else t2[i-4]=t1[i-4]-1<1?9:t1[i-4]-1;
                next.t=now.t+1;
                next.n=getn(t2);
                if(vis[next.n].d==2) continue;
                if(vis[next.n].d==1) return next.t+vis[next.n].t;
                vis[next.n].d=2;
                vis[next.n].t=next.t;
                q.push(next);
            }
            //交换相邻
            for(int i=0;i<3;i++){
                memcpy(t2,t1,sizeof(t1));
                swap(t2[i],t2[i+1]);
                next.t=now.t+1;
                next.n=getn(t2);
                if(vis[next.n].d==2) continue;
                if(vis[next.n].d==1) return next.t+vis[next.n].t;
                vis[next.n].d=2;
                vis[next.n].t=next.t;
                q.push(next);
            }
        }
        sp++;
    }
    return -1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&s,&e);
        printf("%d\n",bfs());
    }
    return 0;
}

 

posted @ 2015-07-05 11:34  哈特13  阅读(372)  评论(0编辑  收藏  举报