/*
    Name: HDU--1195--Open the Lock
    Author: shen_渊
    Date: 17/04/17 08:54
    Description: bfs,用四维数组标记状态,每位数字可以做三种操作 +1,-1,交换
                可以不用优先队列,题意决定,优先队列更费时间
*/

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct node{
    int steps;
    string s;
    node():steps(){
    };
    bool operator <(const node &a)const{
        return steps>a.steps;
    }
};
int bfs();
][][][];
string str1,str2;
int main()
{
    ios::sync_with_stdio(false);
//    freopen("in.txt","r",stdin);
    int n;cin>>n;
    while(n--){
        memset(vis,,sizeof(vis));
        cin>>str1>>str2;
        if(str1 == str2)cout<<"0\n";
        else cout<<bfs()<<endl;
    }
    ;
}
int bfs(){
    priority_queue<node> q;
    node p;
    p.s = str1;
    q.push(p);
    vis[p.s[]-]-]-]-;
    while(!q.empty()){
        node a,temp;
        temp = q.top();q.pop();
        if(temp.s == str2)return temp.steps;
        ; i<; ++i) {
            //+1
            a.s = temp.s;
            ';
            ;
            ]-]-]-]-']){
                a.steps = temp.steps + ;
                q.push(a);
                vis[a.s[]-]-]-]-;
            }
            //-1
            a.s = temp.s;
            ';
            ;
            ]-]-]-]-']){
                a.steps = temp.steps + ;
                q.push(a);
                vis[a.s[]-]-]-]-;
            }
            //交换
            ){
                a.s = temp.s;
                a.s[i] = temp.s[i+];
                a.s[i+] = temp.s[i];
                ]-]-]-]-']){
                a.steps = temp.steps + ;
                q.push(a);
                vis[a.s[]-]-]-]-;
                }
            }
        }
    }
    ;
}

HDU--1195--bfs--Open the Lock的更多相关文章

  1. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  2. hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...

  3. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. hdu 1195 Open the Lock (BFS)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. hdu 1195 Open the Lock

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...

  6. hdu 1195 Open the Lock(广搜,简单)

    题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...

  7. HDU 1195 Open the Lock (双宽搜索)

    意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...

  8. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  9. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  10. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

随机推荐

  1. java架构师负载均衡、高并发、nginx优化、tomcat集群、异步性能优化、Dubbo分布式、Redis持久化、ActiveMQ中间件、Netty互联网、spring大型分布式项目实战视频教程百度网盘

    15套Java架构师详情 * { font-family: "Microsoft YaHei" !important } h1 { background-color: #006; ...

  2. org.apache.commons.lang3 的随机数生成

    apache org.apache.commons.lang3 的随机数生成工具,方便使用. String a12 = RandomStringUtils.random(4, "012345 ...

  3. linux服务器load的含义

    Linux的Load(系统负载),是一个让新手不太容易了解的概念.load的就是一定时间内计算机有多少个active_tasks,也就是说是计算机的任务执行队列的长度,cpu计算的队列. top/up ...

  4. Selenium chrome配置代理Python版

    环境: windows 7 + Python 3.5.2 + Selenium 3.4.2 + Chrome Driver 2.29 + Chrome 58.0.3029.110 (64-bit) S ...

  5. spark streaming(2) DAG静态定义及DStream,DStreamGraph

    DAG 中文名有向无环图.它不是spark独有技术.它是一种编程思想 ,甚至于hadoop阵营里也有运用DAG的技术,比如Tez,Oozie.有意思的是,Tez是从MapReduce的基础上深化而来的 ...

  6. 【Android Developers Training】 89. 最大化的使用谷歌云消息(Google Cloud Messaging)

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. jq-animate

    jq-animate: <!doctype html> <html> <head> <meta charset="utf-8"> & ...

  8. Servlet启动的时机

    Servlet启动的时机有两个:1.在用户第一次请求时:2.在web应用启动之时. 在web.xml 文件中配置 格式 <servlet> <servlet-name>**** ...

  9. 第二章:2.9 总结一下 Django

    1. URLconf(URL configuration ) : 这个模块包含URL模式(正则表达式)到视图函数(view.py)的简单映射. 2. python 正则表达式: 解释: 通配符:r : ...

  10. Unity3D中使用BMFont制作图片字体 (NGUI版)

    [旧博客转移 - 发布于2015年9月10日 16:07] 有时美术会出这种图片格式的文字,NGUI提供了UIFont来支持BMFont导出的图片字体 BMFont原理其实很简单,首先会把文字小图拼成 ...