这题我们可以用优先队列,每次弹出队列中操作次数最少的一个,那么当找到匹配数时,该值一定是最优的。需要注意的时,加个vi[]数组,判读当前数是否已经存在于队列中。我做的很烦啊~~~

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
int n,m;
int vi[];
struct Point{
int n,num;
Point(int x,int y)
{
vi[x]=;
n=x;
num=y;
}
int operator <(const Point &temp) const
{
return num>temp.num;
}
}p(,);
priority_queue<Point> q;
int bfs()
{
if(n==m)
return ;
while(!q.empty())
q.pop();
memset(vi,,sizeof(vi));
p.n=n,p.num=;
q.push(p);
int a,b,c,d;
while(!q.empty())
{
p=q.top();
q.pop();
if(p.n==m)
return p.num;
//cout<<p.n<<" "<<p.num<<endl;
a=p.n/;
b=(p.n/)%;
c=(p.n%)/;
d=p.n%;
//cout<<p.n<<endl;
//cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
if(a==)
{
if(!vi[+b*+c*+d])
q.push(Point(+b*+c*+d,p.num+));
}
else
if(!vi[p.n+])
q.push(Point(p.n+,p.num+));
if(b==)
{
if(!vi[a*++c*+d])
q.push(Point(a*++c*+d,p.num+));
}
else
if(!vi[p.n+])
q.push(Point(p.n+,p.num+));
if(c==)
{
if(!vi[a*+b*++d])
q.push(Point(a*+b*++d,p.num+));
}
else
if(!vi[p.n+])
q.push(Point(p.n+,p.num+));
if(d==)
{
if(!vi[a*+b*+c*+])
q.push(Point(a*+b*+c*+,p.num+));
}
else
if(!vi[p.n+])
q.push(Point(p.n+,p.num+));
if(a==)
{
if(!vi[+b*+c*+d])
q.push(Point(+b*+c*+d,p.num+));
}
else
if(!vi[p.n-])
q.push(Point(p.n-,p.num+));
if(b==)
{
if(!vi[a*++c*+d])
q.push(Point(a*++c*+d,p.num+));
}
else
if(!vi[p.n-])
q.push(Point(p.n-,p.num+));
if(c==)
{
if(!vi[a*+b*++d])
q.push(Point(a*+b*++d,p.num+));
}
else
if(!vi[p.n-])
q.push(Point(p.n-,p.num+));
if(d==)
{
if(!vi[a*+b*+c*+])
q.push(Point(a*+b*+c*+,p.num+));
}
else
if(!vi[p.n-])
q.push(Point(p.n-,p.num+));
if(!vi[b*+a*+c*+d])
q.push(Point(b*+a*+c*+d,p.num+));
if(!vi[a*+c*+b*+d])
q.push(Point(a*+c*+b*+d,p.num+));
if(!vi[a*+b*+d*+c])
q.push(Point(a*+b*+d*+c,p.num+));
}
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
printf("%d\n",bfs());
}
return ;
}

hdu 1195 广度搜索的更多相关文章

  1. 八数码问题:C++广度搜索实现

    毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...

  2. 记录----第一次使用BFS(广度搜索)学习经验总结

    学习经验记录与分享—— 最近在学习中接触到了一种解决最短路径的实用方法----BFS(广度搜索),在这里总结并分享一下第一次学习的经验. 首先第一个要了解的是"queue"(队列函 ...

  3. Leetcode 课程表 C++ 图的深度搜索和广度搜索练习

    广度搜索(degree) struct GraphNode{ int label; vector<GraphNode*> neighbours; GraphNode(int x):labe ...

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

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

  5. hdu 1195(搜索)

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

  6. hdu 1195 Open the Lock

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

  7. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  8. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

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

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

随机推荐

  1. 浏览器插件-ActiveX

    浏览器插件:B/S模式下通过在客户端浏览器安装插件调用外设或者处理特殊格式数据. 常用插件有身份证阅读器.sim卡阅读器.银行卡校验插件.手写板插件.小键盘插件: 处理表格数据的华表插件.图片合成插件 ...

  2. thymeleaf的属性优先级

    所有Thymeleaf属性定义一个数字优先,建立他们的顺序执行的标签.这个顺序是: Order Feature Attributes 1 Fragment inclusion th:includeth ...

  3. FZU 2082 过路费 (树链剖分 修改单边权)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2082 树链剖分模版题,求和,修改单边权. #include <iostream> #include ...

  4. Unity3D之Mecanim动画系统学习笔记(一):认识Mecanim动画系统

    Mecanim简介 Mecanim动画系统是Unity3D4.0开始引入的一套全新的动画系统,主要提供了下面4个方面的功能: 针对人形角色提供一套特殊的工作流. 动画重定向的能力,可以非常方便的把动画 ...

  5. ASP获取json天气信息

    ASP代码(ASP获取页面源码方法,有编码.超时时间参数,处理了乱码.超时的问题): Function GetHttpPage(HttpUrl) Then GetHttpPage="$Fal ...

  6. 存储过程[st_MES_RptInspectShipment]

    USE [ChangHong_612]GO/****** Object: StoredProcedure [dbo].[st_MES_RptInspectShipment] Script Date: ...

  7. Codeforces Testing Round #12 C. Subsequences 树状数组维护DP

    C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  8. C++ Button右键弹出式菜单

    Button右键弹出式菜单 关键点 用类来实现 的 实现过程 新建1个类  类名CButtonPopMenu 基类CButton 新建1个菜单资源 IDR_MENU1 // ButtonPopMenu ...

  9. [React] Styling React Components With Aphrodite

    Aphrodite is a library styling React components. You get all the benefits of inline styles (encapsul ...

  10. android广播集合,intent,action

    android.permission.ACCESS_CHECKIN_PROPERTIES 同意读写訪问"properties"表在checkin数据库中,改值能够改动上传( All ...