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。
输入输出样例
说明
【注释】
只可以上下左右四个方向行走,并且不能走出这些格子之外。开始和结束时的方向可以任意。
这里注意跟朴素bfs不同在于
while不停入队和vis打标记
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
return max(max(a, b), max(c, d));
}
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
const int maxn = ;
#define mod 100003
const int N=; // name*******************************
struct node
{
int x,y;
} p[N];
int dx[]= {,-,,};
int dy[]= {,,,-};
queue<node>que;
bool vis[N][N];
char G[N][N];
int n;
node sx,ex;
int cnt[N][N];
// function******************************
void bfs(node u)
{
que.push(u);
vis[u.x][u.y]=;
while(!que.empty())
{
node t=que.front();
que.pop();
int x=t.x,y=t.y;
For(i,,)
{
int tx=x+dx[i],ty=y+dy[i];
if(tx<||tx>n||ty<||ty>n)continue;
while((G[tx][ty]=='.'||G[tx][ty]=='B'))//vis的判断条件不能加在这里!!就算这点走过了,但这一行或这一列其他点不一定走过!!while需要继续搜
{
if(vis[tx][ty]==)
{
que.push(node{tx,ty});
vis[tx][ty]=;
cnt[tx][ty]=cnt[x][y]+;
}
if(tx==ex.x&&ty==ex.y)
{
cout<<cnt[tx][ty];
exit();
}
tx+=dx[i],ty+=dy[i];
}
}
}
} //***************************************
int main()
{
// freopen("test.txt", "r", stdin);
cin>>n; For(i,,n)
For(j,,n)
{
cin>>G[i][j];
if(G[i][j]=='A')
{
sx.x=i,sx.y=j;
cnt[i][j]=-;
}
if(G[i][j]=='B')
ex.x=i,ex.y=j;
}
bfs(sx); cout<<-; return ;
}
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 ...
- 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 ...
- 障碍路线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,然 ...
随机推荐
- 【代码笔记】iOS-UIActionSheet动态添加按钮
一,效果图. 二,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIVi ...
- 精选20个高品质的免费素材,可以下载PSD格式
GraphicBurger 这个站点免费和收费的都有,注意区分 365psd 在日本比较有名的免费素材站. Pixeden Techandall Premium pixels 全部免费! Design ...
- H5音乐播放器【歌单列表】
上篇详细描述了播放页歌词如何实现跟随跟单滚动,如何解析歌词,那么歌单页又是如何生成的呢,话不多说,直接上图上代码! 首先需要获取数据,具体获取数据api请转到我跟我大兄弟博客去观看学习去,同时也感谢我 ...
- jQuery复选框全选和全选取消
jQuery(".salaryIds").each(function(){ if(jQuery("#salaryIds").attr("checked ...
- Microsoft MVP MSDN Magazine 最新期分享
下载地址:http://1105insight.com/portal/wts/uemcmQeeDyaq%5Ev2gAe6c3b0Djd 可在线或下载查看
- [WPF 容易忽视的细节] —— x:Name与Name属性
一.前言 WPF使用XAML来对界面进行编写,界面与后台逻辑分离.我们也可以写Style.Trigger来实现一些界面效果, 这些都是通过Name来定位控件的,例如Setter.TargetName. ...
- EVE Online Third Party Development
第一部分:price_history表 # 建表语句 CREATE TABLE IF NOT EXISTS `price_history` ( `regionID` INT NOT NULL, `ty ...
- 从零自学Java-3.在程序中存储和修改变量信息
1.创建变量: 2.使用不同类型的变量: 3.在变量中存储值: 4.在数学表达式中使用变量: 5.把一个变量的值赋给另一个变量: 6.递增/递减变量的值. 程序Variable:使用不同类型的变量并赋 ...
- BigDecimal 工具类
arg1.compareTo(arg2) arg1 > arg2 返回 int 1 arg1 = arg2 返回 int 0 arg1 < arg2 返回 int -1 public cl ...
- LINQ学习:Select的用法
转载于:http://www.cnblogs.com/ForEvErNoME/archive/2012/07/25/2606659.html 说明:在查询表达式中,select 子句可以指定将在执行查 ...