• 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): 5307    Accepted Submission(s): 2360


    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
     
    Author
    YE, Kai
     
    Source
     
    Recommend
    Ignatius.L   |   We have carefully selected several similar problems for you:  1072 1180 1044 1254 1043 
     
    一个非常暴力的bfs,列举所有的数字串,用四维数组就行标记。
     
    题意:一个密码箱,第一行输入是初始密码,第二行是正确的密码,每次变化可以一个数字加1(9可以变成1),或者1个数字减1(1可以变成9),或者相邻的两个数字交换位置(1,4不能交换),输出最少多少次从初始密码变为正确密码。
     
    附上代码:
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 using namespace std;
     6 struct node
     7 {
     8     int num[4];
     9     int s;
    10 } s1,s2,fs,fe;
    11 int ha[10][10][10][10];  //用来标记,是否出现过这四个数字的字符串,1为已出现
    12 
    13 void BFS()
    14 {
    15     int i;
    16     queue <node> q;
    17     while(!q.empty())
    18         q.pop();
    19     fs.s=0;
    20     s1=fs;   //初始化
    21     ha[s1.num[0]][s1.num[1]][s1.num[2]][s1.num[3]]=1;
    22     q.push(s1);
    23     while(!q.empty())
    24     {
    25         s1=q.front();
    26         q.pop();
    27         for(i=0; i<4; i++)
    28             if(s1.num[i]!=fe.num[i])
    29                 break;
    30         if(i==4)   //4个数字全部相同,输出结果
    31         {
    32             printf("%d
    ",s1.s);
    33             return;
    34         }
    35         for(i=0; i<4; i++)   //加1变化
    36         {
    37             s2=s1;
    38             if(s1.num[i]==9) s2.num[i]=1;
    39             else s2.num[i]=s1.num[i]+1;
    40             s2.s=s1.s+1;
    41             if(!ha[s2.num[0]][s2.num[1]][s2.num[2]][s2.num[3]])
    42             {
    43                 ha[s2.num[0]][s2.num[1]][s2.num[2]][s2.num[3]]=1;
    44                 q.push(s2);
    45             }
    46         }
    47         for(i=0; i<4; i++)  //减1变化
    48         {
    49             s2=s1;
    50             if(s1.num[i]==1) s2.num[i]=9;
    51             else s2.num[i]=s1.num[i]-1;
    52             s2.s=s1.s+1;
    53             if(!ha[s2.num[0]][s2.num[1]][s2.num[2]][s2.num[3]])
    54             {
    55                 ha[s2.num[0]][s2.num[1]][s2.num[2]][s2.num[3]]=1;
    56                 q.push(s2);
    57             }
    58         }
    59         for(i=0; i<3; i++)  //交换
    60         {
    61             s2=s1;
    62             s2.num[i]=s1.num[i+1];
    63             s2.num[i+1]=s1.num[i];
    64             s2.s=s1.s+1;
    65             if(!ha[s2.num[0]][s2.num[1]][s2.num[2]][s2.num[3]])
    66             {
    67                 ha[s2.num[0]][s2.num[1]][s2.num[2]][s2.num[3]]=1;
    68                 q.push(s2);
    69             }
    70         }
    71     }
    72 }
    73 
    74 int main()
    75 {
    76     int T,i,j;
    77     char ch1[5],ch2[5];
    78     scanf("%d",&T);
    79     while(T--)
    80     {
    81         scanf("%s %s",ch1,ch2);  //输入两个字符串
    82         for(i=0; i<4; i++)
    83         {
    84             fs.num[i]=ch1[i]-'0';    //将其转换为4个数字记录到数组中
    85             fe.num[i]=ch2[i]-'0';
    86         }
    87         memset(ha,0,sizeof(ha));
    88         BFS();
    89     }
    90     return 0;
    91 }
  • 相关阅读:
    事件对象
    事件
    父元素高度为auto,子元素使用top:-50%没有效果的问题
    Window.open 实现导航与打开窗口,导航到一个特定链接地址,也可以打开一个新的浏览器窗体
    8.0 BOM对象
    7.4 私有变量
    006_函数填充_计算列
    004_005_数据区域读取_填充数字
    003_行_列_单元格
    002_读写文件
  • 原文地址:https://www.cnblogs.com/pshw/p/4757113.html
Copyright © 2020-2023  润新知