UVA - 1600 Patrol Robot (巡逻机器人)(bfs)
题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路。
分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int pic[MAXN][MAXN];
int vis[MAXN][MAXN];
int m, n, k;
bool judge(int x, int y){
return x >= && x <= m && y >= && y <= n;
}
int bfs(){
queue<int> x, y, step, obstacle;
x.push();
y.push();
step.push();
obstacle.push();
vis[][] = ;
while(!x.empty()){
int tmpx = x.front(); x.pop();
int tmpy = y.front(); y.pop();
int tmpstep = step.front(); step.pop();
int tmpobstacle = obstacle.front(); obstacle.pop();
for(int i = ; i < ; ++i){
int tx = tmpx + dr[i];
int ty = tmpy + dc[i];
if(judge(tx, ty) && !vis[tx][ty]){
if(tx == m && ty == n) return tmpstep + ;
if(pic[tx][ty] == ){
vis[tx][ty] = ;
x.push(tx);
y.push(ty);
step.push(tmpstep + );
obstacle.push();
}
else if(pic[tx][ty] == ){
int nowobstacle = tmpobstacle + ;
if(nowobstacle <= k){
x.push(tx);
y.push(ty);
step.push(tmpstep + );
obstacle.push(nowobstacle);
}
}
}
}
}
return -;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
memset(pic, , sizeof pic);
memset(vis, , sizeof vis);
scanf("%d%d%d", &m, &n, &k);
for(int i = ; i <= m; ++i){
for(int j = ; j <= n; ++j){
scanf("%d", &pic[i][j]);
}
}
int ans = bfs();
printf("%d\n", ans);
}
return ;
}
UVA - 1600 Patrol Robot (巡逻机器人)(bfs)的更多相关文章
- UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)
UVA 1600 Patrol Robot Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)
非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...
- UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)
题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...
- Uva 1600 Patrol Robot (BFS 最短路)
这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #inclu ...
- UVa 1600 Patrol Robot(BFS)
题意: 给定一个n*m的图, 有一个机器人需要从左上角(1,1)到右下角(n,m), 网格中一些格子是空地, 一些格子是障碍, 机器人每次能走4个方向, 但不能连续穿越k(0<= k <= ...
- UVa 1600 Patrol Robot【BFS】
题意:给出一个n*m的矩阵,1代表墙,0代表空地,不能连续k次穿过墙,求从起点到达终点的最短路的长度 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰到墙,当前的k减去1,碰到0, ...
- UVa 1600 Patrol Robot(三维广搜)
A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumn ...
- UVa 1600 Patrol Robot (习题 6-5)
传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...
- UVA 1600 Patrol Robot
带状态的bfs 用一个数(ks)来表示状态-当前连续穿越的障碍数: step表示当前走过的步数: visit数组也加一个状态: #include <iostream> #include & ...
随机推荐
- DAC
DAC的功能:可以输出电压(无触发,设置DAC转换值,DAC使能更新寄存器和启动转换)或者波形(连续转换,用DMA发送数据,定时器触发).首先有个正弦波波形的数组,内存到DAC的DMA通道不断的将正弦 ...
- 字符串NSString与NSMutableString常用方法
NSString 1.初始化 NSString *str1 = @"a OC Program"; 2.初始化 NSString *str2 = [[NSString alloc] ...
- 解决vue更新默认值时出现的闪屏问题
在Vue项目中,对于一个展示用户个人信息的页面.有以下需求,需要判断用户个人信息是否填充过,如果填充过,需要在页面中展示已填充项(未填充项不展示):如果未填充过,需要在页面中显示另外一种元素(提示用“ ...
- PaperReading20200224
CanChen ggchen@mail.ustc.edu.cn BANANAS Motivation: This paper proposes a network performance pred ...
- 【转载】将Centos的yum源更换为国内的阿里云源
自己的yum源不知道什么时候给改毁了--搜到了个超简单的方法将yum源更换为阿里的源 完全参考 http://mirrors.aliyun.com/help/centos?spm=5176.bbsr1 ...
- Linux下,Tomcat启动成功,发现ip:8080访问失败
Linux下,Tomcat启动成功,发现ip:8080访问失败 Chasel_H 2018.04.23 20:47* 字数 195 阅读 566评论 0喜欢 3 相信很多人都和我一样,在Linux环境 ...
- Python学习第一课——if-else
#if 基本语句 if 1==1: print("如果条件为真,if执行该语句") else: print("如果条件为假,if则执行这条语句") #if 多重 ...
- Vue - @import css 加载第三方css
@import '~@/assets/css/style.css' CSS loader 会把把非根路径的url解释为相对路径, 加~前缀才会解释成模块路径.
- 「CF1C Ancient Berland Circus」
CF第一场比赛的最后一题居然是计算几何. 这道题的考点也是比较多,所以来写一篇题解. 前置芝士 平面直角坐标系中两点距离公式:\(l=\sqrt{(X_1-X_2)^2+(Y_1-Y_2)^2}\) ...
- require - 引入文件
导入 /** * Creates the node for the load command. Only used in browser envs. */ req.createNode = funct ...