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,然 ...
随机推荐
- 弹出浮层css+JQuery
先来张效果图: HTML代码如下: <body> <div class="bg" id="bg" style="display: n ...
- CSS3学习系列之动画
Transitions功能使用方法 在css3中,transitions功能通过将元素的某个属性从一个属性值在指定的时间内平滑过渡到另一个属性值来实现动画功能,可通过transitions属性来使用t ...
- 流行框架(angularj基础)
- 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】
Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...
- hdu2413(二分+二分匹配)
题意:人和外星人星球大战,人总共有H个星球,外星人有A个星球,现在人要用飞船去打外星人的飞船,要求每个人类星球只能对战一个外星球,且每个星球都开始有己知的飞船数,不论是人或外星人的星球,并每个星球都有 ...
- 64位win7安装jdk和eclipse
本人正确安装成功步骤,对他人可能无用: 1.直接拷以前32位eclipse ADT 2.安装32位的jdk:jdk-8u45-windows-i586 3.ok,所有环境变量无需手工设置 eclips ...
- php之试触法----error--关键字的误用
实际开发中,在不同网页的输出中,常常有许多公共的代码或者变量需要使用,于是定义了以下类来缩减代码量 如下代码所示: <?php class universalClass { function w ...
- mysql数据库左联的使用(一张数据库表关联到另外一张数据库表)
左联的数据库表,然后显示的在页面显示的jsp里面改一下代理种类ID的name,这样在页面上显示的不是id了,而是变成你修改了以后相对于的name了
- 列表的系列操作(python)
除了定义和切片外,这里总结下系列的操作: # hanbb come on! names = ["hbb",'tian','bao','cheng'] #Add names.appe ...
- U盘发现器
U盘发现器 package com.lx.io; import java.io.File; import java.io.IOException; import java.util.ArrayList ...