zoukankan      html  css  js  c++  java
  • HDU 1195 Open the Lock

    Open the Lock

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6955    Accepted Submission(s): 3130


    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
    AC代码(BFS+QUEUE)
    注意点:memset() 头文件为:cstring.h
     1 #include<iostream>
     2 #include<fstream>
     3 #include<string>
     4 #include<cstring>
     5 #include<queue>
     6 #include<memory>
     7 using namespace std;
     8 const int MAX=10005;
     9 int step[MAX];
    10 struct data
    11 {
    12     int dig[4];
    13 };
    14 void BFS(int loc[],int pwd[])
    15 {
    16     step[loc[0]*1000+loc[1]*100+loc[2]*10+loc[3]]=0;
    17     data beg={{loc[0],loc[1],loc[2],loc[3]}};
    18     queue<data> que; que.push(beg);
    19     while(!que.empty())
    20     {
    21         data next,cur=que.front(); que.pop();
    22         if(cur.dig[0]==pwd[0]&&cur.dig[1]==pwd[1]&&cur.dig[2]==pwd[2]&&cur.dig[3]==pwd[3])
    23         {
    24             cout<<step[pwd[0]*1000+pwd[1]*100+pwd[2]*10+pwd[3]]<<endl;
    25             break;
    26         }
    27         for(int i=0;i<4;i++)
    28         {
    29             next=cur; next.dig[i]=cur.dig[i]+1;//加1
    30             if(next.dig[i]>=10) next.dig[i]=1;
    31             int nextState=next.dig[0]*1000+next.dig[1]*100+next.dig[2]*10+next.dig[3];
    32             int curState=cur.dig[0]*1000+cur.dig[1]*100+cur.dig[2]*10+cur.dig[3];
    33             if(step[nextState]==0||step[nextState]>step[curState]+1)
    34             {
    35                 step[nextState]=step[curState]+1;
    36                 que.push(next);
    37             }
    38             next=cur;next.dig[i]=cur.dig[i]-1;//减1
    39             if(next.dig[i]<=0) next.dig[i]=9;
    40             nextState=next.dig[0]*1000+next.dig[1]*100+next.dig[2]*10+next.dig[3];
    41             if(step[nextState]==0||step[nextState]>step[curState]+1)
    42             {
    43                 step[nextState]=step[curState]+1;
    44                 que.push(next);
    45             }
    46             if(i<3)//交换
    47             {
    48                 next=cur;
    49                 int temp; temp=next.dig[i]; next.dig[i]=next.dig[i+1]; next.dig[i+1]=temp;
    50                 nextState=next.dig[0]*1000+next.dig[1]*100+next.dig[2]*10+next.dig[3];
    51                 if(step[nextState]==0||step[nextState]>step[curState]+1)
    52                 {
    53                     step[nextState]=step[curState]+1;
    54                     que.push(next);
    55                 }
    56             }
    57         }
    58     }
    59 }
    60 int main()
    61 {
    62     //ifstream in("data.txt");
    63     int T;cin>>T;
    64     while(T--)
    65     {
    66         int loc[4],pwd[4];
    67         string l,p;cin>>l>>p;
    68         int i=0,j,steps;
    69         while(i<4)
    70         {
    71             loc[i]=l[i]-'0'; pwd[i]=p[i]-'0'; i++;
    72         }
    73         memset(step,0,sizeof(step));
    74         BFS(loc,pwd);
    75     }
    76     return 0;
    77 }
     
  • 相关阅读:
    哈哈,原来这叫做“松鼠症”……并谈谈我建议的学习方法
    入坑IT十年(二)技术以外
    入坑IT都快十年了
    也来谈谈IT培训
    技术,是不是越新越好?
    十年之后再看“面向对象”
    话说,你这样根本就不应该去“创业型”公司
    我为什么不做外包
    NetTopologySuite Geometry&WKT&WKB&GeoJSON互转
    wpf 打印图片
  • 原文地址:https://www.cnblogs.com/zhaopeng938/p/7919484.html
Copyright © 2011-2022 走看看