/*
    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. (cljs/run-at (JSVM. :browser) "搭建刚好可用的开发环境!")

    前言  书接上一回,在了解cljs基本语法后并在clojurescript.net的奇特错误提示后,我们必须痛定思痛地搭建一个本地的开发环境,以便后续深入地学习cljs. 现有的构建工具  由于浏览器 ...

  2. jeecg项目子窗口获得父窗口元素id

    jeecg项目子窗口获得父窗口元素id, var parentWin = frameElement.api.opener;alert($(parentWin.document).find(" ...

  3. 新安装mysql 第三方工具连接不上问题

    Mysql从客户端连接服务器连不上的问题   公司要用Mysql做一个测试,开始在自己的本地建一个Mysql数据库自己本地的程序再连上去,没有遇到过连接不上的问题.这次数据库在服务器上,从本地客户端连 ...

  4. [leetcode-442-Find All Duplicates in an Array]

    Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...

  5. USACO The Castle

    首先看一下题目. The CastleIOI'94 - Day 1 In a stroke of luck almost beyond imagination, Farmer John was sen ...

  6. 再谈AbstractQueuedSynchronizer:基于AbstractQueuedSynchronizer的并发类实现

    公平模式ReentrantLock实现原理 前面的文章研究了AbstractQueuedSynchronizer的独占锁和共享锁,有了前两篇文章的基础,就可以乘胜追击,看一下基于AbstractQue ...

  7. Win7使用USB口连接H3C交换机的Console口

    使用Console线的一端连接交换机的Console口,另一端连接电脑的USB口. 使用驱动精灵安装USB转串口驱动,我电脑上面提示安装的是: Prolific PL2303 USB转串口驱动1.16 ...

  8. jquery.load问题

    简单Jquery--Ajax应用 作为一个新手,最近自己写了一个人主页,虽然性能不怎么样,但就出现的各种大的小的问题拿出来与大家分享分享. ----DanlV 描述 错误描述 自己不知道什么原因,直接 ...

  9. php与MySQL(php内置mysql函数)

    获取数据 在讲解下面的方法之前,我们首先来看一下我们的数据表的结构: mysqli_fetch_row()函数 mysqli_fetch_row()函数从结果集中取得一行,并以索引数组形式返回. 语法 ...

  10. Python基础之内置函数和递归

    一.内置函数 下面简单介绍几个: 1.abs() 求绝对值 2.all() 如果 iterable 的所有元素都为真(或者如果可迭代为空),则返回 True 3.any() 如果 iterable 的 ...