这题我们可以用优先队列,每次弹出队列中操作次数最少的一个,那么当找到匹配数时,该值一定是最优的。需要注意的时,加个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. DataSnap与FireDAC三层

    相交资料: http://blog.csdn.net/shuaihj/article/details/6129131http://www.cnblogs.com/hnxxcxg/p/4007876.h ...

  2. Spring AOP Example – Advice

    Spring AOP + AspectJ Using AspectJ is more flexible and powerful. Spring AOP (Aspect-oriented progra ...

  3. codeforces 631A Interview

    A. Interview time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. wordpress后台打开慢/卡顿的解决方法

    ---------------------2014年12月29日更新--------------------- 我已经用下面提到的第三种方法禁用了谷歌字体了,最近wordpress后台还是莫名奇妙地非 ...

  5. 【Linux常用工具】02. 创建启动定时任务工具cron

    一. cron 1. cron是一个守护程序,它提供定时器的功能,让用户在特定的时间得以执行默认的指令或程序.只要用户会编辑定时器的设置文件,就可以使用定时器的功能. 定时器文件格式: 2. cron ...

  6. opennebula虚拟机启动失败

    问题1 故障现象: Wed Jan :: [DiM][I]: New VM state is ACTIVE. Wed Jan :: [LCM][I]: New VM state is PROLOG. ...

  7. php redis安装

    一.redis安装 1 下载redis安装包 wget http://redis.googlecode.com/files/redis-2.4.17.tar.gz (若无法下载请手动下载) 2 编译安 ...

  8. 手机调用系统的拍照和裁剪功能,假设界面有输入框EditText,在一些手机会出现点击EditText会弹出输入法,却不能输入的情况。

    1. 拍照裁剪后 点击EditText会弹出输入法,却不能输入.可是点击点一EdtiText就能够输入了,所以我就写了一个看不见的EdtiText,切换焦点,这样就攻克了这个奇怪的这问题,应该是and ...

  9. C# 手动读写app config 的源码

    public class ConfigOperator { public string strFileName; public string configName; public string con ...

  10. 使用sql生成UUID

    在SQLServer中使用该sql语句可以生成GUID:select cast(NEWID() as varchar(36)) as uuid 通过一下语句将GUID中的'-'字符去掉: select ...