luogu#P1649. [USACO07OCT] Obstacle Course S

    ID: 5707 远端评测题 1000ms 125MiB 尝试: 1 已通过: 1 难度: 3 上传者: 标签>SPFA动态规划dp图论广度优先搜索BFSUSACO2007

[USACO07OCT] Obstacle Course S

题目描述

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.

输入格式

第一行一个整数 NN,下面 NN 行,每行 NN 个字符,只出现字符:.,x,A,B\verb!.!,\verb!x!,\verb!A!,\verb!B!,表示上面所说的矩阵格子,每个字符后有一个空格。

输出格式

一个整数:最少转弯次数。如果不能到达,输出 1-1

题目大意

N×N (1N100)N\times N\ (1\le N\le 100) 方格中,x\verb!x! 表示不能行走的格子,.\verb!.! 表示可以行走的格子。卡门很胖,故而不好转弯。现在要从 AA 点走到 BB 点,请问最少要转 9090 度弯多少次?

3
. x A
. . .
B x .
2

提示

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

数据范围及约定

对于全部数据,保证 2N1002\le N\le 100