洛谷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. ...
随机推荐
- Codeforces Round #519 by Botan Investments(前五题题解)
开个新号打打codeforces(以前那号玩废了),结果就遇到了这么难一套.touristD题用了map,被卡掉了(其实是对cf的评测机过分自信),G题没过, 700多行代码,码力惊人.关键是这次to ...
- golang编译之vendor机制
Go 1.5引入了vendor 机制,但是需要手动设置环境变量 GO15VENDOREXPERIMENT= 1,Go编译器才能启用.从Go1.6起,,默认开启 vendor 目录查找,vendor 机 ...
- .NET BS端和CS端相互压缩发送接收byte对象数据方法
本文是总结实际项目经验,代码不少是学习别人整合的,效果稳定可靠,有很大参考价值:但是也有不全面的地方,朋友们拿到可以按照自己需要修改. 场景是项目需要在客户端控制台软件和.NET MVC站点间互相传递 ...
- linux入门系列5--新手必会的linux命令
上一篇文章"linux入门系列4--vi/vim编辑器"我们讨论了在linux下如何快速高效对文本文件进行编辑和管理,本文将进一步学习必须掌握的linux命令,掌握这些命令才能让计 ...
- js 日期增减
js 的 Date 对象提供了许多方法,可以获取日期的年.月.日等信息,也可以修改年.月.日 日期的增减可以使用setFullYear().setMonth().setDate() 等方法 exp: ...
- 2020 年了,Java 日志框架到底哪个性能好?——技术选型篇
大家好,之前写(shui)了两篇其他类型的文章,感觉大家反响不是很好,于是我乖乖的回来更新硬核技术文了. 经过本系列前两篇文章我们了解到日志框架大战随着 SLF4j 的一统天下而落下帷幕,但 SLF4 ...
- C#调用7z实现文件的压缩与解压
1.关于7z 首先在这里先介绍一下7z压缩软件,7z是一种主流的 压缩格式,它拥有极高的压缩比.在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式.主要有以下特点: 来源且模块化的组 ...
- linux下大文件查询具体段内容
有时候我们的文件比较大,比如几十G,甚至上百G.这么大的文件怎么查询呢? 有很多种方法都可以实现,这儿选择用 cat 这个命令实现. 先来看看 cat 的介绍 cat 有个对应的命令 tac,cat反 ...
- Ubuntu16安装NVIDIA驱动后重复登录 简单粗暴
第一步 卸载所有NVIDIA的东西 第二步 开机,应该能进入默认驱动的桌面了,在设置里关闭开机密码,开机自动登录 第三步 安装英伟达驱动
- 小白学 Python 爬虫(42):春节去哪里玩(系列终篇)
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...