zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    设计模式(一)基础面向对象
    面试题之三门问题
    「巫师3(The Witcher 3:Wild Hunt)」游戏测评
    欧拉角和四元数
    struts标签遍历各种list Map
    浅谈HtmlCleaner+XPath解析html或者xml
    hadoop简单例子
    解决JSP参数传递乱码的问题
    Set、List、Map的区别(浅谈)
    做个犀利的码农
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3235788.html
Copyright © 2011-2022 走看看