zoukankan      html  css  js  c++  java
  • 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): 2678    Accepted Submission(s): 1179

    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

    由于此处的visit标志,只需要标志有没有搜索过,因此只要设置0,1来区分就可以。搜过的,就可以直接跳过

    #include <iostream>
    #include <math.h>
    #include <queue>
    using namespace std;
    
    typedef struct data
    {
        int count;
        int a[5];
    };
    int visit1[10005];
    int add(int a)
    {
        if(a == 9)
            return 1;
        else
            return (a+1);
    }
    int negative(int a)
    {
        if(a ==1)
            return 9;
        else
            return (a-1);
    }
    void swap(int &a ,int &b)
    {
        int temp ;
        temp = a;
        a = b;
        b =temp;
    }
    int hash_func(int u[])
    {
        int sum =0;
        for(int i =1;i<=4;i++)
        {
            sum=sum*10 + u[i];
        }
        return sum;
    }
    void bfs(int *u, int *password)
    {
        int i;
        int hash_number;
        int hash_pass = hash_func(password);
        data w,ww;
        queue<data>mqueue;
        for(i =1;i<=4;i++)
            w.a[i] = u[i];
        w.count = 0;
        mqueue.push(w);
        visit1[hash_func(w.a)] = 1;
        if(hash_func(w.a) ==  hash_pass)
            return 0;
        while (!mqueue.empty())
        {
            w = mqueue.front();
            mqueue.pop();
            for(i =1;i<=4;i++)
            {
                ww = w;
                ww.a[i] = add(ww.a[i]);   //加1搜索
                hash_number = hash_func(ww.a);
                if(!visit1[hash_number])
                {
                    if(hash_number ==hash_pass)
                    {
                        printf("%d\n",w.count);
                        break;
                    }
                    else
                    {
                        visit1[hash_number] =1;
                        ww.count++;
                        mqueue.push(ww);
                    }
                }
                
                ww = w;
                ww.a[i] = negative(ww.a[i]); //减1,搜索
                hash_number = hash_func(ww.a);
                if(!visit1[hash_number])
                {
                    if(hash_number ==hash_pass)
                    {
                        printf("%d\n",w.count);
                        break;
                    }
                    else
                    {
                        visit1[hash_number] = 1;
                        ww.count++;
                        mqueue.push(ww);
                    }
                }
            }
            for( i =1;i<4;i++)//交换搜索
            {
                ww = w;
                swap(ww.a[i],ww.a[i+1]); 
                hash_number = hash_func(ww.a);
                if(!visit1[hash_number])
                {
                    if(hash_number ==hash_pass)
                    {
                        printf("%d\n",w.count);
                        break;
                    }
                    else
                    {
                        visit1[hash_number] = 1;
                        ww.count++;
                        mqueue.push(ww);
                    }
                }
            }
        }
    }
    int main()
    {
        int N;
        char s;
        int i,j;
        char pass;
        int a[5];
        int b[5];
        while(scanf("%d",&N)!=EOF)
        {
            for(int k =1;k<=N;k++)
            {
                memset(visit1,0,sizeof(visit1));
                for (i =1 ;i<=4;i++)
                    {
                        cin>>s;
                        a[i] = s-'0';
                    }
                    for(i =1;i<=4;i++)
                    {
                        cin>>pass;
                        b[i] = pass-'0';
                    }
                    bfs(a,b);
            }
        }
        return 0;
    }
  • 相关阅读:
    [每日一题]:小猫爬山
    [每日一题]:Wannafly挑战赛1 -- Treepath
    JavaWeb学习(19): 文件的上传和下载
    [每日一题]:牛客练习赛61 A:打怪
    [十二省联考2019]春节十二响——长链剖分+堆
    BZOJ4977[Lydsy1708月赛]跳伞求生——贪心+堆+模拟费用流
    BZOJ4003[JLOI2015]城池攻占——可并堆
    BZOJ1823[JSOI2010]满汉全席——2-SAT+tarjan缩点
    BZOJ3876[Ahoi2014&Jsoi2014]支线剧情——有上下界的最小费用最大流
    BZOJ2738矩阵乘法——整体二分+二维树状数组
  • 原文地址:https://www.cnblogs.com/cheng07045406/p/3113189.html
Copyright © 2011-2022 走看看