【习题 6-5 UVA-1600】Patrol Robot
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
设dis[x][y][z]表示到(x,y)连续走了z个墙的最短路
bfs一下就ok
【代码】
/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int k,n,m;
int a[N+10][N+10],dis[N+10][N+10][N+10];
queue <pair<int,pair<int,int> > > dl;
int main(){
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
while (T--){
cin >> n >> m;
cin >> k;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
cin >> a[i][j];
memset(dis,255,sizeof dis);
dis[1][1][0] = 0;
dl.push(make_pair(1,make_pair(1,0)));
while (!dl.empty()){
pair <int,pair<int,int> > temp = dl.front();
int x = temp.first,y = temp.second.first,num = temp.second.second;
dl.pop();
for (int i = 0;i < 4;i++){
int tx = x+dx[i],ty = y + dy[i];
if (tx >=1 && tx <= n && ty >=1 && ty <= m){
if (a[tx][ty]){
int num1 = num+1;
if (num1 <= k){
if (dis[tx][ty][num1]==-1){
dis[tx][ty][num1] = dis[x][y][num] + 1;
dl.push(make_pair(tx,make_pair(ty,num1)));
}
}
}else{
int num1 = 0;
if (dis[tx][ty][num1]==-1){
dis[tx][ty][num1] = dis[x][y][num] + 1;
dl.push(make_pair(tx,make_pair(ty,num1)));
}
}
}
}
}
int ans = -1;
for (int i = 0;i <= k;i++)
if (dis[n][m][i]!=-1){
if (ans==-1){
ans = dis[n][m][i];
}else{
ans = min(ans,dis[n][m][i]);
}
}
cout << ans << endl;
}
return 0;
}
【习题 6-5 UVA-1600】Patrol Robot的更多相关文章
- UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)
UVA 1600 Patrol Robot Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- UVa 1600 Patrol Robot (习题 6-5)
传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...
- 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 (BFS最短路 && 略不一样的vis标记)
题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...
- UVA 1600 Patrol Robot
带状态的bfs 用一个数(ks)来表示状态-当前连续穿越的障碍数: step表示当前走过的步数: visit数组也加一个状态: #include <iostream> #include & ...
- 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 (巡逻机器人)(bfs)
题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路. 分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可. #pragma commen ...
- 【UVa】1600 Patrol Robot(dfs)
题目 题目 分析 bfs可以搞,但是我还是喜欢dfs,要记忆化不然会T 代码 #include <cstdio> #include <cstring> #inc ...
随机推荐
- MongoDB常用操作整理
Mongodb:是一种NoSQL数据库,NoSQL:Not Only SQLSQL: 数据表->JDBC读取->POJO(VO.PO)->控制层转化为JSON数据->客户端 这 ...
- tcpg通信
1.客户端 from socket import * def main(): # 创建套接字 tcp_socket = socket(AF_INET,SOCK_STREAM) # 链接服务端 ip = ...
- php获取csv数据无乱码
<?php //获取csv数据 function csvencode($file){ if(!is_file($file['tmp_name'])){ ...
- 学习——HTML5中事件
HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...
- hiho week 37 P1 : 二分·二分查找之k小数
P1 : 二分·二分查找之k小数 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回里我们知道Nettle在玩&l ...
- 洛谷 P2117 小Z的矩阵
P2117 小Z的矩阵 题目描述 小Z最近迷上了矩阵,他定义了一个对于一种特殊矩阵的特征函数G.对于N*N的矩阵A,A的所有元素均为0或1,则G(A)等于所有A[i][j]*A[j][i]的和对2取余 ...
- XCode6报数组越界错误的问题
今天碰到一个非常奇葩的问题, 调试了半天: 错误:"index 0 beyond bounds for empty array", 意思就是说数据源数组为nil, 所以你调用直接 ...
- js插件---tree(多级文件)插件如何使用
js插件---tree(多级文件)插件如何使用 一.总结 一句话总结:还是一般的引入js和css后js调用的方式, 只不过tree调用的时候必须设置一个 HTML 模板(就是调用的那段html代码,别 ...
- C/C++(数据结构栈的实现)
栈的实现 特点FILO(先进后出) 假设栈的空间为8 top == 0 不能出栈,已到栈底 top == 8 不能入栈,已到栈顶 top始终指向一个待插入的位置 push操作,1.写入数据,2.top ...
- LightOJ 1291 Real Life Traffic
Real Life Traffic Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on LightOJ. O ...