题目描述

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。

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

#include <bits/stdc++.h>
using namespace std;
char a[][];
int book[][];
int flag=;
int nx[]= {,,-,};
int ny[]= {,-,,};
int s1,s2;
int e1,e2;
struct node
{
int px=;
int py=;
int x=;
int y=;
int w=;
friend bool operator < (node a,node b)//小根堆
{
return a.w>b.w;
}
};
int w;
priority_queue<node> q;
int m,n;
void bfs()//dijkstra
{
node fir;
fir.x=s1;
fir.y=s2;
q.push(fir);
book[s1][s2]=;
while(!q.empty())
{
node now=q.top();
q.pop();
book[now.x][now.y]=;
if(now.x==e1&&now.y==e2)
{
//if(now.w<=flag)
{
flag=now.w;
break;
//return;
}
}
for(int i=; i<; i++)
{
int tx=now.x+nx[i];
int ty=now.y+ny[i];
if(tx<||tx>m||ty<||ty>m)
continue;
if(a[tx][ty]=='x')
continue;
if(book[tx][ty]==)
continue;
if((tx-now.x)*(now.x-now.px)+(ty-now.y)*(now.y-now.py)==)//向量判别法
{
node next;
next.x=tx;
next.y=ty;
next.px=now.x;
next.py=now.y;
next.w=now.w+;
q.push(next); }
else
{
node next;
next.x=tx;
next.y=ty;
next.px=now.x;
next.py=now.y;
next.w=now.w;
q.push(next); }
}
}
}
int main()
{
cin>>m;
memset(a,-,sizeof(a));
int x,y;
int z;
for(int i=; i<=m; i++)
for(int j=; j<=m; j++)
{
char c;
cin>>c;
if(c=='A')
{
s1=i;
s2=j;
}
if(c=='B')
{
e1=i;
e2=j;
}
a[i][j]=c;
}
bfs();
if(flag==)
cout<<-;
else
cout<<flag;
return ;
}

洛谷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. JS 中检测数组的四种方法

    今天和大家分享一下 JS 中检测是不是数组的四种方法,虽然篇幅不长,不过方法应该算是比较全面了. 1. instanceof 方法 instanceof 用于检测一个对象是不是某个类的实例,数组也是一 ...

  2. 深入浅出 JVM 系列(一)什么是 JVM?它处于什么位置?

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  3. 【转】python及其工具包安装基本流程

    昨天晚上在家里的旧电脑上安装了<利用python进行数据分析>的部分环境,遇到若干问题,在此予以记录. 部分细节转:http://blog.csdn.net/huanbia/article ...

  4. static和final关键字

    static关键字 静态变量 静态变量:又称做类变量,也就是这个变量属于整个类,而不属于单个实例.类所有的实例共享静态变量,可以直接通过类名来访问它.静态变量在内存中只存在一份,当系统第一次加载类时, ...

  5. 查找2-n之间素数的个数

    题目描述 查找2-n之间素数的个数.n为用户输入值.素数:一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除,就叫素数.如2,3,5,7,11,13,17…. 输入 整数n 输出 2-n ...

  6. 3maven常用命令和配置依赖

    依赖: 例:spring-context.jar 依赖 spring-aop.jar... A中的某些类 需要使用B中的某些类,则称为A依赖于B 在maven项目中,如果要使用 一个当时存在的Jar或 ...

  7. 如何在ArcGIS中恢复注记文字

    文字标注是地图上一种特殊的视觉元素,可通过文字表达图形符号难以说明的地图内容,它与图形符号结合在一起存在于地图上,是关乎地图构图美的关键因素之一. MapGIS软件下子图对象和注释对象统统保存在点文件 ...

  8. 递推预处理 + Manacher

    链接:https://www.nowcoder.com/acm/contest/131/D来源:牛客网 字符串 S 只包含小写英文字母.有四种操作,每次操作你可以选择其中一种: 删除字符串的第一个字母 ...

  9. 18个Java8日期处理的实践,对于程序员太有用了!

    18个Java8日期处理的实践,对于程序员太有用了! Java 8 推出了全新的日期时间API,在教程中我们将通过一些简单的实例来学习如何使用新API. Java处理日期.日历和时间的方式一直为社区所 ...

  10. numpy 数组的计算

    一.数组和数的计算 数组和数计算,数组中的每个元素和数进行计算 1.加 import numpy as np arr1 = np.arange(12).reshape(3, 4) print(arr1 ...