题目描述

Consider an N x N (1 <= N <= 100) square field composed of 1

by 1 tiles. Some of these tiles are impassible by cows and are marked with an 'x' in this 5 by 5 field that is challenging to navigate:

  1. . . B x .
  2. . x x A .
  3. . . . x .
  4. . x . . .
  5. . . x . .

Bessie finds herself in one such field at location A and wants to move to location B in order to lick the salt block there. Slow, lumbering creatures like cows do not like to turn and, of course, may only move parallel to the edges of the square field. For a given field, determine the minimum number of ninety degree turns in any path from A to B. The path may begin and end with Bessie facing in any direction. Bessie knows she can get to the salt lick.

N*N(1<=N<=100)方格中,’x’表示不能行走的格子,’.’表示可以行走的格子。卡门很胖,故而不好转弯。现在要从A点走到B点,请问最少要转90度弯几次?

输入格式

第一行一个整数N,下面N行,每行N个字符,只出现字符:’.’,’x’,’A’,’B’,表示上面所说的矩阵格子,每个字符后有一个空格。

【数据规模】

2<=N<=100

输出格式

一个整数:最少转弯次数。如果不能到达,输出-1。

题目分析:普通bfs在跑图时搜点记录的是最短路径(扩展层数),本题要求输出最小转弯次数,我们可以模仿dijkstra+堆优化将转弯数保存在小根堆中然后跑dijkstra, 由于堆顶始终最小,所以在bfs跑到时就能得出我们的答案

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. char a[][];
  4. int book[][];
  5. int flag=;
  6. int nx[]= {,,-,};
  7. int ny[]= {,-,,};
  8. int s1,s2;
  9. int e1,e2;
  10. struct node
  11. {
  12. int px=;
  13. int py=;
  14. int x=;
  15. int y=;
  16. int w=;
  17. friend bool operator < (node a,node b)//小根堆
  18. {
  19. return a.w>b.w;
  20. }
  21. };
  22. int w;
  23. priority_queue<node> q;
  24. int m,n;
  25. void bfs()//dijkstra
  26. {
  27. node fir;
  28. fir.x=s1;
  29. fir.y=s2;
  30. q.push(fir);
  31. book[s1][s2]=;
  32. while(!q.empty())
  33. {
  34. node now=q.top();
  35. q.pop();
  36. book[now.x][now.y]=;
  37. if(now.x==e1&&now.y==e2)
  38. {
  39. //if(now.w<=flag)
  40. {
  41. flag=now.w;
  42. break;
  43. //return;
  44. }
  45. }
  46. for(int i=; i<; i++)
  47. {
  48. int tx=now.x+nx[i];
  49. int ty=now.y+ny[i];
  50. if(tx<||tx>m||ty<||ty>m)
  51. continue;
  52. if(a[tx][ty]=='x')
  53. continue;
  54. if(book[tx][ty]==)
  55. continue;
  56. if((tx-now.x)*(now.x-now.px)+(ty-now.y)*(now.y-now.py)==)//向量判别法
  57. {
  58. node next;
  59. next.x=tx;
  60. next.y=ty;
  61. next.px=now.x;
  62. next.py=now.y;
  63. next.w=now.w+;
  64. q.push(next);
  65.  
  66. }
  67. else
  68. {
  69. node next;
  70. next.x=tx;
  71. next.y=ty;
  72. next.px=now.x;
  73. next.py=now.y;
  74. next.w=now.w;
  75. q.push(next);
  76.  
  77. }
  78. }
  79. }
  80. }
  81. int main()
  82. {
  83. cin>>m;
  84. memset(a,-,sizeof(a));
  85. int x,y;
  86. int z;
  87. for(int i=; i<=m; i++)
  88. for(int j=; j<=m; j++)
  89. {
  90. char c;
  91. cin>>c;
  92. if(c=='A')
  93. {
  94. s1=i;
  95. s2=j;
  96. }
  97. if(c=='B')
  98. {
  99. e1=i;
  100. e2=j;
  101. }
  102. a[i][j]=c;
  103. }
  104. bfs();
  105. if(flag==)
  106. cout<<-;
  107. else
  108. cout<<flag;
  109. return ;
  110. }

洛谷P1649 【[USACO07OCT]障碍路线Obstacle Course】的更多相关文章

  1. 洛谷 P1649 [USACO07OCT]障碍路线Obstacle Course

    P1649 [USACO07OCT]障碍路线Obstacle Course 题目描述 Consider an N x N (1 <= N <= 100) square field comp ...

  2. bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course

    P1649 [USACO07OCT]障碍路线Obstacle Course bfs 直接上个bfs 注意luogu的题目和bzoj有不同(bzoj保证有解,还有输入格式不同). #include< ...

  3. Luogu P1649 [USACO07OCT]障碍路线Obstacle Course

    题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these ti ...

  4. P1649 [USACO07OCT]障碍路线Obstacle Course

    题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these ti ...

  5. [USACO07OCT]障碍路线Obstacle Course

    题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these ti ...

  6. 障碍路线Obstacle Course

    P1649 [USACO07OCT]障碍路线Obstacle Course 裸的dfs,今天学了一个新招,就是在过程中进行最优性减枝. #include<bits/stdc++.h> us ...

  7. 洛谷 P4478 [BJWC2018]上学路线

    洛谷 P4478 [BJWC2018]上学路线 原题 神仙题orz,竟然没有1A....容斥+卢卡斯+crt?? 首先用容斥做,记\(f[i][0/1]\)表示到i号点经过了奇数/偶数个点的方案数,因 ...

  8. 【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线

    P2233 [HNOI2002]公交车路线 题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另 ...

  9. Java实现 洛谷 Car的旅行路线

    输入输出样例 输入样例#1: 1 3 10 1 3 1 1 1 3 3 1 30 2 5 7 4 5 2 1 8 6 8 8 11 6 3 输出样例#1: 47.5 import java.util. ...

随机推荐

  1. springboot + freemarker 实现计算器

    使用RequestParam("") 接受参数 其后参数有自动类型转换的作用 @RequestParam("firstNumber") double first ...

  2. 小小知识点(二十一)如何修改PPT母版上无法直接点击修改的文字

    1. 进入PPT后,选择下图右上角红色圈出的“视图”,接着选择下方红色圈出的“幻灯片母版”: 2.点击进入母版,如下图所示,最上面一栏第一个选项变成了“幻灯片母版”,在下面一栏最右边变成了“关闭母版视 ...

  3. HTTP Strict Transport Security (HSTS) in ASP.NET Core

    本文是<9012年了,还不会Https>的后篇,本文着重聊一聊 HTTP Strict Transport Security协议的概念和应用. 启用HTTPS还不够安全 站点通过HTTPS ...

  4. vue文字间歇无缝向上滚动

    公司的管理系统中有"文字间歇无缝向上滚动"的需求,现在这种需求基本在项目开发中已经消失了,没什么新颖的,但架不住公司高层喜欢这种玩意儿,所以,作为开发人员,即使你有一百个不乐意,谁 ...

  5. 微信小程序之猜拳游戏

    ---恢复内容开始--- 最近几天在学习小程序,看了网上的学习视频,于是自己捣鼓着做出了视频里面的小程序. 这是实现的效果图 一个小程序页面,一般有三个部分文件组成,index.js 这个文件里面放的 ...

  6. C#实现EXCEL表格转DataTable

    C#代码实现把Excel文件转化为DataTable,根据Excel的文件后缀名不同,用不同的方法来进行实现,下面通过根据Excel文件的两种后缀名(*.xlsx和*.xls)分别来实现.获取文件后缀 ...

  7. ubuntu16.04 docker kubernetes(k8s) istio 安装

    版本: docker: 19.03.5 kubernetes: 1.17.0 istio: 1.4.3 步骤一:给ubuntu换源 https://www.cnblogs.com/lfri/p/106 ...

  8. UIBPlayer (视频播放)demo分享

    本文出自APICloud官方论坛 UIBPlayer 封装了百度云播放器 SDK.本模块带有UI方案,打开后为一个具有完整功能的播放器界面.百度云播放器突破 Android.iOS 平台对视频格式的限 ...

  9. Adobe Acrobat DC 安装

    Adobe Acrobat DC 制作pdf模板 下载:http://www.downza.cn/soft/20562.html 安装出错解决: 可以将C:\Program Files (x86)\C ...

  10. git 查看修改账号密码

    git config user.name         查看用户名 git config user.email         查看用户邮箱 修改用户名和邮箱的命令 git config --glo ...