• hdu1195


    Open the Lock

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


    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
     
     
    广搜
    #include<stdio.h>
    #include<queue>
    #include<string.h>
    using namespace std;
    char a[6],b[6];
    int end,visit[10000];
    typedef struct
    {
        int num;
        int step;
    } point;
    point start;
    void BFS(point s)
    {
        point p;
        int t[5],i,n[5],num;
        queue<point>q;
        visit[s.num]=1;
        q.push(s);
        while(!q.empty())
        {
            p=q.front();
            q.pop();
            if(p.num==end)
            {
                printf("%d
    ",p.step);
                break;
            }
            t[1]=p.num/1000;
            t[2]=p.num%1000/100;
            t[3]=p.num%100/10;
            t[4]=p.num%10;
            for(i=1; i<=4; i++)
            {
                n[1]=t[1];
                n[2]=t[2];
                n[3]=t[3];
                n[4]=t[4];
                n[i]=t[i]+1;
                if(n[i]==10)
                    n[i]=1;
                num=n[1]*1000+n[2]*100+n[3]*10+n[4];
                if(visit[num]==0)
                {
                    visit[num]=1;
                    s.num=num;
                    s.step=p.step+1;
                    q.push(s);
                }
            }
            for(i=1; i<=4; i++)
            {
                n[1]=t[1];
                n[2]=t[2];
                n[3]=t[3];
                n[4]=t[4];
                n[i]=t[i]-1;
                if(n[i]==0)
                    n[i]=9;
                num=n[1]*1000+n[2]*100+n[3]*10+n[4];
                if(visit[num]==0)
                {
                    visit[num]=1;
                    s.num=num;
                    s.step=p.step+1;
                    q.push(s);
                }
            }
            n[1]=t[2];
            n[2]=t[1];
            n[3]=t[3];
            n[4]=t[4];
            num=n[1]*1000+n[2]*100+n[3]*10+n[4];
            if(visit[num]==0)
            {
                visit[num]=1;
                s.num=num;
                s.step=p.step+1;
                q.push(s);
            }
            n[1]=t[1];
            n[2]=t[3];
            n[3]=t[2];
            n[4]=t[4];
            num=n[1]*1000+n[2]*100+n[3]*10+n[4];
            if(visit[num]==0)
            {
                visit[num]=1;
                s.num=num;
                s.step=p.step+1;
                q.push(s);
            }
            n[1]=t[1];
            n[2]=t[2];
            n[3]=t[4];
            n[4]=t[3];
            num=n[1]*1000+n[2]*100+n[3]*10+n[4];
            if(visit[num]==0)
            {
                visit[num]=1;
                s.num=num;
                s.step=p.step+1;
                q.push(s);
            }
        }
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        while(n--)
        {
            getchar();
            scanf("%s%*c",a+1);
            scanf("%s",b+1);
            end=(b[1]-'0')*1000+(b[2]-'0')*100+(b[3]-'0')*10+(b[4]-'0');
            start.num=(a[1]-'0')*1000+(a[2]-'0')*100+(a[3]-'0')*10+(a[4]-'0');
            start.step=0;
            memset(visit,0,sizeof(visit));
            BFS(start);
        }
        return 0;
    }
    
  • 相关阅读:
    用java实现输出英文小说飘中出现次数最多的前N个单词(附:使用文件读写)
    程序员修炼之道第一章读后感
    c++实现线性表中的顺序表(数据结构课程作业)
    java第二次课件课后动手动脑习题整理总结(2019年9月23号)
    课堂练习判断字符串是否为回文序列
    大二上学期九月周总结报告三
    以java实现的一个简单登录界面(带验证码)
    关于二进制的原码 、反码、补码的简要解释说明
    几种方式使用
    spring配置数据源的6种方式
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3235788.html
Copyright © 2020-2023  润新知