bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course
P1649 [USACO07OCT]障碍路线Obstacle Course
bfs
直接上个bfs
注意luogu的题目和bzoj有不同(bzoj保证有解,还有输入格式不同)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define pi pair<int,int>
#define mkp make_pair
#define N 105
const int d1[]={,,,-};
const int d2[]={,,-,};
char q[]; bool dan[N][N];
int a[N][N],vis[N][N],n;
pi S,T; queue<pi> h;
int main(){
scanf("%d",&n);
for(int i=;i<=n;++i){
for(int j=;j<=n;++j){
scanf("%s",q);
if(q[]=='A') S=mkp(i,j);
if(q[]=='B') T=mkp(i,j);
if(q[]=='x') dan[i][j]=;
}
}h.push(S);
while(!h.empty()){
pi u=h.front(); h.pop();
for(int i=;i<;++i){
int r1=u.first+d1[i],r2=u.second+d2[i];
while(!dan[r1][r2]){
if(r1>n||r1<||r2>n||r2<) break;
if(vis[r1][r2]){
r1+=d1[i];r2+=d2[i];
continue;
}
vis[r1][r2]=vis[u.first][u.second]+;
if(mkp(r1,r2)==T){
printf("%d",max(,vis[r1][r2]-));
return ;
}h.push(mkp(r1,r2));
r1+=d1[i];r2+=d2[i];
}
}
}printf("-1");
return ;
}
P1649
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define pi pair<int,int>
#define mkp make_pair
#define N 105
const int d1[]={,,,-};
const int d2[]={,,-,};
char q[N]; bool dan[N][N];
int a[N][N],vis[N][N],n;
pi S,T; queue<pi> h;
int main(){
scanf("%d",&n);
for(int i=;i<=n;++i){
scanf("%s",q);
for(int j=;j<=n;++j){
if(q[j-]=='A') S=mkp(i,j);
if(q[j-]=='B') T=mkp(i,j);
if(q[j-]=='x') dan[i][j]=;
}
}h.push(S);
while(!h.empty()){
pi u=h.front(); h.pop();
for(int i=;i<;++i){
int r1=u.first+d1[i],r2=u.second+d2[i];
while(!dan[r1][r2]){
if(r1>n||r1<||r2>n||r2<) break;
if(vis[r1][r2]){
r1+=d1[i];r2+=d2[i];
continue;
}vis[r1][r2]=vis[u.first][u.second]+;
if(mkp(r1,r2)==T){
printf("%d",max(,vis[r1][r2]-));
return ;
}h.push(mkp(r1,r2));
r1+=d1[i];r2+=d2[i];
}
}
}
}
bzoj1644
bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course的更多相关文章
- 洛谷 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 ...
- [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,然 ...
随机推荐
- python----题库(一)
1.执行 Python 脚本的两种方式 答:1.>>python ../pyhton.py 2. >>python.py #必须在首行有 #!/usr/bin/env pyth ...
- Java-06-动手动脑
1.为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来? 因为子类继承于父类,子类中有父类的对象,父类的构造方法初始化后,子类才能运行自己的构造方法 不能放过来,继 ...
- POJ3268 Silver Cow Party【最短路】
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big co ...
- matlab中norm函数的用法
格式:n=norm(A,p) 功能:norm函数可计算几种不同类型的矩阵范数,根据p的不同可得到不同的范数 以下是Matlab中help norm 的解释 NORM Matrix or vecto ...
- ArcGIS API for javascript开发笔记(四)——GP服务调用之GP模型的规范化制作详解
感谢一路走来默默陪伴和支持的你~~~ -------------------欢迎来访,拒绝转载------------------- 在之前的利用Python分析GP服务运行结果的输出路径 & ...
- The "get" method should be used when the form is idempotent---正交的两个概念 get 幂等
https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.1 17.13.1 Form submission method The me ...
- Json模块dumps、loads、dump、load函数介绍
转自:http://blog.csdn.net/mr_evanchen/article/details/77879967 Json模块dumps.loads.dump.load函数介绍 1.json. ...
- router-link params传参
1.router.js配置 需要在路径后定义上要传的属性名 --> /:属性名(query方式不需要) { path: '/CreateProgress/:name1', name: ...
- Memcached与redis的比较--stackoverflow(转)
原文:http://blog.nosqlfan.com/html/3729.html 这两年Redis火得可以,Redis也常常被当作Memcached的挑战者被提到桌面上来.关于Redis与Memc ...
- MYSQL中的BlackHole引擎
MYSQL中的BlackHole引擎 http://blog.csdn.net/ylspirit/article/details/7234021 http://blog.chinaunix.net/u ...