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 }
  • 相关阅读:
    C#异步编程
    CentOS7下zip解压和unzip压缩文件
    mongodb 按配置文件mongodb.conf启动
    Maven镜像更换为阿里云中央仓库
    Vue项目碰到"‘webpack-dev-server’不是内部或外部命令,也不是可运行的程序或批处理文件"报错
    mysql启动报错cannot allocate memory for the buffer pool处理
    Spring boot Unable to start embedded Tomcat报错 java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()
    免费桌面视频录像工具OBS的简单操作介绍
    CentOS7下zip解压和unzip压缩文件
    ElasticSearch客户端注解使用介绍
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3474229.html
Copyright © 2011-2022 走看看