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 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
随机推荐
- 【转】你真的了解iOS代理设计模式吗?
转自:http://www.cocoachina.com/ios/20160317/15696.html 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递 ...
- (转)从工程中删除Cocoapods
1. 删除工程文件夹下的Podfile.Podfile.lock及Pods文件夹 2. 删除xcworkspace文件 3. 使用xcodeproj文件打开工程,删除Frameworks组下的Pods ...
- codeforces 652B z-sort(思维)
B. z-sort time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- mysql distinct
mysql的DISTINCT的关键字有很多你想不到的用处1.在count 不重复的记录的时候能用到比如SELECT COUNT( DISTINCT id ) FROM tablename;就是计算ta ...
- SQL拆分多规则的字符串分离数字。
--拆分多规则字符串 DECLARE @Codes NVARCHAR(MAX) SET @Codes = '6*BC-007,*BC-016,9*BC-015' --对于*BC-015这种情况,则Qt ...
- JSP/ Servlet常见的中文乱码原因
在开发中,我们经常遇到中文乱码的问题,比方: &浏览器中看到的 Jsp/Servlet 页面中的汉字成了 '?' ? &浏览器中看到的 Servlet 页面中的汉字都成了乱码 &a ...
- SASS优化响应式断点管理
前端开发whqet,csdn,王海庆,whqet,前端开发专家 原文:<Managing Responsive Breakpoints with Sass> 作者:Hugo Giraude ...
- URAL 1777 D - Anindilyakwa 暴力
D - AnindilyakwaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/v ...
- windows command ftp 中文文件名乱码解决方法
有时,使用临时的windows机子,要进行ftp简单操作,但又不想装其它的ftp-client,可以直接使用windows command中的命令ftp来操作. 通常,ftp服务器按标准,使用utf8 ...
- delphi ExecWB
TWebBrowser.ExecWB 关键点 procedure ExecWB(cmdID: OLECMDID; cmdexecopt: OLECMDEXECOPT); overload; 实现过程 ...