[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
说明
【注释】
只可以上下左右四个方向行走,并且不能走出这些格子之外。开始和结束时的方向可以任意。
思路:BFS
代码实现:
#include<cstdio>
#include<iostream>
using namespace std;
int n,sx,sy,tx,ty;
int nx,ny,np,ns,fx,fy,fp,fs;
int cx[]={,,,-};
int cy[]={,,-,};
bool v[][][],map[][];
char ch;
int head,tail;
struct bfsw{int x,y,p,s;}q[];
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
cin>>ch;
if(ch=='x') map[i][j]=;
if(ch=='A') sx=i,sy=j;
if(ch=='B') tx=i,ty=j;
}
for(int i=;i<;i++){
q[head++]=(bfsw){sx,sy,i,};
v[sx][sy][i]=;
}
while(head>tail){
nx=q[tail].x,ny=q[tail].y,np=q[tail].p,ns=q[tail++].s;
for(int i=;i<;i++){
fp=(np++*i)%;
if(!v[nx][ny][fp]){
q[head++]=(bfsw){nx,ny,fp,ns+};
v[nx][ny][fp]=;
}
}
fp=np,fx=nx+cx[fp],fy=ny+cy[fp];
while(fx>&&fy>&&fx<=n&&fy<=n&&!map[fx][fy]){
if(fx==tx&&fy==ty){
printf("%d\n",ns);
return ;
}
if(!v[fx][fy][fp]) q[head++]=(bfsw){fx,fy,fp,ns};
fx+=cx[fp],fy+=cy[fp];
}
}
printf("-1\n");
return ;
}
好歹是道犇站的题,给点面子吧。
题目来源:洛谷
[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 ...
- 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 ...
- 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 ...
- 障碍路线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 ...
- [洛谷1649]障碍路线<BFS>
题目链接:https://www.luogu.org/problem/show?pid=1649 历经千辛万苦,我总算是把这个水题AC了,现在心里总觉得一万只草泥马在奔腾: 这是一道很明显的BFS,然 ...
- UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)
UVA 1600 Patrol Robot Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
随机推荐
- CSS中路径及form表单的用法
1.什么是路径? 路劲分为三种 1.绝对路径: 从盘符开始,然后依次的往下查找 本地: C:/Users/Administrator/Desktop/0527day01/07.html 服务器的: w ...
- jQuery——表单应用(1)
实现结果:聚焦表单的input部分时,input格式变更为CSS样式(获取和失去焦点改变样式) HTML: <!DOCTYPE html> <html> <head> ...
- [ NOI 2002 ] Robot
\(\\\) Description \(\\\) Solution 垃圾语文题毁我青春 这题其实就是重定义了俩函数.... 首先 \(\varphi(1)=0\) . 然后 \(2\) 在计算 \( ...
- jQuery伪分页效果
如图,我们首先分析在一个页面存放4条内容,其余的超出隐藏(因为这里没有后台数据,所以我们把内容‘写死’),然后就是下面两个按钮(这里我们不用button,因为button有自带的提交功能),然后我们可 ...
- 浅谈css的行内类型标签和块级标签
常用标签的行内类型标签有:a.span.img:块级标签有:div.p.h1~6.ul.ol.li.dl.dt.dd. 行内类型标签的特征:标签的大小由标签的内容决定,不能设置width和height ...
- App保持登录状态的常用方法
我们在使用App时,一次登录后App如果不主动退出登录或者清除数据,App会在很长一段时间内保持登录状态,或者让用户感觉到登录一次就不用每次都输入用户密码才能进行登录.银行.金融涉及到支付类的App一 ...
- Python中*args和**kwargs的使用
函数定义 使用*args和**kwargs传递可变长参数: *args用作传递非命名键值可变长参数列表(比如元组) **kwargs用作传递键值可变长参数列表(比如字典) 函数入参 *args 传递一 ...
- Squid 正向代理
实现通过特定设备对特定的网址访问加速 使用squid 正向代理 实现,区别于反向代理,两者区别的根本在于作为中转的服务器在一个完整的请求中是代表客户端还是代表服务器. 服务端设置 1.安装程序包(推荐 ...
- JSON字符串的生成
public class Corporation { public string remark { get; set; } public string version { get; set; } pu ...
- 初步认识MVC
一丶路由(One) 自定义路由,静态路由,动态路由,组合路由 routes.MapRoute 二丶Action向View传值的四种方式(ViewData.ViewBag.TempData.Model ...