题目描述

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

说明

【注释】

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

思路:搜索。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int map[][];
int dx[]={,-,,};
int dy[]={,,,-};
int n,sx,sy,tx,ty,ans=0x7f7f7f7f;
void dfs(int x,int y,int tot,int pre){
if(tot>ans) return ;
if(x==tx&&y==ty&&tot<ans){
ans=tot;
return ;
}
for(int i=;i<;i++){
int cx=x+dx[i];
int cy=y+dy[i];
if(cx>=&&cx<=n&&cy>=&&cy<=n&&!map[cx][cy]){
map[cx][cy]=;
if(i!=pre&&pre!=) dfs(cx,cy,tot+,i);
else dfs(cx,cy,tot,i);
map[cx][cy]=;
}
}
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++) {
char x;cin>>x;
if(x=='x') map[i][j]=;
else map[i][j]=;
if(x=='A'){ sx=i;sy=j; }
else if(x=='B'){ tx=i;ty=j; }
}
}
dfs(sx,sy,,);
if(ans!=) cout<<ans;
else cout<<"-1";
}
/*
5
. . B x .
. x x A .
. . . x .
. x . . .
. . x . .
*/

洛谷 P1649 [USACO07OCT]障碍路线Obstacle Course的更多相关文章

  1. bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course

    P1649 [USACO07OCT]障碍路线Obstacle Course bfs 直接上个bfs 注意luogu的题目和bzoj有不同(bzoj保证有解,还有输入格式不同). #include< ...

  2. 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 ...

  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. [USACO07OCT]障碍路线Obstacle Course

    题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these ti ...

  5. 洛谷P1649 【[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. 洛谷 P4478 [BJWC2018]上学路线

    洛谷 P4478 [BJWC2018]上学路线 原题 神仙题orz,竟然没有1A....容斥+卢卡斯+crt?? 首先用容斥做,记\(f[i][0/1]\)表示到i号点经过了奇数/偶数个点的方案数,因 ...

  8. 【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线

    P2233 [HNOI2002]公交车路线 题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另 ...

  9. 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. ...

随机推荐

  1. bzoj4004 [JLOI2015]装备购买——线性基+贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4004 今天讲课讲到的题,据说满足拟阵的性质,所以贪心是正确的: 总之就贪心,按价格从小到大排 ...

  2. css 引入方式

    css介绍 现在的互联网前端分三层: HTML:超文本标记语言.从语义的角度描述页面结构. CSS:层叠样式表.从审美的角度负责页面样式. JS:JavaScript .从交互的角度描述页面行为 CS ...

  3. android开发 设备调试问题

    如果在mac下面用趁机开发android程序,发现不能连接上时就需要进行简单设置即可 先设置Finder为可以显示所有隐藏文件夹: 打开终端,输入如下命令: 显示Mac隐藏文件的命令:defaults ...

  4. checked、disabled在原生、jquery、vue下不同写法

          以下是原生和jquery <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...

  5. Java常见错误整理(一)

    1.配置完Java环境变量之后,仍然不能使用java命令. 解决方法: 如果是Windows10系统出现此问题,是因为个别Windows10系统不识别“JAVA_HOME”环境变量,将path中所有的 ...

  6. angular2之组件通讯

    定义父组件,在父组件中以路由插座形式引入子组件,定义相关输入输出属性 可以在同一模块内部定义多个组件,将一个组件引入另一个组件中去:也可以该模块整体导出,将该模块导入到其他模块,这样此模块中的组件就能 ...

  7. 使用CMD建立指定格式的文件

    一.建立空文件的几种方法1.cd.>a.txtcd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出.>表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.txt. ...

  8. js基础标签用法

    js是脚本语言,开始标签<script type="text/javascript">.......结束标签</script>.script通常放在< ...

  9. Deutsch lernen (08)

    1. empfehlen - empfahl - hat empfohlen  推荐:劝说,劝告 Können Sie mir einen guten Artz empfehlen? jemand e ...

  10. 使用CImage类 显示图片

    在不适用openCv的一种时候,使用CImage显示图片数据,并且直接嵌入DC框中. 使用CImage 在pic控件里显示图片 void CMyCalLawsDlg::MyShowImage( CIm ...