hdu 1195 广度搜索
这题我们可以用优先队列,每次弹出队列中操作次数最少的一个,那么当找到匹配数时,该值一定是最优的。需要注意的时,加个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 广度搜索的更多相关文章
- 八数码问题:C++广度搜索实现
毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...
- 记录----第一次使用BFS(广度搜索)学习经验总结
学习经验记录与分享—— 最近在学习中接触到了一种解决最短路径的实用方法----BFS(广度搜索),在这里总结并分享一下第一次学习的经验. 首先第一个要了解的是"queue"(队列函 ...
- Leetcode 课程表 C++ 图的深度搜索和广度搜索练习
广度搜索(degree) struct GraphNode{ int label; vector<GraphNode*> neighbours; GraphNode(int x):labe ...
- HDU 1195 Open the Lock (双宽搜索)
意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
- hdu 1195(搜索)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1195 Open the Lock
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...
- 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: ...
- HDU 4499.Cannon 搜索
Cannon Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
随机推荐
- angular 管理后台
http://blog.csdn.net/iamnieo/article/details/50474399
- Python 3.2: 使用pymysql连接Mysql
在python 3.2 中连接MYSQL的方式有很多种,例如使用mysqldb,pymysql.本文主要介绍使用Pymysql连接MYSQL的步骤 1 安装pymysql · ...
- Castle框架中的IOC和AOP机制
反转控制(IOC)和面向切面编程(AOP)技术作为当前比较流行的技术,其优势已受到广泛关注,但是这两项新技术在实际项目上的应用研究却很落后,而且在.NET平台下实现这两项技术没有形成可以广泛套用的框架 ...
- UVA12219
//by Rujia Liu /* 字符串的对比是缓慢的. 鉴于这道题最多只有四个小写字母, 也就是最多26*4种情况, 我们完全可以用整数来代替字符串. 一种比较简单的做法是把字符串看成一个四位的2 ...
- mysql 报错之创建自定义函数
I experienced this error while trying to alter one of my stored procedures remotely on a master serv ...
- MVC 中WebViewPage的运用
MVC在View的最后处理中是将View的文件页面编译成一个类,这个类必须继承自WebViewPage,WebViewPage默认添加对AjaxHelper和HtmlHelper的支持 public ...
- 安装Loopback网卡/回环网卡
$CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\' ...
- Playing with ptrace, Part I
X86_64 的 Redhat / Centos / Scientific 下面,若要编译.运行32位程序,需要安装以下包: yum install libgcc.i686 yum install g ...
- Linux 引导过程内幕
转载:http://www.ibm.com/developerworks/cn/linux/l-linuxboot/index.html 从主引导记录到第一个用户空间应用程序的指导 引导 Linu ...
- JAVA基础之StringBuilder基础方法
StringBuilder sb = new StringBulder("kikikiki"); sb.apend("123") //追加 输出kikik ...