• hdu1195 Open the Lock (DFS)


    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
    #include<stdio.h>
    #include<string.h>
    int vist[10],flog;
    char str[5],ch[5];
    void DFS(int j,int x,int step,char ch1[])
    {
        int i,st,e;
        char tem[5];
    //printf("%s %d
    ",ch1,ste);
        if(str[0]==ch1[0]&&str[1]==ch1[1]&&str[2]==ch1[2]&&str[3]==ch1[3])//一样了
        {
            if(flog>step) //记录小的
            flog=ste;
            //printf("%d ",ste);
        }
        vist[x]=1; //表示在ch1中第x位置方问过
        for(i=x+1;i<4;i++)
        if(vist[i]==0)
        {
            if(str[j]-ch1[i]>=5||str[j]-ch1[i]<=-5)//第一步,当成立时,把ch1[i]加或减变成str[j]的所用时间加起来,会用tem装
            {
                if(str[j]>ch[i])
                st=(ch1[i]-'0'+'9'-str[j])+step;
                else
                st=(str[j]-'0'+'9'-ch1[i])+step;
            }
            else
            {
                if(str[j]>ch1[i])
                st=(str[j]-ch1[i])+step;
                else
                st=(ch1[i]-str[j])+step;
            }
            for(e=0;e<j;e++)//把ch1的第j个以前的数和str第j个数以前相同的数先装起来
            tem[e]=ch1[e];
            tem[j]=str[j];//当前第j个位置改变成str和第j个
            if(i!=j)//不相等说明是回朔了,i比j大
            {
                for(e=j+1;e<=i;e++)//把第i个要放到第j个前面,那么ch1中从第j个开始位置开始到第i个位置都往后存放一位,
                tem[e]=ch1[e-1];
                for(;e<4;e++) //把第i个位置以后的数装入
                tem[e]=ch1[e];
            }
            else//相等说明还没有回朔,那么就直接存放
            for(e=j+1;e<4;e++)
                tem[e]=ch1[e];
    //printf("%d %d %d %s %s
    ",j,i,st+i-j,tem,ch1);
    
            DFS(j+1,j,st+i-j,tem);//i-j的意思是把ch1里的第i个移到第j个位置,那么tem中从0->i(包括第i个)都是与str相同
            }
        vist[x]=0;
    }
    int main()
    {
        int i,t,j,e,st;
        char ch1[5];
        scanf("%d",&t);
        while(t--)
        {
            getchar();
            scanf("%s",str);
            getchar();
            scanf("%s",ch);
            flog=13322;
        for(i=0;i<4;i++)//以ch中的第i个数为开头,并把头一个变成str[0]相同
        {
                ch1[0]=ch[i];
                for(j=0,e=1;j<4;j++)
                if(j!=i)
                ch1[e++]=ch[j];
    
            if(str[0]-ch1[0]>=5||str[0]-ch1[0]<=-5)
            {
                if(str[0]>ch1[0])
                st=(ch1[0]-'0'+'9'-str[0]);
                else
                st=(str[0]-'0'+'9'-ch1[0]);
            }
            else
            {
                if(str[0]>ch1[0])
                st=(str[0]-ch1[0]);
                else
                st=(ch1[0]-str[0]);
            }
                ch1[0]=str[0];
                //printf("%s
    ",ch1);
                //printf("%d
    ",st+i);
                DFS(1,0,st+i,ch1);//i是从第i个到第一个须要多少步,st是把ch[i]变成str[0]须要多少步
                //printf("
    ");
        }
        printf("%d
    ",flog);
        }
    }
    


     
  • 相关阅读:
    练习:给Keras ResNet50源码加上正则化参数, 修改激活函数为Elu
    凸集,凸函数,凸优化问题。
    Keras用动态数据生成器(DataGenerator)和fitgenerator动态训练模型
    Lagrangian 对偶 和 Slater 条件
    凸集分离定理
    Python 中的 sorted 和 sort的区别
    工作反思
    jemalloc
    libcoap
    dropbear
  • 原文地址:https://www.cnblogs.com/riskyer/p/3235318.html
Copyright © 2020-2023  润新知