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 & ...
随机推荐
- 「JSOI2010」满汉全席
前言 由于蒟蒻才刚开始学 \(\text{2-SAT}\),所以题解中有的地方可能不够精炼,望多包涵! 题目描述 题目意思很简单,标准的\(\text{2-SAT}\)问题模型.那么我们就先来介绍一下 ...
- mysql时出现:is not allowed to connect to this MySQL serverConnection closed by foreign host问题的解决
这个原因是因为索要链接的mysql数据库只允许其所在的服务器连接,需要在mysql服务器上设置一下允许的ip权限,如下: 1.连接mysql mysql -u root -p 1 如图: 2.授权 g ...
- java连接sql server 2008
请先确保已经设置好了sa,如果不是,可以参照下面链接修改http://jingyan.baidu.com/article/8cdccae9452b3c315513cd52.html 然后重启数据库,重 ...
- 巧用DOS命令合并多个文本文件的内容
假设,在网上下载了一本小说.这本小说是由100多个文本文件组成的.这个时候,将这100多个文本文件的内容全部合并到一个文本文件中,阅读起来就会显得很方便 (1)首先,使用本书中“批量按序更改文 ...
- wxPython--学习笔记
wxPython程序由两个必要的对象组成,应用对象APP和顶级窗口对象Frame 应用程序对象APP管理主事件循环MainLoop() 顶级窗口对象Frame管理数据,控制并呈现给用户 先看一段最简单 ...
- HTML元素和测试用例的简要介绍
HTML和CSS的基本语法就不出教程,线下自己看电子书即可 HTML元素 标签 内容 属性 标签+内容+属性 <html> <head> <title>我的主页&l ...
- ajax请求Controller,返回信息乱码问题
参考:https://blog.csdn.net/hgg923/article/details/53610548 @RequestMapping(value = "changeMobile& ...
- PIL pip error
结果显示: 提示——Could not find a version that satisfies the requirement PIL (from versions: )No matching d ...
- spring mvc ,spring boot 整合swagger
https://blog.csdn.net/qq_35992900/article/details/81274436
- java递归调用 return的问题
最近比较闲,写了个递归调用获取最大公约数,刚开始写错了,但一直不明白错在哪,错误代码如下: public class Demo { public static void main(String[] a ...