hdu 1195 Open the Lock 单项bfs(31ms) 双向bfs(0ms)

来源:互联网 发布:淘宝衣服关键词 编辑:程序博客网 时间:2024/05/22 12:50

Open the Lock

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


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
21234214411119999
 

Sample Output
24
题目大意:给出原密码和准确密码,对于原密码每次可以进行三个操作:   1:任意一位+1  且9+1=1   2:任意一位-1  且1-1=9   3:相邻两位互换,且首位和末位不是相邻的。(密码只有四位数,每个数字1-9)  题目要求原密码变成准确密码的最少步数
思路:挺简单的bfs 就不用说了
感想: 第一次用双向bfs 感觉还可以 比单项bfs快
下面贴一下两种做法:
1.单项bfs
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;int n,m,ans;int dx[]= {1,10,100,1000};int dy[]= {1,-1};int q[10005];int step[10005];int vis[10005];int getdight(int s,int k)             // 取出s的第k+1位{    int i,j,t;    switch(k)    {        case 0:t=s%10;break;        case 1:t=(s/10)%10;break;        case 2:t=(s/100)%10;break;        case 3:t=(s/1000)%10;break;    }    return t;}void bfs(){    int i,j,nx,tx,ttx,temp;    int d[5];    int head=0,tail=-1;    memset(vis,0,sizeof(vis));    vis[n]=1;    step[n]=0;    q[++tail]=n;    while(head<=tail)    {        nx=q[head];        if(nx==m)        {            ans=step[m];            return ;        }        for(i=0; i<4; i++)        {            d[i]=getdight(nx,i);        }        for(i=0; i<4; i++)             // 增减        {            tx=nx;            for(j=0; j<2; j++)            {                ttx=tx;                ttx-=d[i]*dx[i];                temp=d[i]+dy[j];                if(temp==0) temp=9;                if(temp==10) temp=1;                ttx+=temp*dx[i];                if(!vis[ttx])                {                    vis[ttx]=1;                    q[++tail]=ttx;                    step[ttx]=step[nx]+1;                }            }        }        tx=nx;                       //交换个位 十位        tx=tx/100*100;        tx+=d[1]+d[0]*10;        if(!vis[tx])        {            vis[tx]=1;            q[++tail]=tx;            step[tx]=step[nx]+1;        }        tx=nx;                       //交换十位 百位        tx=tx-d[1]*10-d[2]*100;        tx=tx+d[1]*100+d[2]*10;        if(!vis[tx])        {            vis[tx]=1;            q[++tail]=tx;            step[tx]=step[nx]+1;        }        tx=nx;                       //交换百位 千位        tx=tx-d[2]*100-d[3]*1000;        tx=tx+d[2]*1000+d[3]*100;        if(!vis[tx])        {            vis[tx]=1;            q[++tail]=tx;            step[tx]=step[nx]+1;        }        head++;    }}int main(){    int i,j,t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        bfs();        printf("%d\n",ans);    }    return 0;}

2.双向bfs (前提:从前往后扩展和从后往前扩展时可逆的) 就是代码要长一点 不过很好由单项bfs改之得
#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;int n,m,ans;int dx[]= {1,10,100,1000};int dy[]= {1,-1};int q1[10005],q2[10005];int step1[10005],step2[10005];int vis1[10005],vis2[10005];int getdight(int s,int k){    int i,j,t;    switch(k)    {    case 0:        t=s%10;        break;    case 1:        t=(s/10)%10;        break;    case 2:        t=(s/100)%10;        break;    case 3:        t=(s/1000)%10;        break;    }    return t;}void bfs(){    int i,j,nx,tx,ttx,temp,nstep1,nstep2;    int d[5];    int head1=0,head2=0,tail1=-1,tail2=-1;    memset(vis1,0,sizeof(vis1));    memset(vis2,0,sizeof(vis2));    vis1[n]=1;    step1[n]=0;    q1[++tail1]=n;    vis2[m]=1;    step2[m]=0;    q2[++tail2]=m;    nstep1=nstep2=0;    while(1)    {        while(step1[q1[head1]]==nstep1&&head1<=tail1)      // 扩展第一个队列  每次将一层的节点扩展        {            nx=q1[head1];            if(vis2[nx])                // 如果在第二个队列中已得到 则可得到最佳ans 退出循环            {                ans=step1[nx]+step2[nx];                return ;            }            for(i=0; i<4; i++)            {                d[i]=getdight(nx,i);            }            for(i=0; i<4; i++)             // 增减            {                tx=nx;                for(j=0; j<2; j++)                {                    ttx=tx;                    ttx-=d[i]*dx[i];                    temp=d[i]+dy[j];                    if(temp==0) temp=9;                    if(temp==10) temp=1;                    ttx+=temp*dx[i];                    if(!vis1[ttx])                    {                        vis1[ttx]=1;                        q1[++tail1]=ttx;                        step1[ttx]=step1[nx]+1;                    }                }            }            tx=nx;                       //交换个位 十位            tx=tx/100*100;            tx+=d[1]+d[0]*10;            if(!vis1[tx])            {                vis1[tx]=1;                q1[++tail1]=tx;                step1[tx]=step1[nx]+1;            }            tx=nx;                       //交换十位 百位            tx=tx-d[1]*10-d[2]*100;            tx=tx+d[1]*100+d[2]*10;            if(!vis1[tx])            {                vis1[tx]=1;                q1[++tail1]=tx;                step1[tx]=step1[nx]+1;            }            tx=nx;                       //交换百位 千位            tx=tx-d[2]*100-d[3]*1000;            tx=tx+d[2]*1000+d[3]*100;            if(!vis1[tx])            {                vis1[tx]=1;                q1[++tail1]=tx;                step1[tx]=step1[nx]+1;            }            head1++;        }        nstep1++;        while(step2[q2[head2]]==nstep2&&head2<=tail2)      // 扩展第二个队列        {            nx=q2[head2];            if(vis1[nx])                            // 同理            {                ans=step1[nx]+step2[nx];                return ;            }            for(i=0; i<4; i++)            {                d[i]=getdight(nx,i);            }            for(i=0; i<4; i++)             // 增减            {                tx=nx;                for(j=0; j<2; j++)                {                    ttx=tx;                    ttx-=d[i]*dx[i];                    temp=d[i]+dy[j];                    if(temp==0) temp=9;                    if(temp==10) temp=1;                    ttx+=temp*dx[i];                    if(!vis2[ttx])                    {                        vis2[ttx]=1;                        q2[++tail2]=ttx;                        step2[ttx]=step2[nx]+1;                    }                }            }            tx=nx;                       //交换个位 十位            tx=tx/100*100;            tx+=d[1]+d[0]*10;            if(!vis2[tx])            {                vis2[tx]=1;                q2[++tail2]=tx;                step2[tx]=step2[nx]+1;            }            tx=nx;                       //交换十位 百位            tx=tx-d[1]*10-d[2]*100;            tx=tx+d[1]*100+d[2]*10;            if(!vis2[tx])            {                vis2[tx]=1;                q2[++tail2]=tx;                step2[tx]=step2[nx]+1;            }            tx=nx;                       //交换百位 千位            tx=tx-d[2]*100-d[3]*1000;            tx=tx+d[2]*1000+d[3]*100;            if(!vis2[tx])            {                vis2[tx]=1;                q2[++tail2]=tx;                step2[tx]=step2[nx]+1;            }            head2++;        }        nstep2++;    }}int main(){    int i,j,t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        bfs();        printf("%d\n",ans);    }    return 0;}