博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 11573(bfs)
阅读量:6857 次
发布时间:2019-06-26

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

题意:给一个方阵表示一个湖每个格子代表那个位置的风向,顺风走不要花费否则花费为1。给你起点重点问你最小花费是多少?

思路:这道题写的四不像,先spfa写了一下T,然后bfs又T。接着加了个优先队列就过了。。。主要就是类似于spfa dis数组记录到那点的最小花费。然后只要能更新就入队。

代码如下:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #define PB(a) push_back(a)13 14 using namespace std;15 16 typedef long long ll;17 typedef pair
pii;18 typedef pair
puu;19 typedef pair
pid;20 typedef pair
pli;21 typedef pair
pil;22 23 const int INF = 0x3f3f3f3f;24 const double eps = 1e-6;25 const int LEN = 1002;26 int Map[LEN][LEN], n, m, dis[LEN][LEN];27 int xx[] = {-1,-1, 0, 1, 1, 1, 0,-1};28 int yy[] = { 0, 1, 1, 1, 0,-1,-1,-1};29 struct P { int x, y, st;};30 struct cmp31 {32 bool operator() (P a, P b){ return a.st>b.st;}33 };34 35 P temp;36 37 int bfs(int x, int y, int ex, int ey)38 {39 temp.x = x;temp.y = y;temp.st = 0;40 priority_queue
, cmp> q;41 int ret = INF;42 q.push(temp);43 register int tx, ty, i;44 memset(dis, 0x3f, sizeof dis);45 dis[x][y] = 0;46 while(!q.empty()){47 P vex = q.top();q.pop();48 if(vex.x==ex && vex.y==ey) { return vex.st;}49 for(i=0; i<8; i++){50 tx = vex.x+xx[i];51 ty = vex.y+yy[i];52 temp.st = vex.st+(i==Map[vex.x][vex.y]?0:1);53 if(tx>=0 && tx
=0 && ty
temp.st){54 temp.x = tx;temp.y = ty;55 dis[tx][ty] = temp.st;56 q.push(temp);57 }58 }59 }60 }61 62 void read(int &ret)63 {64 char c;65 while((c = getchar())<'0' || c>'9');66 ret = 0;67 while(c>='0' && c<='9'){68 ret = ret*10+(c-'0');69 c = getchar();70 }71 }72 73 int main()74 {75 // freopen("in.txt", "r", stdin);76 77 int q, sx, sy, ex, ey, ans;78 while(scanf("%d%d", &n, &m)!=EOF){79 getchar();80 for(int i=0; i
View Code

 

转载于:https://www.cnblogs.com/shu-xiaohao/p/3533870.html

你可能感兴趣的文章
<转载>构造函数与拷贝构造函数
查看>>
[转]K近邻算法
查看>>
表单元素01
查看>>
React Native项目Xcode打包发布iOS问题
查看>>
JPress v1.0-rc2 发布,新增微信小程序 SDK
查看>>
Confluence 6 为搜索引擎隐藏外部链接
查看>>
Python Mysql 数据库操作
查看>>
iOS Autolayout 介绍 2 Interface Builder 技巧
查看>>
打卡加虐狗
查看>>
Springboot + swagger2 通过实体对象封装形式上传视频或者图片问题解决
查看>>
Confluence 5 中如何快速创建一个 JIRA Ticket
查看>>
TP5搭建虚拟主机详细步骤
查看>>
为什么我们做分布式使用Redis?
查看>>
【4opencv】求解向量和轮廓的交点
查看>>
一次邮件发送协议SMTP问题排查
查看>>
BugkuCTF 文件上传测试
查看>>
7- OpenCV+TensorFlow 入门人工智能图像处理-彩色反转&边缘检测
查看>>
不同地域的内容偏好性分析
查看>>
JPA @Column 字段命名 默认驼峰转换
查看>>
作者谈《阿里巴巴Java开发手册(规约)》背后的故事
查看>>