首页 > 其他 > 详细

HDU-1195-Open the Lock(BFS)

时间:2014-08-13 14:57:36      阅读:329      评论:0      收藏: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
 

Author
YE, Kai
 

Source


思路:又是一道水题。


#include <stdio.h>
#include <string.h>
#include <cstdlib>

struct{
char num[5];
int step;
}que[1000000],t;

int p[4]={1,10,100,1000};
bool vis[10000];

int main()
{
    int T,i,j,len,val,temp;
    char des[5];

    scanf("%d",&T);

    while(T--)
    {
        scanf("%s%s",que[0].num,des);

        int top=0,bottom=1;
        que[0].step=0;

        memset(vis,0,sizeof vis);

        while(top<bottom)
        {
            t=que[top];

            if(strcmp(t.num,des)==0)
            {
                printf("%d\n",t.step);

                break;
            }

            t.step++;

            for(i=0;i<4;i++)
            {

                if(t.num[i]=='9') t.num[i]='1';
                else t.num[i]++;

                val=0;

                for(j=0;j<4;j++) val=val*10+t.num[j]-'0';

                if(!vis[val])
                {
                    vis[val]=1;

                    que[bottom++]=t;
                }

                strcpy(t.num,que[top].num);

                if(t.num[i]=='1') t.num[i]='9';
                else t.num[i]--;

                val=0;

                for(j=0;j<4;j++) val=val*10+t.num[j]-'0';

                if(!vis[val])
                {
                    vis[val]=1;

                    que[bottom++]=t;
                }

                strcpy(t.num,que[top].num);
            }

            for(i=0;i<3;i++)
            {
                temp=t.num[i];
                t.num[i]=t.num[i+1];
                t.num[i+1]=temp;

                val=0;

                for(j=0;j<4;j++) val=val*10+t.num[j]-'0';

                if(!vis[val])
                {
                    vis[val]=1;

                    que[bottom++]=t;
                }

                temp=t.num[i];
                t.num[i]=t.num[i+1];
                t.num[i+1]=temp;
            }

            top++;
        }
    }
}


HDU-1195-Open the Lock(BFS),布布扣,bubuko.com

HDU-1195-Open the Lock(BFS)

原文:http://blog.csdn.net/faithdmc/article/details/38536261

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!