博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sicily_How can I go
阅读量:6856 次
发布时间:2019-06-26

本文共 1320 字,大约阅读时间需要 4 分钟。

#include
#include
#include
#include
#include
#include
#include
using namespace std;#define LL long longbool vis[1005][1005];int go[3][2]= { { 1,1},{-1,1},{ 0,1}};int n,L[1005],R[1005];struct point{ int x,y,steps,dis; friend bool operator< (point n1, point n2) //优先队列的比较函数,按照steps的大小从小到大排列排列队列中元素 { return n1.steps > n2.steps; } point(int a,int b,int c,int d) { x=a,y=b,steps=c,dis=d; }};int bfs(int hehe){ priority_queue
Q; //优先队列 point f(hehe,0,0,n); Q.push(f); vis[hehe][0]=1; while(!Q.empty()) { point s=Q.top(); Q.pop(); for(int i=0;i<3;++i) { int nx=s.x+go[i][0]; int ny=s.y+go[i][1]; int d=abs(ny-n); if(vis[nx][ny]||nx
L[ny]||abs(ny-n)>=s.dis) //abs(ny-n)>=s.dis剪枝,如果s.dis大于终点和ny的值就不需要加入队列中了 continue; if(ny==n) { return s.steps; //为啥是return s.steps呢,因为有一个方向是(x,y+1),也就是对每个点来说,它的下一个Y坐标必有不需要增加花费的X坐标 } vis[nx][ny]=1; point next(nx,ny,s.steps+(i!=2),d); //i==2的方向是不需要增加1的,其他都是需要增加的 Q.push(next); } } return -1;}int main(){ int t,i,s_x; cin>>t; while(t--) { scanf("%d",&n); for(i=1;i<=n;++i) scanf("%d",&L[i]); for(i=1;i<=n;++i) scanf("%d",&R[i]); memset(vis,0,sizeof(vis)); scanf("%d",&s_x); printf("%d\n",bfs(s_x)); } return 0;}

 

转载于:https://www.cnblogs.com/A-way/archive/2013/04/28/3050049.html

你可能感兴趣的文章
Python(面向对象5——高级)
查看>>
chocolatey使用
查看>>
【转】iOS高级向的十道面试问题
查看>>
了解ES6
查看>>
Maven_2 本地资源库 中央存储库
查看>>
昂贵的聘礼 poj 1062 Dijkstra
查看>>
Hadoop HA的搭建
查看>>
JavaScript实现搜索框效果
查看>>
搭建nginx流媒体服务器(支持HLS)
查看>>
ubuntu 12.04 安装libpcap
查看>>
Android JNI编程提高篇之一
查看>>
SVN TORTOISESVN 使用手则
查看>>
DB2 replace into实现
查看>>
git 基本工作流程
查看>>
NYOJ127 星际之门(一)【定理】
查看>>
input 标签的监听事件总结
查看>>
Java Development Kit(JDK) 8 新特性(简述)
查看>>
龙威零式_团队项目例会记录_8
查看>>
一年做十个项目不如十年做一个项目
查看>>
vim 去掉自动注释和自动回车
查看>>