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 & ...
随机推荐
- CentOS7编译安装httpd-2.4.41
安装参考环境: CentOS Linux release 7.5.1804 (Core) 一.安装依赖包 httpd安装的依赖包 # yum -y install pcre-devel # yum - ...
- Atcoder Beginning Contest 134E(二分查找(upper_bound),思维)
#include<bits/stdc++.h>using namespace std;int a[100007],f[100007],ans,n;int main(){ cin>&g ...
- [经验] 关于 Java 中的非空判断
在写项目的时候, 遇到一个问题 假设有一个控制层接口为: @ResponseBody @RequestMapping(value = "test", method = Reques ...
- Node.js 阻塞 回调函数
回调例程 N所有API都支持回调函数,可以处理大量并发请求.回调函数一般作为最后一个参数出现: function foo1(name, age, callback){ } function foo2( ...
- 网络请求中的URL中传bool型数据
如果在URL中要拼接bool的数据,OC这边不能使用BOOL型.因为使用NSString的拼接字符串类方法中,会将BOOL型数据转化为0或者1. 解决办法: NSString *overdue_str ...
- 配置web应用全局的错误页面
- python中metaclass的工作原理
class TMetaclass(type): def __new__(cls, name, bases, attrs): print(cls, name, bases, attrs) return ...
- importlib 与__import__的区别
importlib 与__import__的区别 importlib import importlib name = "lib.test" # lib.test指的是lib路劲下的 ...
- P1095 解码PAT准考证
1095 解码PAT准考证 (25分) PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: ...
- Ubuntu的妥协将支持精选的32位应用
据外媒Tom's hardware,Ubuntu开发人员Canonical在早先的时候宣布Ubuntu 19.10将不再更新32位软件包和应用程序,引来了诸多应用开发者的不满.现在,Ubuntu方面宣 ...