Luogu 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。
输入输出样例
3
. x A
. . .
B x .
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的更多相关文章
- bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course
P1649 [USACO07OCT]障碍路线Obstacle Course bfs 直接上个bfs 注意luogu的题目和bzoj有不同(bzoj保证有解,还有输入格式不同). #include< ...
- 洛谷 P1649 [USACO07OCT]障碍路线Obstacle Course
P1649 [USACO07OCT]障碍路线Obstacle Course 题目描述 Consider an N x N (1 <= N <= 100) square field comp ...
- 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 ...
- [USACO07OCT]障碍路线 & yzoj P1130 拐弯 题解
题意 给出n* n 的图,A为起点,B为终点,* 为障碍,.可以行走,问最少需要拐90度的弯多少次,无法到达输出-1. 解析 思路:构造N * M * 4个点,即将原图的每个点分裂成4个点.其中点(i ...
- 2021.10.29 P1649 [USACO07OCT]Obstacle Course S(BFS)
2021.10.29 P1649 [USACO07OCT]Obstacle Course S(BFS) 题意: 给一张n*n的图,起点为A,终点为 B,求从A到B转弯次数最少为多少. 分析: 是否存在 ...
- [洛谷1649]障碍路线<BFS>
题目链接:https://www.luogu.org/problem/show?pid=1649 历经千辛万苦,我总算是把这个水题AC了,现在心里总觉得一万只草泥马在奔腾: 这是一道很明显的BFS,然 ...
随机推荐
- Linux 重启和关机命令
shutdown -r 05:30 在凌晨五点30分关机 shutdown -r 05:30 & 后台执行 shutdown -c 取消前一个关机命令 ...
- Java异常体系简析
最近在阅读<Java编程思想>的时候看到了书中对异常的描述,结合自己阅读源码经历,谈谈自己对异常的理解.首先记住下面两句话: 除非你能解决这个异常,否则不要捕获它,如果打算记录错误消息,那 ...
- Go学习笔记(一)Let's 干吧
加 Golang学习 QQ群共同学习进步成家立业 ^-^ 群号:96933959 简介 Go是Google开发的一种 静态强类型.编译型,并发型,并具有垃圾回收功能的编程语言.为了方便搜索和识 ...
- 基于JQ的单双日历,本人自己写的哈,还没封装,但是也能用
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>小 ...
- Android Studio和eclipse混淆打包总结
最近项目有点闲,考虑到以前的项目没有做过混淆,只是用了加固软件进行加固,为了安全性,准备给项目加上,这里做个总结,都经本人亲自在项目实践,说是为了安全性,这好像说大了,一来项目中没用到什么特别的技术, ...
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...
- 安装cocoaPods第三方类库
*1 检测gem 镜像文件 输入指令: gem sources -l 回车后得到镜像地址.可能是一个,也可能有好几个,常见两个如下 https://rubygems.org/ ...
- 点击<tr>表格行元素进行跳转
意外发现table中利用<a>标签控制tr的行为无效. 尝试<a>标签在table中的使用,只有在<td><a href="">&l ...
- super 与 this 同时使用问题
大家都知道this 和 super 调用构造函数时都必须放在第一句,今天同学问我的一个问题有点意思. 那么:我怎么在子类中 显式的用 super 初始化父类同时用 this 初始化子类? ------ ...
- Stacked Regression的详细步骤和使用注意事项
声明:这篇博文是我基于一篇网络文章翻译的,并结合了自己应用中的一些心得,如果有侵权,请联系本人删除. 最近做推荐的时候,开始接触到Stacking方法,在周志华老师的西瓜书中,Stacking方法是在 ...