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 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

学会复制粘贴吧,bfs+优先队列,很水

#include"stdio.h"
#include"string.h"
#include"queue"
#include"algorithm"
using namespace std;
struct node
{
    int x,step;
    friend bool operator<(node a,node b)
    {
        return a.step>b.step;
    }
};
int a,b,c,d;
int mark[10000];
int fun(int x,int d)
{
    x+=d;
    if(x==10)
        x=1;
    if(x==0)
        x=9;
    return x;
}
int fun2(int a,int b,int c,int d)
{
    return a*1000+b*100+c*10+d;
}
void inti(int x)
{
    a=x/1000;b=(x/100)%10;
    c=(x/10)%10;d=x%10;
}
int bfs(int s,int t)
{
    int x;
    priority_queue<node >q;
    node cur,next;
    cur.x=s;
    cur.step=0;
    q.push(cur);
    memset(mark,0,sizeof(mark));
    mark[s]=1;
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        if(cur.x==t)
            return cur.step;
        next.step=cur.step+1;
        inti(cur.x);

        next.x=x=fun2(fun(a,1),b,c,d);
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }
        next.x=x=fun2(fun(a,-1),b,c,d);
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }

        next.x=x=fun2(a,fun(b,1),c,d);
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }
        next.x=x=fun2(a,fun(b,-1),c,d);
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }

        next.x=x=fun2(a,b,fun(c,1),d);
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }
        next.x=x=fun2(a,b,fun(c,-1),d);
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }

        next.x=x=fun2(a,b,c,fun(d,1));
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }
        next.x=x=fun2(a,b,c,fun(d,-1));
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }

        next.x=x=fun2(b,a,c,d);
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }
        next.x=x=fun2(a,c,b,d);
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }
        next.x=x=fun2(a,b,d,c);
        if(!mark[x])
        {
            mark[x]=1;
            q.push(next);
        }
    }
    return 1;
}
int main()
{
    int T,s,t;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&s,&t);
        int ans=bfs(s,t);
        printf("%d\n",ans);
    }
    return 0;
}

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

时间: 2024-10-09 22:15:30

hdu 1195 Open the Lock (bfs+优先队列)的相关文章

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 双向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广搜)

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 (双向广搜)

题意:给你初始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 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an

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 c

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 3152 Obstacle Course(BFS+优先队列 重载)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3152 Problem Description You are working on the team assisting with programming for the Mars rover. To conserve energy, the rover needs to find optimal paths across the rugged terrain to get from its sta

hdu - 1180 诡异的楼梯 (bfs+优先队列)

http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是水平的还是垂直的. 并且我们需要知道当前到达楼梯这个点的方向,这样才知道下一个往哪个方向走,可以根据dir数组来判断方向. 楼梯不用判重. #include<stdio.h> #include<string.h> #include<queue> #include<io