题意:从(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)的更多相关文章

  1. UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)

    UVA 1600 Patrol Robot   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   ...

  2. UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)

    非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...

  3. UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)

    题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...

  4. Uva 1600 Patrol Robot (BFS 最短路)

    这道题运用的知识点是求最短路的算法.一种方法是利用BFS来求最短路. 需要注意的是,我们要用一个三维数组来表示此状态是否访问过,而不是三维数组.因为相同的坐标可以通过不同的穿墙方式到达. #inclu ...

  5. UVa 1600 Patrol Robot(BFS)

    题意: 给定一个n*m的图, 有一个机器人需要从左上角(1,1)到右下角(n,m), 网格中一些格子是空地, 一些格子是障碍, 机器人每次能走4个方向, 但不能连续穿越k(0<= k <= ...

  6. UVa 1600 Patrol Robot【BFS】

    题意:给出一个n*m的矩阵,1代表墙,0代表空地,不能连续k次穿过墙,求从起点到达终点的最短路的长度 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰到墙,当前的k减去1,碰到0, ...

  7. 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 ...

  8. UVa 1600 Patrol Robot (习题 6-5)

    传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...

  9. UVA 1600 Patrol Robot

    带状态的bfs 用一个数(ks)来表示状态-当前连续穿越的障碍数: step表示当前走过的步数: visit数组也加一个状态: #include <iostream> #include & ...

随机推荐

  1. 阿里云服务器Ubantu16.04升级python3.6

    从ppa上下载下载python3.6 sudo apt-get install software-properties-common#使用 “add-apt-repository” 脚本添加 ppa ...

  2. 「JLOI2011」飞行路线

    前言 看到好多大佬都在跑分层图最短路,\(\text{DP}\) 解法的我瑟瑟发抖... 题目描述 给定一张 \(N\) 个点(点编号从 \(0\) 到 \(N-1\)),\(M\) 条边的无向带权图 ...

  3. FFmpeg调用c语言SDK实现日志的打印

    日志文件的三大步 // 导入头文件 #include <libavutil/log.h> // 设置日志级别 av_log_set_level(AV_LOG_DEBUG); //DEBUG ...

  4. Flutter | 状态管理特别篇——Provide

    前言 今天偶然发现在谷歌爸爸的仓库下出现了一个叫做flutter-provide的状态管理框架,2月8日才第一次提交,非常新鲜.在简单上手之后感觉就是一个字--爽!所以今天就跟大家分享一下这个新的状态 ...

  5. 让tableView的高度等于contentSize的高度、动态调整tableView的高度、tableView的高度自适应布局

    文章概要: 1.简介下,tableView中的内容如何高度自适应的布局 2.如何做到让tableView的高度动态调整 还是看图作文吧- 首先,tableView的高度就是用户能够看见里面更大世界的那 ...

  6. 虚拟机centos7 识别不出网卡的解决方案

    问题提出 之前在VMware安装centos 7(网络连接采取Nat方式),进入Centos 7后成功开启了sshd服务,然后在主机(我的windows系统)用puTTY软件可以ssh到虚拟机上.后来 ...

  7. 3.8.1 HTML与CSS简单页面效果实例

    HTML与CSS简单页面效果实例 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  8. 第1节 storm编程:7、并行度分析以及如何解决线程安全问题

    storm其实就是一个多进程与多线程的框架 开多个进程:分配到的资源更多 开多个线程:执行的速度更快 设置进程个数以及线程个数 ==================================== ...

  9. Spring 通读官方文档

    Spring 通读官方文档 这部分参考文档涵盖了Spring Framework绝对不可或缺的所有技术. 其中最重要的是Spring Framework的控制反转(IoC)容器.Spring框架的Io ...

  10. lnmp1.5下安装mongodb

    一.安装mongodb .下载MongoDB 2.6.0二进制发行版 $ curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64 ...