洛谷P1649 【[USACO07OCT]障碍路线Obstacle Course】
题目描述
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】的更多相关文章
- 洛谷 P1649 [USACO07OCT]障碍路线Obstacle Course
P1649 [USACO07OCT]障碍路线Obstacle Course 题目描述 Consider an N x N (1 <= N <= 100) square field comp ...
- bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course
P1649 [USACO07OCT]障碍路线Obstacle Course bfs 直接上个bfs 注意luogu的题目和bzoj有不同(bzoj保证有解,还有输入格式不同). #include< ...
- 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 ...
- P1649 [USACO07OCT]障碍路线Obstacle Course
题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these ti ...
- [USACO07OCT]障碍路线Obstacle Course
题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these ti ...
- 障碍路线Obstacle Course
P1649 [USACO07OCT]障碍路线Obstacle Course 裸的dfs,今天学了一个新招,就是在过程中进行最优性减枝. #include<bits/stdc++.h> us ...
- 洛谷 P4478 [BJWC2018]上学路线
洛谷 P4478 [BJWC2018]上学路线 原题 神仙题orz,竟然没有1A....容斥+卢卡斯+crt?? 首先用容斥做,记\(f[i][0/1]\)表示到i号点经过了奇数/偶数个点的方案数,因 ...
- 【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线
P2233 [HNOI2002]公交车路线 题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另 ...
- 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. ...
随机推荐
- 「SP25784」BUBBLESORT - Bubble Sort 解题报告
SP25784 BUBBLESORT - Bubble Sort 题目描述 One of the simplest sorting algorithms, the Bubble Sort, can b ...
- linux installer os的驱动更新
installer os的驱动更新 linux系统可以简单的分为installer OS与运行时的OS,安装阶段识别不到硬件设备大概率因为installers OS版本较低,没有驱动来识别新的硬件,可 ...
- 深入 Create React App 核心概念
本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...
- Java 基础(一)| 使用泛型的正确姿势
前言 为跳槽面试做准备,今天开始进入 Java 基础的复习.希望基础不好的同学看完这篇文章,能掌握泛型,而基础好的同学权当复习,希望看完这篇文章能够起一点你的青涩记忆. 一.什么是泛型 泛型,即&qu ...
- OpenGL ES for Android
经过半年的准备OpenGL ES for Android系列文章终于要和大家见面了,在这里定一个小目标-先吸引1000个粉丝,万一实现了呢.写关于OpenGL ES的文章开始是有一些犹豫的,因为Ope ...
- dp - 循环数组的最大和
首尾相连数组的最大子数组和 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一个由N个整数元素组成的数组arr,数组中有正数也有负数,这个数组不是一般的数组,其首尾是 ...
- Linux初始化Git环境
第一步:设置Git全局用户名和邮箱 git config --global user.name "你的用户名" git config --global user.email &qu ...
- Go 每日一库之 fsnotify
简介 上一篇文章Go 每日一库之 viper中,我们介绍了 viper 可以监听文件修改进而自动重新加载. 其内部使用的就是fsnotify这个库,它是跨平台的.今天我们就来介绍一下它. 快速使用 先 ...
- Bootstrap 常用网站
https://www.bootcss.com/ 中文官方文档 https://www.bootcdn.cn/ BootCDN http://www.fontawesome.com.cn/ ...
- 异数OS谈发展国产操作系统的问题
异数OS谈发展国产操作系统的问题 为什么写本文 最近中兴被美制裁的问题以及红芯使用开源技术宣称国产自主技术引发了舆论不少对国产CPU以及国产操作系统自主技术的讨论,为什么我们国家有BAT,有原子弹,能 ...