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

    Open the Lock
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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
     
     
     
    变相的BFS,交换的次数为层数
    遍历所有交换的可能,计算每种可能下,距离答案的“距离”
     
     
     1 /*
     2 By:OhYee
     3 Github:OhYee
     4 Email:oyohyee@oyohyee.com
     5 Blog:http://www.cnblogs.com/ohyee/
     6 
     7 かしこいかわいい?
     8 エリーチカ!
     9 要写出来Хорошо的代码哦~
    10 */
    11 
    12 #include <cstdio>
    13 #include <algorithm>
    14 #include <cstring>
    15 #include <cmath>
    16 #include <string>
    17 #include <iostream>
    18 #include <vector>
    19 #include <list>
    20 #include <queue>
    21 #include <stack>
    22 #include <map>
    23 using namespace std;
    24 
    25 //DEBUG MODE
    26 #define debug 0
    27 
    28 //循环
    29 #define REP(n) for(int o=0;o<n;o++)
    30 
    31 int del(int a,int b) {
    32     int cnt = 0;
    33     while (a) {
    34         int t1 = (a % 10);
    35         int t2 = (b % 10);
    36         cnt += abs(t1-t2)>4?(9 - abs(t1 - t2)): abs(t1 - t2);
    37         a /= 10;
    38         b /= 10;
    39     }
    40     return cnt;
    41 }
    42 
    43 int Swap(int s,int a,int b) {
    44     int t[4] = {0};
    45     for (int i = 3;i >= 0;i--) {
    46         t[i] = s % 10;
    47         s /= 10;
    48     }
    49     swap(t[a],t[b]);
    50     return t[0] * 1000 + t[1] * 100 + t[2] * 10 + t[3];
    51 }
    52 
    53 int BFS(int s,int v) {
    54     int Min = del(s,v);
    55     bool visited[9999];
    56     memset(visited,false,sizeof(visited));
    57 
    58     queue<pair<int,int> > Q;
    59     Q.push(pair<int,int>(s,0));
    60     visited[s] = true;
    61     while (!Q.empty()) {
    62         int k = Q.front().first;
    63         int n = Q.front().second;
    64         Q.pop();
    65 
    66         for (int i = 0;i < 3;i++) {
    67             int kk = Swap(k,i,i + 1);
    68             int nn = n + 1;
    69             if (visited[kk])
    70                 continue;
    71             visited[kk] = true;
    72             Min = min(nn + del(kk,v),Min);
    73             //printf(" %d %d+%d=%d
    ",kk,nn,del(kk,v),nn + del(kk,v));
    74             Q.push(pair<int,int>(kk,nn));
    75         }
    76     }
    77     return Min;
    78 }
    79 
    80 bool Do() {
    81     int s,v;
    82     scanf("%d%d",&s,&v);
    83 
    84     printf("%d
    ",BFS(s,v));
    85 
    86     return true;
    87 }
    88 
    89 int main() {
    90     int T;
    91     scanf("%d",&T);
    92     while (T--)
    93         Do();
    94     return 0;
    95 }
  • 相关阅读:
    C++中重载、重定义、重写概念辨析
    虚函数,抽象函数
    取出一个int的每一位,用算法
    Typedef和define
    枚举
    基于 ThinkPHP 3.2.3 的页面静态化功能的实现
    骑行在华盛顿 针对320万次共享单车骑行数据的分析
    骑行在华盛顿 针对320万次共享单车骑行数据的分析
    分析了10个垂直行业后,告诉你大数据应用面临哪些挑战
    分析了10个垂直行业后,告诉你大数据应用面临哪些挑战
  • 原文地址:https://www.cnblogs.com/ohyee/p/5428649.html
Copyright © 2011-2022 走看看