HDU 1195 Open the Lock (不一样的BFS)

Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6045    Accepted Submission(s): 2697

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

Zhejiang University Local Contest 2005

Recommend

Ignatius.L

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195

知道是BFS 的题目,但想不到怎么去搜,据说比素数环还要简单,经过别人的点拨和网上的代码,瞬间发现,想通了真的比素数环还要简单

AC代码:

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
struct node
{
    int a[4],step;
};
char str[2][5];
int f[4];
bool vis[10][10][10][10];
int BFS()
{
    node now,next;
    for(int i=0;i<4;i++)
    {
        now.a[i]=str[0][i]-'0';
        f[i]=str[1][i]-'0';
    }
    memset(vis,false,sizeof(vis));
    now.step=0;
    queue<node>q;
    q.push(now);
    bool lock;

    while(!q.empty())
    {
        now=q.front();
        q.pop();
        lock=true;
        for(int i=0;i<4;i++)
        {
            if(now.a[i]!=f[i])
            {
                lock=false;
                break;
            }
        }
        if(lock)
            return now.step;
        for(int i=0;i<4;i++)//加一
        {
            next=now;
            if(now.a[i]==9)
                next.a[i]=1;
            else
                next.a[i]=now.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=now.step+1;
                q.push(next);
            }
        }
        for(int i=0;i<4;i++)//减一
        {
            next=now;
            if(now.a[i]==1)
                next.a[i]=9;
            else
                next.a[i]=now.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=now.step+1;
                q.push(next);
            }
        }
        for(int i=0;i<3;i++)//交换
        {
            next=now;
            next.a[i]=now.a[i+1];
            next.a[i+1]=now.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=now.step+1;
                q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>str[0]>>str[1];
        cout<<BFS()<<endl;
    }
    return 0;
}
时间: 2024-11-01 04:23:28

HDU 1195 Open the Lock (不一样的BFS)的相关文章

HDU 1195 Open the Lock (双向广搜)

题意:给你初始4个数字和目标4个数字,问是否能由初始经过变换到目标数字: 变换规则:每个数字可以加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别对初始和目标数字进行广搜,vis数组用1和2标记两种已搜索的数字,用mp数组记录状态的步数. 当从前往后搜可以到达2或从后往前搜可以到达1状态则就可以了... #include<stdio.h> #include<string.h> #include<string> #inclu

HDU 1195 Open the Lock 双向BFS

题目链接:Open the Lock 题意:就是给定两个4位数,起始,结束.问从起始数字到达结束数字 最少变换多少步,每个数 可以+1 / -1 或交换位置,都算做是一步. 单广,双广都用了,感觉双向BFS,太棒了,HDU的这个题双向BFS时间优化的太棒了 有图,有真相! 时间优化了近9倍... PS:今天还学习一个sscanf函数,挺棒的 单搜的代码就不贴了,贴个双搜的 #include<cstdio> #include <iostream> #include<cstrin

hdu 1195 Open the Lock (bfs+优先队列)

Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4253    Accepted Submission(s): 1858 Problem Description Now an emergent task for you is to open a password lock. The password is c

hdu 1195:Open the Lock(暴力BFS广搜)

mediaxyz是一位研究ffmpeg有三年的高人了,这几天一直在折腾ffmpeg中的x264,就是不知道该如何控制码率,主要是参数太多,也不知道该如何设置,在 google上search了一下,这方面的介绍为0,那就找mediaxyz请教请教吧,这些可都是经验,非常宝贵! 以下是与mediaxyz在QQ上聊天的记录,只有一部分,因为QQ把之前的谈话删除了,但基本上精髓都可这里了. mediaxyz 23:40:26你说的qsable是c->global_quality吧 Leon 23:40:

HDU 1195.Open the Lock

Open the Lock Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1195 Description Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbe

hdu - 1195 Open the Lock (bfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个状态都能遍历到. 得到四个数之后,分三种情况处理,每次改变一个数之后都要加入队列,最先输出的就是步数最少. 1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std;

HDU 1195 Open the Lock (双宽搜索)

意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别对初始和目标数字进行广搜,vis数组用1和2标记两种已搜索的数字,用mp数组记录状态的步数. 当从前往后搜能够到达2或从后往前搜能够到达1状态则就能够了... #include<stdio.h> #include<string.h> #include<string> #in

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

HDU 4771 Stealing Harry Potter&#39;s Precious dfs+bfs

Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his