ACM-BFS之Open the Lock——hdu1195

Open the Lock

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

Total Submission(s): 3825    Accepted Submission(s): 1650

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

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1195

题意就是开锁,四个数字的锁,1~9没有0哟。

每次可以对任意一个数字+1或者-1,当数字为1,减1,则数字变为9。

同样,当数字为9,若+1,数字则变为1。

每次操作除了对任意一位数字+1或者-1之外,还可以对相邻位置的数字进行交换。

第一位置与第二位置 or 第二位置和第三位置 or 第三位置和第四位置

但是第一位置和第四位置不可以交换。

暴力广搜,尝试过剪枝,但发现减了就WA。。。o(╯□╰)o。。。

暴力也才62MS,等一会做个双广的来试试。

双向BFS已A,详情请戳:http://blog.csdn.net/lttree/article/details/24669525

/**************************************
***************************************
*        Author:Tree                 *
*From  :http://blog.csdn.net/lttree  *
* Title : Scrambled Polygon           *
*Source: hdu 1195                     *
* Hint  : 暴力BFS                     *
***************************************
**************************************/

#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
struct Key
{
    int k[4],step;
};
bool vis[10001];         // 9^4
int ini[4],ans[4],dis[2]={-1,1};
int solu[9]={9,1,2,3,4,5,6,7,8};

bool judge(Key a)
{
    for(int i=0;i<4;++i)
        if( a.k[i]!=ans[i] )
            return false;
    return true;
}
// 用来做VIS数组
int total(Key a)
{
    int i,sum;
    sum=0;
    for(i=0;i<4;++i)
        sum=sum*10+a.k[i];
    return sum;
}
int bfs(void)
{
    memset(vis,0,sizeof(vis));
    queue <Key> q;
    Key pre,lst;
    int i,t,sum;

    for(i=0;i<4;++i)
        pre.k[i]=ini[i];
    sum=total(pre);
    vis[sum]=1;
    pre.step=0;
    q.push(pre);

    while( !q.empty() )
    {
        pre=q.front();
        q.pop();
        if( judge(pre) )    return pre.step;

        // 每位数 加1或减1(若相应位置与答案不同)
        for(i=0;i<4;++i)
        {
            if( pre.k[i]!=ans[i] )
            {
                for(t=0; t<2; ++t)
                {
                    lst=pre;
                    lst.k[i]=solu[(pre.k[i]+dis[t])%9];
                    sum=total(lst);
                    if( vis[sum] )  continue;
                    lst.step=pre.step+1;
                    vis[sum]=1;
                    q.push(lst);
                }
            }
        }

        // 相邻交换
        for(i=0;i<3;++i)
        {
            lst=pre;
            lst.k[i]=pre.k[i+1];
            lst.k[i+1]=pre.k[i];
            sum=total(lst);
            if( !vis[sum] )
            {
                lst.step=pre.step+1;
                vis[sum]=1;
                q.push(lst);
            }
        }

    }
}
int main()
{
    int i,s,test;
    char c;
    cin>>test;
    while( test-- )
    {
        // 输入数据
        for(i=0;i<4;++i)
        {
            cin>>c;
            ini[i]=c-‘0‘;
        }
        for(i=0;i<4;++i)
        {
            cin>>c;
            ans[i]=c-‘0‘;
        }

        s=bfs();
        cout<<s<<endl;
    }
    return 0;
}
时间: 2024-12-20 04:47:09

ACM-BFS之Open the Lock——hdu1195的相关文章

HDU1195 ZOJ2416 Open the Lock

问题链接:HDU1195 ZOJ2416 Open the Lock. 题意简述:输入测试用例数t,每个例子包括两个4个数字的整数(由1到9组成),一个为源,另外一个为目标.每次可以将其中任何一个数字+1或者-1运算,并且规定1-1=9,9+1=1:也可以将相邻2位数进行交换.问最少需要变换几次,才能从源变为目标. 问题分析:该问题可以用BFS来解决.在BFS搜索过程中,出现过的4位数就不必再试探了,因为再用这个4位数变下去其次数不可能比上次开始的变换次数少.数组notvist[]用于避免重复搜

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

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Alice was felling into a cave. She found a strange door with a number square m

[ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 941    Accepted Submission(s): 352 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Clas

[ACM] hdu 1253 胜利大逃亡 (三维BFS)

胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出

[ACM] hdu 1242 【BFS】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 参考链接:http://www.acmerblog.com/hdu-1242-Rescue-1605.html 普通队列+bfs确实是蒙对的,因为击败守卫需要消耗时间1,因此普通队列每一次出队列的元素只是步数上最优,但不一定是时间上最优的,这时即使我们把整个迷宫搜完以最小值为最优依然不行,因为每一步处理完成都会把该状态标记为已处理vis[i][j]=1,因此,只要有一步不是最优,就会影响后面的

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 997    Accepted Submission(s): 306 Problem Description The empire is under attack again. The general of empire is planning to defend his

HDU1195 双向BFS(或BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195 , 双向BFS或者直接BFS也可以过. 其实这道题只是单向BFS就可以过的,但是为了练算法,所以还是用了双向BFS来写. 算法: 先预处理一下,从1111到9999的所有点进行构图(由于是1~9的,所以除去含有0元素的数字),能进行一次变换变成的数字则表示两点之间连通.然后从初态与目态两个点进行BFS,如果有轨迹重合的就返回路程和. 这里注意双向BFS要一层一层的进行搜索,不然的话会产生错误,