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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195

    Open the Lock

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


    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
     
     
    BFS可以水过。。。。PE四次,没看清题,囧......据说双向BFS更快,可惜不会。。。。
     
    贴下渣代码,等以后有机会改进下:
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<queue>
     5 #include<cmath>
     6 #include<cstdlib>
     7 using namespace std;
     8 int vis[10000],end;
     9 char start[4];
    10 struct node
    11 {
    12     char num[5];
    13     int time;
    14 };
    15 int convert(char s[])
    16 {
    17     int i,j,sum=0;
    18     for(i=0,j=strlen(s)-1;i<strlen(s);i++,j--)
    19     sum+=(s[i]-48)*pow(10.0,j*1.0);
    20     return sum;
    21 }
    22 void bfs()
    23 {
    24     queue<node>q;
    25     node p,s;
    26     int i,temp;
    27     char sh;
    28     memset(vis,0,sizeof(vis));
    29     p.time=0;
    30     strcpy(p.num,start);
    31     q.push(p);
    32     while(!q.empty())
    33     {
    34         p=q.front();
    35         q.pop();
    36         if(convert(p.num)==end)//strcpy(p.num,end)
    37         {
    38             printf("%d\n",p.time);
    39             return ;
    40         }
    41         for(i=0;i<4;i++)
    42         {
    43             strcpy(s.num,p.num);
    44             if(s.num[i]<'9'&&s.num[i]>='1')s.num[i]+=1;
    45             else if(s.num[i]=='9')s.num[i]='1';
    46             s.num[4]='\0';
    47             temp=convert(s.num);
    48             if(!vis[temp])
    49             {
    50                 vis[temp]=1;
    51                 s.time=p.time+1;
    52                 q.push(s);
    53             }
    54             strcpy(s.num,p.num);
    55             if(s.num[i]<='9'&&s.num[i]>'1')s.num[i]-=1;
    56             else if(s.num[i]=='1')s.num[i]='9';
    57             s.num[4]='\0';
    58             temp=convert(s.num);
    59             if(!vis[temp])
    60             {
    61                 vis[temp]=1;
    62                 s.time=p.time+1;
    63                 q.push(s);
    64             }
    65             if(i!=3)
    66             {
    67                 strcpy(s.num,p.num);
    68                 sh=s.num[i];
    69                 s.num[i]=s.num[i+1];
    70                 s.num[i+1]=sh;
    71                 s.num[4]='\0';
    72                 temp=convert(s.num);
    73                 if(!vis[temp])
    74                 {
    75                     vis[temp]=1;
    76                     s.time=p.time+1;
    77                     q.push(s);
    78                 }
    79             }
    80         }
    81     }
    82 }
    83 int main()
    84 {
    85     int t;
    86     cin>>t;
    87     while(t--)
    88     {
    89         cin>>start>>end;
    90         getchar();
    91         bfs();
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    前端 network
    C语言的安装及使用
    c语言
    mongodb
    大型网站--负载均衡架构
    双机热备ROSE HA工作原理
    Linux vmstat命令实战详解
    管理员必备的20个Linux系统监控工具
    linux top命令详解
    linux命令TOP参数load average详解[转]
  • 原文地址:https://www.cnblogs.com/lfeng/p/3048105.html
Copyright © 2011-2022 走看看