洛谷 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。
输入输出样例
说明
【注释】
只可以上下左右四个方向行走,并且不能走出这些格子之外。开始和结束时的方向可以任意。
思路:搜索。
#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的更多相关文章
- bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course
P1649 [USACO07OCT]障碍路线Obstacle Course bfs 直接上个bfs 注意luogu的题目和bzoj有不同(bzoj保证有解,还有输入格式不同). #include< ...
- 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 ...
- [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 ...
- 洛谷 P4478 [BJWC2018]上学路线
洛谷 P4478 [BJWC2018]上学路线 原题 神仙题orz,竟然没有1A....容斥+卢卡斯+crt?? 首先用容斥做,记\(f[i][0/1]\)表示到i号点经过了奇数/偶数个点的方案数,因 ...
- 【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线
P2233 [HNOI2002]公交车路线 题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另 ...
- 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. ...
随机推荐
- JNDI 笔记(一) 概述
很多地方都会用到JNDI,一大堆的缩写加上一大堆不清不楚的概念描述,使得在看到的时候都不认识,更不要说使用了. JNDI,Java Naming Directory Interface,J2EE的 ...
- Sequence(优先队列)
http://poj.org/problem?id=2442 题意:给你n*m的矩阵,然后每行取一个元素,组成一个包含n个元素的序列,一共有n^m种序列, 让你求出序列和最小的前n个序列的序列和. # ...
- bzoj1708[Usaco2007 Oct]Money奶牛的硬币(背包方案数dp)
1708: [Usaco2007 Oct]Money奶牛的硬币 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 763 Solved: 511[Submi ...
- 前端常见面试题总结part2
今天总结了几道,感觉非常有意思的题,有感兴趣的可以看下,有疑问请留言~ (答案在最后) 考察自执行函数的this指向 审题要细心 var n = 2, obj = { n:2, fn:(functio ...
- Vue发布过程中遇到坑,以及webpack打包优化
前言 这段时间,本人自己做了一个vue画面部署到自己的服务器上,发现运行速度慢的的惊人,虽然服务器很渣(本人没什么钱,只能租最差的服务器,主要是给自己学习用的),但是这样开发出来的网站简直不能用,所以 ...
- 使用Attiny 85开发板制作BadUSB
什么是BadUSB?请查看:http://www.baike.com/wiki/BadUSB 或者看看腾讯这个视频!https://v.qq.com/x/page/l01425u2igw.html ...
- 网上干货 ElasticSearch详解与优化设计
https://blog.csdn.net/joez/article/details/52171199?locationNum=3&fps=1 分析得很好,需要仔细阅读 问题遗留点: Fiel ...
- 【PostgreSQL-9.6.3】表操作语句
1.创建数据表 create table table_name ( 字段1 数据类型[列级别约束条件][默认值], 字段2 数据类型[列级别约束条件][默认值], 字段3 数据类型[列级别约束条件][ ...
- Caffe: gflag编译出现问题汇总
1. 使用Unicode字符集: 出现问题 E:\CodeBase\ML\Caffe\ThirdPartySrc\gflags-master\src\gflags.cc(1340): error C2 ...
- Android开发之拍照功能实现
参考链接:http://www.linuxidc.com/Linux/2013-11/92892p3.htm 原文链接:http://blog.csdn.net/tangcheng_ok/articl ...