zoukankan      html  css  js  c++  java
  • HDU 1195 Open the Lock(BFS)

    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. 

    InputThe 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. 
    OutputFor each test case, print the minimal steps in one line. 
    Sample Input

    2
    1234
    2144
    
    1111
    9999

    Sample Output

    2
    4
    题意:
       给出两组数字,问最短需要几步可以变换到想要的数字。
    题解:
       刚开始是没有什么思路的,感觉很麻烦。其实想通了之后很简单,就是普通的BFS,只不过这道题的搜索方式很多,代码长了一些。

    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,m,ans[4];
    const int maxn=1e4+5;
    struct node
    {
        int a[4],step;
    };
    bool vis[10][10][10][10];
    int bfs()
    {
        queue<node> que;
        node cur;
        int temp=1000;
        for(int i=0;i<4;i++)
        {
            cur.a[i]=n/temp%10;
            ans[i]=m/temp%10;
            temp/=10;
        }
        cur.step=0;
        que.push(cur);
        while(que.size())
        {
            cur=que.front();
            que.pop();
            int k;
            for(k=0;k<4;k++)
                if(cur.a[k]!=ans[k])
                    break;
            if(k==4)
                return cur.step;
            node next;
            for(int i=0;i<4;i++)//加一
            {
                next=cur;
                if(next.a[i]==9)
                    next.a[i]=1;
                else
                    next.a[i]++;
                if(!vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]])
                {
                    vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]]=true;
                    next.step++;
                    que.push(next);
                }
            }
            for(int i=0;i<4;i++)//减一
            {
                next=cur;
                if(next.a[i]==1)
                    next.a[i]=9;
                else
                    next.a[i]--;
                if(!vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]])
                {
                    vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]]=true;
                    next.step++;
                    que.push(next);
                }
            }
            for(int i=0;i<3;i++)//相邻数字交换
            {
                next=cur;
                swap(next.a[i],next.a[i+1]);
                if(!vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]])
                {
                    vis[next.a[0]][next.a[1]][next.a[2]][next.a[3]]=true;
                    next.step++;
                    que.push(next);
                }
            }
        }
        return -1;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            cin>>n>>m;
            memset(vis,false,sizeof(vis));
            cout<<bfs()<<endl;
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    在UITableView顶部制作简单的UISegmentControl实例方法
    iOS9 适配网络请求,适配分享失败,适配无法正常跳转到客户端
    iOS页面切换动画实现方式。
    Springboot-Dockerfile指令
    Springboot-Docker-Dockerfile
    Springboot-Docker-1
    SpringBoot-Security
    SpringBoot-MongoDB
    SpringBoot-RabbitMQ
    SpringBoot-Memcached
  • 原文地址:https://www.cnblogs.com/orion7/p/7337642.html
Copyright © 2011-2022 走看看