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

    Open the Lock

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 1195
    64-bit integer IO format: %I64d      Java class name: Main
     
     
    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

    Source

     
    解题:bfs...
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 struct node{
    18     int key[4],step;
    19 };
    20 queue<node>q;
    21 set<int>st;
    22 int temp[4],tar[4];
    23 bool transformIt(int *d){
    24     int num = 0;
    25     for(int i = 0; i < 4; i++)
    26         num = num*10 + d[i];
    27     if(st.count(num)) return false;
    28     st.insert(num);
    29     return true;
    30 }
    31 bool ok(int *d){
    32     for(int i = 0; i < 4; i++)
    33         if(d[i] != tar[i]) return false;
    34     return true;
    35 }
    36 int bfs(){
    37     while(!q.empty()) q.pop();
    38     node a,b;
    39     memcpy(a.key,temp,sizeof(temp));
    40     st.clear();
    41     a.step = 0;
    42     q.push(a);
    43     transformIt(a.key);
    44     while(!q.empty()){
    45         b = q.front();
    46         q.pop();
    47         if(ok(b.key)) return b.step;
    48         for(int i = 0; i < 4; i++){
    49             a.step = b.step+1;
    50             memcpy(temp,b.key,sizeof(temp));
    51             if(temp[i] + 1 > 9) temp[i] = 1;
    52             else temp[i]++;
    53             if(transformIt(temp)){
    54                 memcpy(a.key,temp,sizeof(temp));
    55                 q.push(a);
    56             }
    57             memcpy(temp,b.key,sizeof(temp));
    58             if(temp[i]-1 == 0) temp[i] = 9;
    59             else temp[i]--;
    60             if(transformIt(temp)){
    61                 memcpy(a.key,temp,sizeof(temp));
    62                 q.push(a);
    63             }
    64             if(i){
    65                 memcpy(temp,b.key,sizeof(temp));
    66                 swap(temp[i],temp[i-1]);
    67                 if(transformIt(temp)){
    68                     memcpy(a.key,temp,sizeof(temp));
    69                     q.push(a);
    70                 }
    71             }
    72             if(i < 3){
    73                 memcpy(temp,b.key,sizeof(temp));
    74                 swap(temp[i],temp[i+1]);
    75                 if(transformIt(temp)){
    76                     memcpy(a.key,temp,sizeof(temp));
    77                     q.push(a);
    78                 }
    79             }
    80         }
    81     }
    82     return -1;
    83 }
    84 int main(){
    85     int t,i,j;
    86     char md[6];
    87     scanf("%d",&t);
    88     while(t--){
    89         scanf("%s",md);
    90         for(i = 0; i < 4; i++) temp[i] = md[i] - '0';
    91         scanf("%s",md);
    92         for(i = 0; i < 4; i++) tar[i] = md[i] - '0';
    93         printf("%d
    ",bfs());
    94     }
    95     return 0;
    96 }
    View Code

    不用set居然快了这么多。。。。。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 struct node{
    18     int key[4],step;
    19 };
    20 queue<node>q;
    21 int temp[4],tar[4];
    22 bool vis[10000];
    23 bool transformIt(int *d){
    24     int num = 0;
    25     for(int i = 0; i < 4; i++)
    26         num = num*10 + d[i];
    27     if(vis[num]) return false;
    28     vis[num] = true;
    29     return true;
    30 }
    31 bool ok(int *d){
    32     for(int i = 0; i < 4; i++)
    33         if(d[i] != tar[i]) return false;
    34     return true;
    35 }
    36 int bfs(){
    37     while(!q.empty()) q.pop();
    38     node a,b;
    39     memcpy(a.key,temp,sizeof(temp));
    40     memset(vis,false,sizeof(vis));
    41     a.step = 0;
    42     q.push(a);
    43     transformIt(a.key);
    44     while(!q.empty()){
    45         b = q.front();
    46         q.pop();
    47         if(ok(b.key)) return b.step;
    48         for(int i = 0; i < 4; i++){
    49             a.step = b.step+1;
    50             memcpy(temp,b.key,sizeof(temp));
    51             if(temp[i] + 1 > 9) temp[i] = 1;
    52             else temp[i]++;
    53             if(transformIt(temp)){
    54                 memcpy(a.key,temp,sizeof(temp));
    55                 q.push(a);
    56             }
    57             memcpy(temp,b.key,sizeof(temp));
    58             if(temp[i]-1 == 0) temp[i] = 9;
    59             else temp[i]--;
    60             if(transformIt(temp)){
    61                 memcpy(a.key,temp,sizeof(temp));
    62                 q.push(a);
    63             }
    64             if(i){
    65                 memcpy(temp,b.key,sizeof(temp));
    66                 swap(temp[i],temp[i-1]);
    67                 if(transformIt(temp)){
    68                     memcpy(a.key,temp,sizeof(temp));
    69                     q.push(a);
    70                 }
    71             }
    72             if(i < 3){
    73                 memcpy(temp,b.key,sizeof(temp));
    74                 swap(temp[i],temp[i+1]);
    75                 if(transformIt(temp)){
    76                     memcpy(a.key,temp,sizeof(temp));
    77                     q.push(a);
    78                 }
    79             }
    80         }
    81     }
    82     return -1;
    83 }
    84 int main(){
    85     int t,i,j;
    86     char md[6];
    87     scanf("%d",&t);
    88     while(t--){
    89         scanf("%s",md);
    90         for(i = 0; i < 4; i++) temp[i] = md[i] - '0';
    91         scanf("%s",md);
    92         for(i = 0; i < 4; i++) tar[i] = md[i] - '0';
    93         printf("%d
    ",bfs());
    94     }
    95     return 0;
    96 }
    View Code
  • 相关阅读:
    jQuery Mobile方向感应事件
    Linq-多条件查询
    linux top命令详解
    在Python中调用C++,使用SWIG
    linux下core文件调试方法
    如何设置、查看以及调试core文件
    标准C++中的string类的用法总结(转)
    实用make最佳实践
    GDB多进程调试(转)
    GDB详解
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3945570.html
Copyright © 2011-2022 走看看