巡逻机器人(BFS)
巡逻机器人问题(F - BFS,推荐)
Description

A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (i, j) denotes the cell in row i and column j in the grid. At each step, the robot can only move from one cell to an adjacent cell, i.e. from (x, y) to (x + 1, y), (x, y + 1), (x - 1, y) or (x, y - 1). Some of the cells in the grid contain obstacles. In order to move to a cell containing obstacle, the robot has to switch to turbo mode. Therefore, the robot cannot move continuously to more than k cells containing obstacles.
Your task is to write a program to find the shortest path (with the minimum number of cells) from cell (1, 1) to cell (m, n). It is assumed that both these cells do not contain obstacles.
Input
The input consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.
For each data set, the first line contains two positive integer numbers m and n separated by space (1m, n
20). The second line contains an integer number k(0
k
20). The ith line of the next m lines contains n integer aij separated by space (i = 1, 2,..., m;j = 1, 2,..., n). The value of aij is 1 if there is an obstacle on the cell (i, j), and is 0 otherwise.
Output
For each data set, if there exists a way for the robot to reach the cell (m, n), write in one line the integer number s, which is the number of moves the robot has to make; -1 otherwise.
Sample Input
- 3
- 2 5
- 0
- 0 1 0 0 0
- 0 0 0 1 0
- 4 6
- 1
- 0 1 1 0 0 0
- 0 0 1 0 1 1
- 0 1 1 1 1 0
- 0 1 1 1 0 0
- 2 2
- 0
- 0 1
- 1 0
- Sample Output
7- 10
- -1
题目大意:
机器人要从一个M*N(1m, n
20)网格的左上角(1,1)走到右下角(M,N)。网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示)。
机器人每次可以往4个方向走一格,但不能连续的穿越k(0k
20)个障碍,求最短路长度。起点和终点保证是空地。
- 分析:
求最短路径,即最优解问题,采用BFS。
1.可以跨过一个障碍,但不能连续跨障碍。
2.边界点没有4个方向
3.要将走过的点进行标记- 代码:
- #include<cstdio>
- #include<iostream>
- #include<cstring>
- #include<queue>
- using namespace std;
- #define MAX 23
- int a[MAX][MAX];
- int nxt[][] = {{,},{,-},{-,},{,}};
- int book[MAX][MAX][MAX];
- int n,m,k;
- struct node
- {
- int x,y;
- int step;
- int kk;
- };
- queue<node>Q;
- void init()
- {
- while (!Q.empty())
- {
- Q.pop();
- }
- memset(book,,sizeof(book));
- }
- int can_move( int x ,int y,int kk )
- {
- if ( x>=&&x<=n&&y>=&&y<=m&&book[x][y][kk]== )
- return ;
- else
- return ;
- }
- int bfs ( node start )
- {
- init();
- if ( start.x==n&&start.y==m )
- {
- return start.step;
- }
- Q.push(start);
- while ( !Q.empty() )
- {
- node now = Q.front();
- Q.pop();
- if ( now.kk < )
- continue;
- for ( int i = ;i < ;i++ )
- {
- node newnode;
- int tx = now.x+nxt[i][], ty = now.y+nxt[i][];
- if ( a[tx][ty]== )
- newnode.kk = now.kk-;
- else
- newnode.kk = k;
- if ( can_move(tx,ty,newnode.kk) )
- {
- if ( tx==n&&ty==m )
- {
- return now.step+;
- }
- newnode.x = tx; newnode.y = ty; newnode.step = now.step+;
- if( newnode.kk >= )
- {
- Q.push(newnode);
- book[tx][ty][newnode.kk] = ;
- }
- }
- }
- }
- return -;
- }
- int main(void)
- {
- int t;scanf("%d",&t);
- while ( t-- )
- {
- scanf("%d%d%d",&n,&m,&k);
- for ( int i = ;i <= n;i++ )
- {
- for ( int j = ;j <= m;j++ )
- {
- scanf("%d",&a[i][j]);
- }
- }
- node start;
- start.x = ; start.y = ;start.step = ; start.kk = k;
- book[start.x][start.y][start.kk] = ;
- int ans = bfs(start);
- if ( ans == - )
- printf("-1\n");
- else
- printf("%d\n",ans);
- }
- return ;
- }
心得:
BFS的题自己还是不会写,要看别人AC的代码才会。还是要多看一些别人的优秀代码来仿写,加油吧,争取自己能写好的代码。
巡逻机器人(BFS)的更多相关文章
- BFS 巡逻机器人
巡逻机器人 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/F 题目大意: 机器人在一个矩形区域巡逻, ...
- UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)
非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...
- UVA - 1600 Patrol Robot (巡逻机器人)(bfs)
题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路. 分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可. #pragma commen ...
- UVA:1600 巡逻机器人
机器人要从一个m*n(m和n的范围都在1到20的闭区间内)的网格的左上角(1,1)走到右下角(m,n).网格中的一些格子是空地,用0表示,其它格子是障碍,用1表示.机器人每次可以往四个方向走一格,但不 ...
- UVa——1600(巡逻机器人)
迷宫求最短路的话一般用bfs都可以解决,但是这个加了个状态,那么就增加一个维度,用来判断k的值.比较简单的三维bfs.写搜索题的话一定要注意细节.这个题花了好长的时间.因为k的原因,一开始用了k的原因 ...
- 6-5 巡逻机器人 uva1600
一开始按照标准bfs来写 标记为二维数组 后来按照三维数组写过了 ps大部分bfs都不会是二维数组搞定!!! 其中有一个bug弄了半个小时... 一开始我是先判断!vis[x][y][v.c] ...
- 中国VR公司的详尽名单
中国VR公司的详尽名单 <VR圈深度投资报告一:2014年以来所有VR/AR融资事件> 特征一.投资机构观望居多 尽管VR在媒体和二级市场炒得很热,但大多风险投资机构却慎于出手,以观望 ...
- 日常英语---十三、MapleStory/Monsters/Level 11-20(邪恶之眼)
日常英语---十三.MapleStory/Monsters/Level 11-20(邪恶之眼) 一.总结 一句话总结: evil ['ivl] A stronger version of Evil E ...
- 洛谷P1126机器人搬重物[BFS]
题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有些格子为不可移动的障碍.机 ...
随机推荐
- UVa455 Periodic Strings
#include <stdio.h>#include <string.h> int main(){ int T, k, len; char str[81], *p, ...
- [LeetCode]题解(python):002-Add Two Numbers
题目来源: https://leetcode.com/problems/add-two-numbers/ 题意分析: 这道题目是要将两个单链条相加.输出得到的新链条. 题目思路: 不难发现,其实题目就 ...
- Object-c KVC的使用和举例
如果我们的对象需要使用KVC,必须符合object-c的非正式协议NSKeyValueCoding.我们可以简单的来理解KVC,即所有符合KVC机制的对象都看成一个字典(NSDictionary),对 ...
- perl5 第十章 格式化输出
第十章 格式化输出 by flamephoenix 一.定义打印格式二.显示打印格式三.在打印格式中显示值 1.通用的打印格式 2.格式和局域变量 3.选择值域格式 4.输出值域字符四.输出到 ...
- Swift与Objective-C API的交互
互用性是让 Swift 和 Objective-C 相接合的一种特性,使你能够在一种语言编写的文件中使用另一种语言.当你准备开始把 Swift 融入到你的开发流程中时,你应该懂得如何利用互用性来重新定 ...
- HDU 1465 不容易系列之排错
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 大家常常感 ...
- man命令重定向后有^H乱码问题
在 man ld.so>ld.so后 vim打开ld.so后出现重叠乱码问题 但是cat.less可以正常查看 解决办法: man ld.so|col -b >ld.so col命令是 ...
- is,as,sizeof,typeof,GetType
这几个符号说来也多多少少的用过,今天就根据ProC#的讲述来总结一下: 1. IS: 检查变量类型是否与指定类型相符,返回True ,False.不报错. 老实说,我没怎么用过.看看下 ...
- OC中属性readwrite,readonly,assign,retain,copy,nonatomic 各是什么作用,在那种情况下用?
此次只做简单说明,不做代码演示! 1> readwrite:同时生成get方法和set方法的声明和实现 2> readonly:只生成get方法的声明和实现 3> assign:se ...
- Java中 hashCode()方法详解
先来看下Object源码里hashcode方法: /** * Returns a hash code value for the object. This method is * s ...