题目描述

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:

. . B x .
. x x A .
. . . x .
. x . . .
. . 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: 复制

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

2

说明

【注释】

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

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

while不停入队和vis打标记

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
return max(max(a, b), max(c, d));
}
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
const int maxn = ;
#define mod 100003
const int N=; // name*******************************
struct node
{
int x,y;
} p[N];
int dx[]= {,-,,};
int dy[]= {,,,-};
queue<node>que;
bool vis[N][N];
char G[N][N];
int n;
node sx,ex;
int cnt[N][N];
// function******************************
void bfs(node u)
{
que.push(u);
vis[u.x][u.y]=;
while(!que.empty())
{
node t=que.front();
que.pop();
int x=t.x,y=t.y;
For(i,,)
{
int tx=x+dx[i],ty=y+dy[i];
if(tx<||tx>n||ty<||ty>n)continue;
while((G[tx][ty]=='.'||G[tx][ty]=='B'))//vis的判断条件不能加在这里!!就算这点走过了,但这一行或这一列其他点不一定走过!!while需要继续搜
{
if(vis[tx][ty]==)
{
que.push(node{tx,ty});
vis[tx][ty]=;
cnt[tx][ty]=cnt[x][y]+;
}
if(tx==ex.x&&ty==ex.y)
{
cout<<cnt[tx][ty];
exit();
}
tx+=dx[i],ty+=dy[i];
}
}
}
} //***************************************
int main()
{
// freopen("test.txt", "r", stdin);
cin>>n; For(i,,n)
For(j,,n)
{
cin>>G[i][j];
if(G[i][j]=='A')
{
sx.x=i,sx.y=j;
cnt[i][j]=-;
}
if(G[i][j]=='B')
ex.x=i,ex.y=j;
}
bfs(sx); cout<<-; return ;
}

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. canvas绘画交叉波浪

    做个记录,自己写的动态效果,可能以后用的着呢: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  2. ActiveReports 报表应用教程 (14)---数据可视化

    葡萄城ActiveReports报表中提供了丰富的数据可视化解决方案,用户可以将数据以图像化的方式进行显示,让报表数据更加形象且便于理解.在葡萄城ActiveReports报表中提供了大多数常用的二维 ...

  3. chrome浏览器使用chrome://inspect调试app 网页,打开空白的问题

    使用chrome浏览器,输入chrome://inspect可以调试android app里面的网页,如果inspect的时候,是空白, 问题截图: 那就在C:\Windows\System32\dr ...

  4. Python技巧——list与字符串互相转换

    Python技巧——list与字符串互相转换   在Python的编程中,经常会涉及到字符串与list之间的转换问题,下面就将两者之间的转换做一个梳理. 1.字符串转换成list 命令:list() ...

  5. java基础知识文章汇总

    将之前的所有关于Java基础知识的随笔,整理成质量较高的十几篇随笔,几乎是好几篇比较零散的随笔合成现在的一篇,自认为还不错. java基础(一) 深入解析基本类型 java基础(二) 自增自减与贪心规 ...

  6. 50家硅谷IT公司技术博客

    分享一下 50 家硅谷优秀 IT 公司技术博客,从中可以了解企业文化,技术特色和设计语言,如果直接列出来很单调,加上点评,算吐槽版吧. 知名大厂   1. Facebook https://www.f ...

  7. 【转】Linux思维导图

    [原文]https://www.toutiao.com/i6591690511763898888/ 1.Linux学习路径: 2.Linux桌面介绍: 3.FHS(文件系统目录标准): 4.Linux ...

  8. fatal: cannot create directoryxxxx': Invalid argument

    问题:fatal: cannot create directoryxxxx': Invalid argument 环境:git 拉取远程仓库的代码后 原因:同事是mac,这个文件夹命名规则在苹果上没有 ...

  9. yum 私有仓库

    参考地址:https://blog.oldboyedu.com/autodeploy-yum YUM主要用于自动安装.升级rpm软件包,它能自动查找并解决rpm包之间的依赖关系.要成功的使用YUM工具 ...

  10. [luogu P4230]连环病原体

    [luogu P4230] 连环病原体 题意 给定一个长度为 \(n\) 的边序列, 当这个序列的一个子区间内的边都加入图中时产生了环则称其为"加强区间", 求序列中的每条边在多少 ...