zoukankan      html  css  js  c++  java
  • hdu 1195(搜索)

    Open the Lock

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


    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
     
    这种类型做的比较少,还是挺有收获的。
    对每一位进行标记,然后对每种操作进行处理。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<math.h>
    #include<queue>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    bool vis[10][10][10][10];
    struct Node{
        int v[4];
        int step;
    };
    Node s,t;
    bool _equal(Node a,Node b){
        for(int i=0;i<4;i++){
            if(a.v[i]!=b.v[i]) return false;
        }
        return true;
    }
    Node operate(int x,Node now){
        Node next;
        for(int i=0;i<4;i++){
            next.v[i] = now.v[i];
        }
        next.step=now.step+1;
        if(x<4){ ///+
            if(now.v[x]==9) next.v[x]=1;
            else next.v[x]=now.v[x]+1;
        }else if(x<8){ ///-
            if(now.v[x%4]==1) next.v[x%4]=9;
            else next.v[x%4]=now.v[x%4]-1;
        }else{ ///exchange
            int a = now.v[x%4];
            int b = now.v[x%4+1];
            next.v[x%4]=b;
            next.v[x%4+1] = a;
        }
        return next;
    }
    int bfs(Node s){
        memset(vis,false,sizeof(vis));
        queue <Node>q;
        vis[s.v[0]][s.v[1]][s.v[2]][s.v[3]]=true;
        q.push(s);
        s.step = 0;
        while(!q.empty()){
            Node now = q.front();
            q.pop();
            if(_equal(now,t)){
                return now.step;
            }
            for(int i=0;i<11;i++){ ///总共11种操作,[1-4]+ [1-4]- exwchange[1,2][2,3][3,4]
                Node next=operate(i,now);
                if(vis[next.v[0]][next.v[1]][next.v[2]][next.v[3]]==false){
                    vis[next.v[0]][next.v[1]][next.v[2]][next.v[3]]=true;
                    q.push(next);
                }
            }
        }
        return -1;
    }
    int main()
    {
        char s1[5],s2[5];
        int tcase;
        scanf("%d",&tcase);
        while(tcase--){
    
            scanf("%s",s1);
            scanf("%s",s2);
            for(int i=0;i<4;i++){
                s.v[i]=s1[i]-'0';
                t.v[i]=s2[i]-'0';
            }
            int res = bfs(s);
            printf("%d
    ",res);
        }
    
        return 0;
    }
  • 相关阅读:
    ionic + cordova+angularJs 搭建的H5 App完整版总结
    在DevExpress程序中使用GridView直接录入数据的时候,增加列表选择的功能
    【Web动画】SVG 线条动画入门
    闲来无聊,研究一下Web服务器 的源程序
    PHP实现RTX发送消息提醒
    关于AngularJS(1)
    项目总结12:bootstrap-select下拉框模糊搜索
    JAVA读取XML文件并解析获取元素、属性值、子元素信息
    项目总结11:Centos部署JDK+Tomcat+MySQL文档(阿里云-网易云-华为云)
    项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5572829.html
Copyright © 2011-2022 走看看