洛谷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. ...
随机推荐
- 洛谷P1002 过河卒 题解 动态规划
题目链接:https://www.luogu.com.cn/problem/P1002 题目大意 棋盘上\(A\)点有一个过河卒,需要走到目标\(B\)点.卒行走的规则:可以向下.或者向右.同时在棋盘 ...
- java线程相关基本方法
java线程中常用的基本方法有wait,notify,notifyAll,sleep,join,yield等. 线程的生命周期一共分为五个部分,分别是:新建(New).就绪(Runnable).运行( ...
- 《C++Primer》第五版习题答案--第三章【学习笔记】
[C++Primer]第五版[学习笔记]习题解答第三章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/10 第三章:字符串,向量和数组 ...
- express框架中使用nodemon自启动服务
1.安装nodemon //全局安装 npm install -g nodemon //本地安装 npm install nodemon --save 2.修改package.json配置 " ...
- MySQL快速回顾:计算字段与函数
9.1 计算字段 存储在数据库表中的数据一般不是应用程序所需要的格式.比如: 如果想要在一个字段中既显示公司名,又显示公式的地址,但这两个信息一般包含在不同的表列中. 城市.州和邮政编码存储在不同的列 ...
- Spring Boot2 系列教程 (八) | 配置日志
前言 如题,今天介绍 springboot 默认日志的配置. 默认日志 Logback 默认情况下,Spring Boot 用 Logback 来记录日志,并用 INFO 级别输出到控制台.如果你在平 ...
- OpenGL ES for Android
经过半年的准备OpenGL ES for Android系列文章终于要和大家见面了,在这里定一个小目标-先吸引1000个粉丝,万一实现了呢.写关于OpenGL ES的文章开始是有一些犹豫的,因为Ope ...
- C++ 排序引用的优化
链接:https://www.nowcoder.com/acm/contest/83/B来源:牛客网 题目描述 第一次期中考终于结束啦!沃老师是个语文老师,他在评学生的作文成绩时,给每位学生的分数都是 ...
- 《Java核心技术》 JVM指令集
https://www.jianshu.com/p/bc91c6b46d7b
- mysql 视图/触发器/函数
一.视图 作用:简写代码,与临时表的作用差不多 .创建 create view 视图名 as SQL语句 .修改 alter view 视图名 as 新SQL语句 .删除 drop view 视图名 ...