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;
    }
  • 相关阅读:
    Python协程
    Python3常用标准库
    温故而知新--day3
    温故而知新--day2
    解决 WPF 绑定集合后数据变动界面却不更新的问题
    WPF 消息框 TextBox 绑定新数据时让光标和滚动条跳到最下面
    C# 枚举转列表
    真・WPF 按钮拖动和调整大小
    使用 GB28181.Solution + ZLMediaKit + MediaServerUI 进行摄像头推流和播放
    将 .NET Framework 项目转换为 .NET Standard 项目
  • 原文地址:https://www.cnblogs.com/cheng07045406/p/3113189.html
Copyright © 2011-2022 走看看