巡逻机器人问题(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, n20). The second line contains an integer number k(0k20). 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, n20)网格的左上角(1,1)走到右下角(M,N)。网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示)。机器人每次可以往4个方向走一格,但不能连续的穿越k(0k20)个障碍,求最短路长度。起点和终点保证是空地。

分析:求最短路径,即最优解问题,采用BFS。1.可以跨过一个障碍,但不能连续跨障碍。2.边界点没有4个方向3.要将走过的点进行标记

代码:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<queue>
  5 using namespace std;
  6
  7 #define MAX 23
  8
  9 int a[MAX][MAX];
 10 int nxt[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};
 11 int book[MAX][MAX][MAX];
 12
 13 int n,m,k;
 14
 15 struct node
 16 {
 17     int x,y;
 18     int step;
 19     int kk;
 20 };
 21 queue<node>Q;
 22
 23 void init()
 24 {
 25     while (!Q.empty())
 26     {
 27         Q.pop();
 28     }
 29     memset(book,0,sizeof(book));
 30 }
 31
 32 int can_move( int x ,int y,int kk )
 33 {
 34     if ( x>=1&&x<=n&&y>=1&&y<=m&&book[x][y][kk]==0 )
 35         return 1;
 36     else
 37         return 0;
 38 }
 39
 40 int bfs ( node start )
 41 {
 42     init();
 43     if ( start.x==n&&start.y==m )
 44     {
 45         return start.step;
 46     }
 47     Q.push(start);
 48     while ( !Q.empty() )
 49     {
 50         node now = Q.front();
 51         Q.pop();
 52         if ( now.kk < 0 )
 53             continue;
 54         for ( int i = 0;i < 4;i++ )
 55         {
 56             node newnode;
 57             int tx = now.x+nxt[i][0], ty = now.y+nxt[i][1];
 58             if ( a[tx][ty]==1 )
 59                 newnode.kk = now.kk-1;
 60             else
 61                 newnode.kk = k;
 62             if ( can_move(tx,ty,newnode.kk) )
 63             {
 64                 if ( tx==n&&ty==m )
 65                 {
 66                     return now.step+1;
 67                 }
 68                 newnode.x = tx; newnode.y = ty; newnode.step = now.step+1;
 69                 if( newnode.kk >= 0 )
 70                 {
 71                     Q.push(newnode);
 72                     book[tx][ty][newnode.kk] = 1;
 73                 }
 74             }
 75         }
 76     }
 77
 78     return -1;
 79 }
 80
 81 int main(void)
 82 {
 83     int t;scanf("%d",&t);
 84     while ( t-- )
 85     {
 86         scanf("%d%d%d",&n,&m,&k);
 87         for ( int i = 1;i <= n;i++ )
 88         {
 89             for ( int j = 1;j <= m;j++ )
 90             {
 91                 scanf("%d",&a[i][j]);
 92             }
 93         }
 94         node start;
 95         start.x = 1; start.y = 1;start.step = 0; start.kk = k;
 96         book[start.x][start.y][start.kk] = 1;
 97         int ans = bfs(start);
 98         if ( ans == -1 )
 99             printf("-1\n");
100         else
101             printf("%d\n",ans);
102     }
103     return 0;
104 }

View Code

心得:

BFS的题自己还是不会写,要看别人AC的代码才会。还是要多看一些别人的优秀代码来仿写,加油吧,争取自己能写好的代码。

转载于:https://www.cnblogs.com/ttmj865/p/4679516.html

巡逻机器人(BFS)相关推荐

  1. UVa1600 PatrolRobot 巡逻机器人(bfs)

    题目大意:给定一个网络(1m,n20)m是row,n是col,求从(1,1)到点(m,n)的最短路径,其中0是空地,1是障碍物,给定k,表示不能连续穿过k个障碍物, 方法:bfs,其中必须要注意的是每 ...

  2. BFS 巡逻机器人

    巡逻机器人 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/F 题目大意: 机器人在一个矩形区域巡逻, ...

  3. UVA 1600 巡逻机器人

    巡逻机器人 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu 题意·:一个机器人从(1,1)点走到( ...

  4. 瓦力机器人故障维修_大眼萌!5G巡逻机器人亮相乌镇,24小时值守互联网大会...

    大大的眼睛,方方的身体, 一台酷似"瓦力"的机器人 出现在互联网之光新展馆. 原来这是嘉兴公安的"新同事"-5G巡逻机器人,今天是它第一天上岗.这也是本次世界互 ...

  5. 巡逻机器人用应用的pc端车牌识别

    PC端车牌识别产品形态 加密锁.车牌识别SDK开发包.开发文档 pc端车牌识别使用背景 人工智能的发展,京东送件也用上了送件机器人,某机器人公司如今研发出巡逻机器人,用于对可以车辆的抓拍与检查,在停车 ...

  6. 安防巡逻机器人在不同应用场景下的作用是什么?

    目前,传统的安防行业正在加速人工智能在该领域的集成和应用.随着人工智能安防技术进一步加强,如今,很多城市都开始纷纷打造一座智慧型城市,安防机器人也普遍出现在各个行业当中,下面,国辰机器人就为大家来盘点 ...

  7. 工业机器人 郝卫东_智能保安巡逻机器人论文

    第 1 页 共 5 页 智能保安巡逻机器人 黄海明 杨雷 宋跃 赖思沅 ( 东莞理工学院电子工程学院,广东 东莞 523808) 摘 要: 设计一个具有自动远程值守. GPRS 遥控. 远程监控等功能 ...

  8. 水下自动循迹机器人_一种夜间巡逻机器人自动循迹方法

    一种夜间巡逻机器人自动循迹方法 [技术领域] [0001]本发明涉及机器人应用技术领域,尤其涉及一种夜间巡逻机器人自动循迹方法. [背景技术] [0002]目前,博物馆.会展中心以及公园等场所的夜间安 ...

  9. 明光市机器人_明光市安保巡逻机器人在线咨询

    明光市安保巡逻机器人在线咨询hagj 根据IFR统计数据显示,2019年服务机器人市场规模约22亿美元,约占全球25%市场份额,电子学会预测未来这一比例有望达到30%.2015年到2019年间,服务机 ...

最新文章

  1. numpy使用[]语法索引二维numpy数组中指定指定列之后所有数据列的数值内容(accessing columns in numpy array after specifc column)
  2. 计算机应用虚拟仿真实验答案,虚拟仿真 实验教学+.ppt
  3. java中集合类的转换_Java中的两个常用工具类及集合数组的相互转换
  4. UserWarning: The default mode, 'constant', will be changed to 'reflect'
  5. (23)System Verilog旗语解决资源共享需求
  6. php core模块,module.php
  7. 漫话:如何给女朋友解释什么是撞库、脱库和洗库?
  8. 2018我读过的那些书
  9. photoshop印章效果制作
  10. S3C2440 SDRAM内存驱动
  11. React Native 布局实现测试
  12. 【753. 破解保险箱】
  13. Android视频播放器ExoPlayer
  14. 爬取起点小说总排行榜
  15. 众海世纪影业:五一档19部影片“扎堆”,能否再次掀起观影热潮?
  16. 南京航空航天大学计算机考研经验贴
  17. html消除图之间间距,html表格间距怎么取消 html 表格行之间的间距怎么设置
  18. 在 Domino 邮件服务器上配置 Verse On-Premises
  19. 【视频学习】VALSE短教程《因果发现与因果性学习》 蔡瑞初教授
  20. 138+134 新手寻星,三分钟搞定,呵呵,我明天才!!

热门文章

  1. mysql-8.0.21-winx64 安装
  2. Java攻略之API
  3. 静态成员变量与静态成员函数的声明与定义
  4. 全局刷新和局部刷新的理解
  5. PyQt实现按钮控件的拖动效果,利用鼠标移动事件实现。
  6. 判断当前时间是否在股票开盘时间,不考虑周六周日和节假日
  7. Github建立远程库,并从本地导入
  8. 递归学习_组合_全组合排列
  9. 智能LED电子钟的制作
  10. python关闭浏览器、未过期的session_解决因为关闭浏览器造成session失效的假象