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): 5355    Accepted Submission(s): 2386


    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
     
    比较水...直接bfs就好.
    妈蛋一直交错题目QAQ
     
      1 /*************************************************************************
      2     > File Name: code/hdu/1195.cpp
      3     > Author: 111qqz
      4     > Email: rkz2013@126.com 
      5     > Created Time: 2015年10月02日 星期五 14时42分21秒
      6  ************************************************************************/
      7 
      8 #include<iostream>
      9 #include<iomanip>
     10 #include<cstdio>
     11 #include<algorithm>
     12 #include<cmath>
     13 #include<cstring>
     14 #include<string>
     15 #include<map>
     16 #include<set>
     17 #include<queue>
     18 #include<vector>
     19 #include<stack>
     20 #include<cctype>
     21 #define yn hez111qqz
     22 #define j1 cute111qqz
     23 using namespace std;
     24 typedef long long LL;
     25 typedef double DB;
     26 const int inf = 0x3f3f3f3f;
     27 int x,y;
     28 bool vis[10005];
     29 struct Q
     30 {
     31     int g,s,b,q;
     32     int d;
     33     
     34 }s,tar;
     35 
     36 int add( int x)
     37 {
     38     int res = 0 ;
     39     res = (x+1);
     40     if (res == 10) res = 1;
     41     return res;
     42 }
     43 int minu( int x)
     44 {
     45     int res = 0 ;
     46     res = (x-1);
     47     if (res==0) res =  9;
     48     return res;
     49 }
     50 
     51 int get(Q a)
     52 {
     53     int res = 0 ;
     54     res = a.q*1000+a.b*100+a.s*10+a.g;
     55     return res;
     56 }
     57 
     58 void bfs()
     59 {
     60     queue<Q>q;
     61     q.push(s);
     62     
     63     while (!q.empty())
     64     {
     65     
     66     Q pre = q.front();q.pop();
     67 //    cout<<get(pre)<<" "<<pre.d<<endl;
     68     if (get(pre)==y)
     69     {
     70         printf("%d
    ",pre.d);
     71      //   cout<<"aaahhh"<<endl;
     72         return;
     73     }
     74     Q nxt = pre;
     75     nxt.d = pre.d + 1;
     76 
     77     nxt.g = add(pre.g);
     78     if (!vis[get(nxt)])
     79     {
     80         vis[get(nxt)] = true;
     81        // nxt.d = pre.d  +1;
     82         q.push(nxt);
     83     }
     84 
     85     nxt.g = minu(pre.g);
     86     if (!vis[get(nxt)])
     87     {
     88        // nxt.d = pre.d  +1;
     89         vis[get(nxt)] =true;
     90         q.push(nxt);
     91     }
     92         
     93     nxt = pre;
     94     nxt.d = pre.d+1;
     95     nxt.s = add(pre.s);    
     96     if (!vis[get(nxt)])
     97     {
     98        // nxt.d = pre.d  +1;
     99         vis[get(nxt)]=true;
    100         q.push(nxt);
    101     }
    102 
    103     nxt.s = minu(pre.s);
    104     if (!vis[get(nxt)])
    105     {
    106     //    nxt.d = pre.d  +1;
    107         vis[get(nxt)] =true;
    108         q.push(nxt);
    109     }
    110     
    111     nxt = pre;
    112     nxt.d = pre.d+1;
    113     nxt.b =add(pre.b);
    114     if (!vis[get(nxt)])
    115     {
    116       //  nxt.d = pre.d  +1;
    117         vis[get(nxt)] = true;
    118         q.push(nxt);
    119     }
    120 
    121     nxt.b =minu(pre.b);
    122     if (!vis[get(nxt)])
    123     {
    124         //nxt.d = pre.d  +1;
    125         vis[get(nxt)] = true;
    126         q.push(nxt);
    127     }
    128     
    129     nxt = pre;
    130     nxt.d = pre.d+1;
    131     nxt.q =add(pre.q);
    132     if (!vis[get(nxt)])
    133     {
    134     //    nxt.d = pre.d  +1;
    135         vis[get(nxt)] = true;
    136         q.push(nxt);
    137     }
    138 
    139     nxt.q = minu(pre.q);
    140     if (!vis[get(nxt)])
    141     {
    142       //  nxt.d = pre.d  +1;
    143         vis[get(nxt)]= true;
    144         q.push(nxt);
    145     }
    146     
    147     nxt = pre;
    148     nxt.d = pre.d+1;
    149     nxt.g = pre.s;
    150     nxt.s = pre.g;
    151     if (!vis[get(nxt)])
    152     {
    153     //    nxt.d = pre.d  +1;
    154         vis[get(nxt)] = true;
    155         q.push(nxt);
    156     }
    157     
    158     nxt = pre;
    159     nxt.d = pre.d+1;
    160     nxt.s = pre.b;
    161     nxt.b = pre.s;
    162     if (!vis[get(nxt)])
    163     {
    164       //  nxt.d = pre.d  +1;
    165         vis[get(nxt)] = true;
    166         q.push(nxt);
    167     }
    168     
    169     nxt = pre;
    170     nxt.d = pre.d+1;
    171     nxt.b = pre.q;
    172     nxt.q = pre.b;
    173     if (!vis[get(nxt)])
    174     {
    175        // nxt.d = pre.d  +1;
    176         vis[get(nxt)] = true;
    177         q.push(nxt);
    178     }    
    179     }
    180 }
    181 int main()
    182 {
    183   #ifndef  ONLINE_JUDGE 
    184    freopen("in.txt","r",stdin);
    185   #endif
    186     int T;
    187     scanf("%d",&T);
    188     while (T--)
    189     {
    190     memset(vis,false,sizeof(vis));
    191     scanf("%d %d",&x,&y);
    192     s.g = x %10;
    193     s.s = x%100/10;
    194     s.b = x/100%10;
    195     s.q = x/1000;
    196     s.d = 0 ;
    197     vis[x] = true;
    198     bfs();
    199 
    200     }
    201    
    202  #ifndef ONLINE_JUDGE  
    203   fclose(stdin);
    204   #endif
    205     return 0;
    206 }
    View Code
  • 相关阅读:
    docker部署nginx文件服务器
    local variable 'a' referenced before assignment
    函数嵌套的顺序
    动态参数
    函数——函数接收不定个数的数字实参,将数字之和返回给调用者
    函数——函数接收四个参数分别是:姓名,性别,年龄,学历。用户通过输入这四个内容,然后将这四个内容传入到函数中, 此函数接收到这四个内容,将内容追加到一个student msg文件中
    函数——此函数只接收一个参数且此参数必须是列表数据类型,此函数完成的功能是返回给调用者一个字典, 此字典的键值对为此列表的索引及对应元素。例如传入的列表为:[11,22,33]返回的字典为{0:11,1:22,2:33}
    函数——检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者
    函数—判断用户传入的对象(字符串、列表、元组)长度是否大于5
    Python中深拷贝与浅拷贝的区别
  • 原文地址:https://www.cnblogs.com/111qqz/p/4852400.html
Copyright © 2011-2022 走看看