• 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;
    }
    
    
    
     
  • 相关阅读:
    使用.Net Core RT 标准动态库
    X509 颁发者和使用者 详解
    .Net Core 中X509Certificate2 私钥保存为 pem 的方法
    dpkg:处理软件包 mysql-server-5.5 (--configure)时出错
    sql server time(7) 默认值
    如何 打包整合linux系统文件夹 用于刷机包等等, 其中包括打包 句号开头 . 开头的文件, 排除系统文件 等
    Visual Studio 2015中 安卓环境 cannot find adb.exe in specified sdk path
    PuTTY 命令行改进 有效解决 中文乱码
    System.Windows.Forms.WebBrowser中 处理 js 脚本 window.Open 禁止新建窗口 的方法
    rest_framework之认证与权限 token不存数据库认证
  • 原文地址:https://www.cnblogs.com/orion7/p/7337642.html
Copyright © 2020-2023  润新知