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): 3388    Accepted Submission(s): 1499


    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:  1175 1072 1026 1180 1044 
     
     1 //15MS    340K    1607 B    C++
     2 /*
     3 
     4     题意:
     5         给出两个四位数,有两种操作,相邻交换或每位+/-1;
     6         求最少步数使第一串变为第二串
     7         
     8     bfs:
     9         细心点就可以过,先考虑每一次最多可以有多少步可以走,因为只有四位数,
    10     而且有两种操作,一共有七步,相邻交换有三步,每位操作有四步,知道这个然后就常规的
    11     bfs求解。 
    12 
    13 */
    14 #include<iostream>
    15 #include<queue>
    16 using namespace std;
    17 struct node{
    18     int a,cnt;
    19 };
    20 int a,b;
    21 int vis[10005];
    22 int bfs()
    23 {
    24     memset(vis,0,sizeof(vis));
    25     queue<node>Q;
    26     node t={a,0};
    27     vis[a]=1;
    28     Q.push(t);
    29     while(!Q.empty()){
    30         t=Q.front();
    31         Q.pop();
    32         if(t.a==b) return t.cnt;
    33         node tt=t;
    34         tt.cnt++;
    35         tt.a=t.a%100+(t.a/1000)*100+(t.a%1000)/100*1000;
    36         if(!vis[tt.a]){
    37             Q.push(tt);vis[tt.a]=1;
    38             //printf("1*%d
    ",tt.a);
    39         }
    40         tt.a=t.a/1000*1000+(t.a%1000)/100*10+(t.a%100)/10*100+t.a%10;
    41         if(!vis[tt.a]){
    42             Q.push(tt);vis[tt.a]=1;
    43              //printf("2*%d
    ",tt.a);
    44         }
    45         tt.a=t.a/100*100+(t.a%10)*10+(t.a%100)/10;
    46         if(!vis[tt.a]){
    47             Q.push(tt);vis[tt.a]=1;
    48              //printf("3*%d
    ",tt.a);
    49         }
    50         
    51         for(int i=1;i<1001;i*=10){
    52             int temp=(t.a%(i*10))/i;
    53             if(temp==9) tt.a=t.a-8*i;
    54             else tt.a=t.a+i;
    55             if(!vis[tt.a]){ 
    56                 Q.push(tt);vis[tt.a]=1;
    57                 //printf("4*%d
    ",tt.a);
    58             }
    59             if(temp==1) tt.a=t.a+8*i;
    60             else tt.a=t.a-i;
    61             if(!vis[tt.a]){ 
    62                 Q.push(tt);vis[tt.a]=1;
    63                 //printf("5*%d
    ",tt.a);
    64             }
    65         }
    66         if(t.a<1000) return 0;
    67     }
    68     return 0; 
    69 }
    70 int main(void)
    71 {
    72     int t; 
    73     scanf("%d",&t);
    74     while(t--)
    75     { 
    76         scanf("%d%d",&a,&b);
    77         printf("%d
    ",bfs());         
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    Why to define my own blog skin
    安装drupal7.7
    同步和异步的区别
    神马是云计算神马是物联网
    zen主题安装图文记录
    《那些年啊,那些事——一个程序员的奋斗史》——127
    《一个程序员的奋斗史》帮我选封面哇! —— 猜封面页数赢赠书活动~
    《那些年啊,那些事——一个程序员的奋斗史》——126
    《那些年啊,那些事——一个程序员的奋斗史》——128 (终章)
    伍定轩乱语
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3474229.html
Copyright © 2011-2022 走看看