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,然 ...
随机推荐
- openstack 部署(Q版)-----环境准备篇
一.环境准备 系统:centos7 cinder01 内网:192.168.10.51 外网:172.16.1.51 compute01 内网:192.168.10.52 外网:172.16.1. ...
- ASP.NET IIS System.UnauthorizedAccessException: 对路径“C:\......xls”的访问被拒绝。
问题: System.UnauthorizedAccessException: 对路径“C:\.....xls”的访问被拒绝. 背景: 项目中用到Excel导出功能,用的是Excel模板的方式来做,意 ...
- ubuntu16.04 下 卸载CUDA9.1
网上很多教程删除都全 安装其他还会出错 在这把它删除,在命令行中输入 sudo apt-get remove cuda sudo apt-get autoclean sudo apt-get rem ...
- codeforces 779D - String Game
time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...
- 使用Intel IPT技术保护您的帐号安全
使用Intel IPT技术保护您的帐号安全
- initrd和initramfs的区别
Linux内核在初始化之后会执行init进程,而init进程会挂载我们的根文件系统,但由于init程序也是在根文件系统上的,所以这就有了悖论.Linux采用两步走的方法来解决这个问题.Linux2 ...
- sublime text3控制台每次报错会显示几行[ ]
如下图所示,每次编译报错的时候会显示: 我只需要报错信息,不想红框中的信息出现. 解决方案: 1 找到sublime Text3安装路径下的Default.sublime-package,如~Subl ...
- 14 jmeter性能测试实战--数据库MySQL
需求 测试用户表(对用户表select操作) 测试步骤 1.MySQL驱动下载并安装. 2.测试计划面板点击“浏览”按钮,将JDBC驱动(mysql-connector-java.jar)添加进来. ...
- Lodash 中文文档 (v4.16.1) 手机版
http://lodash.swift.ren/ 手机扫描二维码直接进入
- 万恶之源 - Python运算符与编码
格式化输出 现在有个需要我们录入我们身边好友的信息,格式如下: ------------ info of Alex Li ---------- Name : Alex Li Age : 22 job ...