题目描述

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。

输入输出样例

输入样例#1: 复制

  1. 3
  2. . x A
  3. . . .
  4. B x .
输出样例#1: 复制

  1. 2

说明

【注释】

只可以上下左右四个方向行走,并且不能走出这些格子之外。开始和结束时的方向可以任意。

这里注意跟朴素bfs不同在于

while不停入队和vis打标记

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define inf 2147483647
  5. const ll INF = 0x3f3f3f3f3f3f3f3fll;
  6. #define ri register int
  7. template <class T> inline T min(T a, T b, T c)
  8. {
  9. return min(min(a, b), c);
  10. }
  11. template <class T> inline T max(T a, T b, T c)
  12. {
  13. return max(max(a, b), c);
  14. }
  15. template <class T> inline T min(T a, T b, T c, T d)
  16. {
  17. return min(min(a, b), min(c, d));
  18. }
  19. template <class T> inline T max(T a, T b, T c, T d)
  20. {
  21. return max(max(a, b), max(c, d));
  22. }
  23. #define pi acos(-1)
  24. #define me(x, y) memset(x, y, sizeof(x));
  25. #define For(i, a, b) for (int i = a; i <= b; i++)
  26. #define FFor(i, a, b) for (int i = a; i >= b; i--)
  27. #define mp make_pair
  28. #define pb push_back
  29. const int maxn = ;
  30. #define mod 100003
  31. const int N=;
  32.  
  33. // name*******************************
  34. struct node
  35. {
  36. int x,y;
  37. } p[N];
  38. int dx[]= {,-,,};
  39. int dy[]= {,,,-};
  40. queue<node>que;
  41. bool vis[N][N];
  42. char G[N][N];
  43. int n;
  44. node sx,ex;
  45. int cnt[N][N];
  46. // function******************************
  47. void bfs(node u)
  48. {
  49. que.push(u);
  50. vis[u.x][u.y]=;
  51. while(!que.empty())
  52. {
  53. node t=que.front();
  54. que.pop();
  55. int x=t.x,y=t.y;
  56. For(i,,)
  57. {
  58. int tx=x+dx[i],ty=y+dy[i];
  59. if(tx<||tx>n||ty<||ty>n)continue;
  60. while((G[tx][ty]=='.'||G[tx][ty]=='B'))//vis的判断条件不能加在这里!!就算这点走过了,但这一行或这一列其他点不一定走过!!while需要继续搜
  61. {
  62. if(vis[tx][ty]==)
  63. {
  64. que.push(node{tx,ty});
  65. vis[tx][ty]=;
  66. cnt[tx][ty]=cnt[x][y]+;
  67. }
  68. if(tx==ex.x&&ty==ex.y)
  69. {
  70. cout<<cnt[tx][ty];
  71. exit();
  72. }
  73. tx+=dx[i],ty+=dy[i];
  74. }
  75. }
  76. }
  77. }
  78.  
  79. //***************************************
  80. int main()
  81. {
  82. // freopen("test.txt", "r", stdin);
  83. cin>>n;
  84.  
  85. For(i,,n)
  86. For(j,,n)
  87. {
  88. cin>>G[i][j];
  89. if(G[i][j]=='A')
  90. {
  91. sx.x=i,sx.y=j;
  92. cnt[i][j]=-;
  93. }
  94. if(G[i][j]=='B')
  95. ex.x=i,ex.y=j;
  96. }
  97. bfs(sx);
  98.  
  99. cout<<-;
  100.  
  101. return ;
  102. }

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

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

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

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

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

  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. [USACO07OCT]障碍路线 & yzoj P1130 拐弯 题解

    题意 给出n* n 的图,A为起点,B为终点,* 为障碍,.可以行走,问最少需要拐90度的弯多少次,无法到达输出-1. 解析 思路:构造N * M * 4个点,即将原图的每个点分裂成4个点.其中点(i ...

  8. 2021.10.29 P1649 [USACO07OCT]Obstacle Course S(BFS)

    2021.10.29 P1649 [USACO07OCT]Obstacle Course S(BFS) 题意: 给一张n*n的图,起点为A,终点为 B,求从A到B转弯次数最少为多少. 分析: 是否存在 ...

  9. [洛谷1649]障碍路线<BFS>

    题目链接:https://www.luogu.org/problem/show?pid=1649 历经千辛万苦,我总算是把这个水题AC了,现在心里总觉得一万只草泥马在奔腾: 这是一道很明显的BFS,然 ...

随机推荐

  1. python正则表达式3-模式匹配

    re.S,使 '.'  匹配换行在内的所有字符 >>> pattern=r'ghostwu.com' >>> import re >>> re.f ...

  2. 微信小程序点击图片放大预览

    微信小程序点击图片放大预览使用到 wx.previewImage 接口,可以放大.上/下一张 上代码 wxml代码 <view class='content-img' wx:if="{ ...

  3. JSz中的静态方法和实例方法的分析

    我又回来了,最近忙着喝枸杞,没来写博客感觉很有负罪感,今晚我来写一点小小的知识点 可能我们在用形如Array.of()的方法时会产生一些疑问,为什么我们能不实例化直接使用Array上的of()方法呢, ...

  4. JS 解决 IOS 中拍照图片预览旋转 90度 BUG

    上篇博文[ Js利用Canvas实现图片压缩 ]中做了图片压缩上传,但是在IOS真机测试的时候,发现图片预览的时候自动逆时针旋转了90度.对于这个bug,我完全不知道问题出在哪里,接下来就是面向百度编 ...

  5. SAP FI/CO 基本概念

    每一个SAP从业者都对这些概念不陌生,理解透了这些概念,对SAP的业务体系构架才能有明确地认识. 1.集团(client)的概念:是SAP中的最高等级:每一个集团建立主数据库. 2.公司(Compan ...

  6. PDO预处理语句

    1.造PDO对象$dsn = "mysql:dbname=mydb;host=localhost";$pdo = new PDO($dsn,"root",&qu ...

  7. Python数据类型之list和tuple

    list是一种有序的集合,可以随时添加和删除其中的元素. 用len()函数可以获得list元素的个数. 用索引来访问list中每一个位置的元素,索引是从0开始的.如果要取最后一个元素,除了计算索引位置 ...

  8. 有关 Azure IaaS VM 磁盘以及托管和非托管高级磁盘的常见问题解答

    本文将对有关 Azure 托管磁盘和 Azure 高级存储的一些常见问题进行解答. 托管磁盘 什么是 Azure 托管磁盘? 托管磁盘是一种通过处理存储帐户管理来简化 Azure IaaS VM 的磁 ...

  9. CSS| text文本属性

    注意:一般来说,可以为所有块级元素应用 text-indent,但无法将该属性应用于行内元素,图像之类的替换元素上也无法应用 text-indent 1)  text-indent 取值: 5px/2 ...

  10. UIButton vs UIEventListener 事件处理

    NGUI的事件 在使用NGUI的事件处理时,发现UIButton和UIEventListener之间的共同点越来越多. 当然处理事件,也有一些其它的函数,比如:UIEventTrigger,Butto ...