HDU 1195 Open the Lock(隐式图的遍历)

题目地址:点击打开链接

题意:给你一个初始的密码和最后的密码求从初始到最后所需的最少的步数,每位数字可以加1,减1,和交换,9加1位1,1减1为9,第一位只能和右边的一位交换,最后一位只能和左边的一位交换

思路:隐式图的遍历,把能走的情况全走一遍并标记下来,直到到达最终状态

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

using namespace std;

struct password
{
    int digital[4];
    int step;
}start,middle1,middle2,end1;

char word1[5],word2[5];
int mark[10][10][10][10];

void bfs()
{
    int i;
    queue<password> que;
    que.push(start);
    mark[ start.digital[0] ][ start.digital[1] ][ start.digital[2] ][ start.digital[3] ] = 1;
    while(!que.empty())
    {
        middle1 =  que.front();
        que.pop();
        for(i=0; i<4; i++)
        {
            if(middle1.digital[i] != end1.digital[i])
                break;
        }
        if(i == 4)
        {
            printf("%d\n",middle1.step);
            return;
        }
        for(i=0; i<4; i++)
        {
            middle2 = middle1;//加1
            middle2.step++;
            if(middle1.digital[i] == 9)
            {
                middle2.digital[i] = 1;
            }
            else
            {
                middle2.digital[i] += 1;
            }
            if(!mark[ middle2.digital[0] ][ middle2.digital[1] ][ middle2.digital[2] ][ middle2.digital[3] ])
            {
                mark[ middle2.digital[0] ][ middle2.digital[1] ][ middle2.digital[2] ][ middle2.digital[3] ] = 1;
                que.push(middle2);
            }
            middle2 = middle1;
            middle2.step++;
            if(middle1.digital[i] == 1)//减1
            {
                middle2.digital[i] = 9;
            }
            else
            {
                middle2.digital[i] -= 1;
            }
            if(!mark[ middle2.digital[0] ][ middle2.digital[1] ][ middle2.digital[2] ][ middle2.digital[3] ])
            {
                mark[ middle2.digital[0] ][ middle2.digital[1] ][ middle2.digital[2] ][ middle2.digital[3] ] = 1;
                que.push(middle2);
            }
            if(i < 3)//交换
            {
                middle2 = middle1;
                middle2.step++;
                int temp = middle2.digital[i];
                middle2.digital[i] = middle2.digital[i+1];
                middle2.digital[i+1] = temp;
                if(!mark[ middle2.digital[0] ][ middle2.digital[1] ][ middle2.digital[2] ][ middle2.digital[3] ])
                {
                    mark[ middle2.digital[0] ][ middle2.digital[1] ][ middle2.digital[2] ][ middle2.digital[3] ] = 1;
                    que.push(middle2);
                }
            }
        }
    }
}
int main()
{
    int t,i;
    scanf("%d",&t);
    while(t--)
    {
        memset(mark,0,sizeof(mark));
        scanf("%s%s",word1,word2);
        if(strcmp(word1,word2) == 0)
        {
            printf("0\n");
            continue;
        }
        for(i=0; i<4; i++)
        {
            start.digital[i] = word1[i] - '0';
            start.step = 0;
            end1.digital[i] = word2[i] - '0';
        }
        bfs();
    }
    return 0;
}
    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/qq_25605637/article/details/48947883
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞