题目描述

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

说明

【注释】

只可以上下左右四个方向行走,并且不能走出这些格子之外。开始和结束时的方向可以任意。

  1//我用的DFS,他们说dfs做不出来,但我做出来了QWQ 
#include<bits/stdc++.h>
using namespace std;
int read()
{
int ret=,ok=;char ch=getchar();
while(ch<''||ch>'') {if(ch=='-')ok=-;ch=getchar();}
for(;ch>=''&&ch<='';ch=getchar()) ret=ret*+ch-'';
return ret*ok;
}
int n;
char a[][];
int ans[][];
bool vis[][],sea[][];
int bx[]={-,,,};
int by[]={,,-,};
inline void dfs(int sx,int sy)
{
sea[sx][sy]=true;
if(sx>n || sy>n || sy<= || sx<=)
return;
vis[sx][sy]=;
for(int i=sx+;i<=n;i++)
{
if(a[i][sy]=='x')
break;
if(ans[i][sy]>ans[sx][sy]+)
{
ans[i][sy]=ans[sx][sy]+;
dfs(i,sy);
}
vis[i][sy]=true;
}
for(int i=sx-;i>=;i--)
{
if(a[i][sy]=='x')
break;
if(ans[i][sy]>ans[sx][sy]+)
{
ans[i][sy]=ans[sx][sy]+;
dfs(i,sy);
}
vis[i][sy]=true;
}
for(int i=sy+;i<=n;i++)
{
if(a[sx][i]=='x')
break;
if(ans[sx][i]>ans[sx][sy]+)
{
ans[sx][i]=ans[sx][sy]+;
dfs(sx,i);
}
vis[sx][i]=true;
}
for(int i=sy-;i>=;i--)
{
if(a[sx][i]=='x')
break;
if(ans[sx][i]>ans[sx][sy]+)
{
ans[sx][i]=ans[sx][sy]+;
dfs(sx,i);
}
vis[sx][i]=true;
}
for(int i=;i<=;i++)
{
int nx=sx+bx[i];
int ny=sy+by[i];
if(nx>n || ny>n || ny<= || nx<=)
continue;
if(vis[nx][ny] && !sea[nx][ny])
{
sea[nx][ny]=;
dfs(nx,ny);
}
}
}
int main()
{
int sx,sy;
int lx,ly;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
ans[i][j]=; //这一步赋值很关键!之前用memset赋0x7f,会错一个点
n=read();
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j]=='A')
{
sx=i;
sy=j;
ans[i][j]=-;
}
if(a[i][j]=='B')
{
lx=i;
ly=j;
}
}
dfs(sx,sy);
if(ans[lx][ly]==)
{
cout<<-<<endl;
return ;
}
cout<<ans[lx][ly]<<endl;
return ;
}

Luogu 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. 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. 关于mysql查询数据库时间和系统时间差

    1. MySQL数据库表中有两个时间的字段,需要计算他们的时间差: (1)datediff函数来表示时间差. 基本语法: DATEDIFF(datepart,startdate,enddate) 说明 ...

  2. 【干货】Chrome插件(扩展)开发全攻略(不点进来看看你肯定后悔)

    写在前面 我花了将近一个多月的时间断断续续写下这篇博文,并精心写下完整demo,写博客的辛苦大家懂的,所以转载务必保留出处.本文所有涉及到的大部分代码均在这个demo里面:https://github ...

  3. Chrome浏览器扩展开发系列之十二:Content Scripts

    Content Scripts是运行在Web页面的上下文的JavaScript文件.通过标准的DOM,Content Scripts 可以操作(读取并修改)浏览器当前访问的Web页面的内容. Cont ...

  4. CentOS7下使用YUM安装mariadb10

    1:由于centos7 默认使用yum安装MySQL的话就会安装mariadb,只是安装的版本停留在mariadb5.x,版本比较低.如果我们需要安装mariadb10这里就需要删除mariadb-l ...

  5. 将git版本号编译进程序

    问题的提出 不管是什么版本管理工具,每一条提交记录都会有一个对应的版本号,一般是一个整数,git是一个hash字符串.不管怎样,这个版本号是唯一的,有时候我们在程序运行的时候会在日志里面输出程序的版本 ...

  6. Spring Boot 系列(三)属性配置&自定义属性配置

    在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...

  7. 深入理解javascript异步编程障眼法&&h5 web worker实现多线程

    0.从一道题说起 var t = true; setTimeout(function(){ t = false; }, 1000); while(t){ } alert('end'); 1 2 3 4 ...

  8. HTML5中a标签的锚点使用

    前几天有个用户问我关于在线手册功能里的锚点问题.因为他通过代码发现,在编辑手册内容时,锚点的设置是通过id选择器来制定的,而不是带有name属性的a标签.其实这是HTML5和HTML4(XHTML)等 ...

  9. 一步一步学Vue (一)

    vue应该是前端主流框架中的集成大成者,它吸取了knockout,angular,react设置avalon的经验,支持各种模式写法,入门很简单,从本章开始,会记录学习vue中的点点滴滴,以笔记的形式 ...

  10. 我眼中的ASP.NET Core之微服务 (二)

    前言 接上一篇. 上一篇未完待续的原因是当时刚好是6-30号晚上马上12点了还没写完,然后我想赶在7月1号之前发出去,所以当时就发了.然后在发的时候出了一点问题,结果发出去的时候刚好是 7.1号 00 ...