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;
    }
  • 相关阅读:
    监听浏览器使用不同版本js并且处理ie兼容getElementByClassName
    超出字数部分省略(主要解决不兼容;display: -webkit-box;的浏览器)
    highcharts图表
    整理前端css/js/jq常见问题及解决方法(1)
    [转载]移动页面所需meta元素和Viewport窗口知识点
    在ie和chrome浏览器中滚动条样式的设置
    关于:before :after
    mysql备份与还原
    使用xkbeancomparator对比javabean,生成操作记录
    SSO单点登录和CAS
  • 原文地址:https://www.cnblogs.com/cheng07045406/p/3113189.html
Copyright © 2011-2022 走看看