题目描述

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. Linux 安装mysql,mariadb,mysql主从同步

    myariadb安装 centos7 mariadb的学习 在企业里面,多半不会使用阿里云的mariadb版本,因为版本太低,安全性太低,公司会配置myariadb官方的yum仓库 1.手动创建mar ...

  2. JavaScript--动态加载脚本和样式(23)

    一 动态脚本 // 当网站需求变大,脚本的需求也逐步变大;我们不得不引入太多的JS脚本而降低了整站的性能; // 所以就出现了动态脚本的概念,在适时的时候加载相应的脚本; 1.动态引入js文件 var ...

  3. Angular4.+ ngx-bootstrap Pagination 自定义分页组件

    Angular4 随笔(二)  ——自定义分页组件 1.简介 本组件主要是实现了分页组件显示功能,通过使用 ngx-bootstrap Pagination分页组件实现. 基本逻辑: 1.创建一个分页 ...

  4. JS 提升 p4

    提示不多说,记住几个要点: 1.变量和函数都会提升,如下 a = 2; var a ; console.log(a); fn(); function fn(){ console.log(1); } 2 ...

  5. layui点击弹框页面 表单请求

    $("#addSite").click(function () { layer.open({ title: '添加站点', type: 1, area: ['700px', '40 ...

  6. JS实现自定义排序

    定义:用本地特定的顺序来比较两个字符串. 语法:stringObject.localeCompare(target) 参数:target——要以本地特定的顺序与 stringObject 进行比较的字 ...

  7. 前端开发笔记(3)css基础(中)

    上一篇中我们学习了html的标准文档流,下面我们先来看看如何脱离标准流. 脱离标准流 css中一共有三种方法脱离标准流 浮动 绝对定位 固定定位 浮动 我们要搞清楚什么是浮动,先来看一个标准文档流的例 ...

  8. Android 使用RecyclerView SnapHelper详解

    简介 RecyclerView在24.2.0版本中新增了SnapHelper这个辅助类,用于辅助RecyclerView在滚动结束时将Item对齐到某个位置.特别是列表横向滑动时,很多时候不会让列表滑 ...

  9. 学习vue.js的正确姿势(转载)

    最近饶有兴致的又把最新版 Vue.js 的源码学习了一下,觉得真心不错,个人觉得 Vue.js 的代码非常之优雅而且精辟,作者本身可能无 (bu) 意 (xie) 提及这些.那么,就让我来吧:) 程序 ...

  10. LeetCode题解之Unique Email Addresses

    1.题目描述 2.问题分析 将字符串中的 ‘.’ 去掉,将 ‘+’后面直到‘@’的字符串去掉,然后利用set的特性. 3.代码 int numUniqueEmails(vector<string ...