巡逻机器人问题(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.要将走过的点进行标记 代码:
 #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)的更多相关文章

  1. BFS 巡逻机器人

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

  2. UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)

    非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...

  3. UVA - 1600 Patrol Robot (巡逻机器人)(bfs)

    题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路. 分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可. #pragma commen ...

  4. UVA:1600 巡逻机器人

    机器人要从一个m*n(m和n的范围都在1到20的闭区间内)的网格的左上角(1,1)走到右下角(m,n).网格中的一些格子是空地,用0表示,其它格子是障碍,用1表示.机器人每次可以往四个方向走一格,但不 ...

  5. UVa——1600(巡逻机器人)

    迷宫求最短路的话一般用bfs都可以解决,但是这个加了个状态,那么就增加一个维度,用来判断k的值.比较简单的三维bfs.写搜索题的话一定要注意细节.这个题花了好长的时间.因为k的原因,一开始用了k的原因 ...

  6. 6-5 巡逻机器人 uva1600

    一开始按照标准bfs来写  标记为二维数组 后来按照三维数组写过了    ps大部分bfs都不会是二维数组搞定!!! 其中有一个bug弄了半个小时... 一开始我是先判断!vis[x][y][v.c] ...

  7. 中国VR公司的详尽名单

    中国VR公司的详尽名单   <VR圈深度投资报告一:2014年以来所有VR/AR融资事件> 特征一.投资机构观望居多 尽管VR在媒体和二级市场炒得很热,但大多风险投资机构却慎于出手,以观望 ...

  8. 日常英语---十三、MapleStory/Monsters/Level 11-20(邪恶之眼)

    日常英语---十三.MapleStory/Monsters/Level 11-20(邪恶之眼) 一.总结 一句话总结: evil ['ivl] A stronger version of Evil E ...

  9. 洛谷P1126机器人搬重物[BFS]

    题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有些格子为不可移动的障碍.机 ...

随机推荐

  1. 重定向输入输出流--freopen

    freopen是被包含于C标准库头文件<stdio.h>中的一个函数,用于重定向输入输出流.该函数可以在不改变代码原貌的情况下改变输入输出环境. C99函数声明: FILE *freope ...

  2. C单链表实现

    /* * LinkNode.c * * Created on: Jan 14, 2014 * Author: root */ #include <stdlib.h> #include &l ...

  3. ubuntu vim终端编辑命令

    一. VIM高亮 进入vim后,在普通模式下输入如下命令,开启php代码高亮显示   :syntax enable   :source $VIMRUNTIME/syntax/php.vim   二. ...

  4. 关于switch的思考和总结

    1.通常每个case的末尾都应该加个break; 否则会default分支也会被执行 var score = 40;switch (score){case 50:console.log('50');/ ...

  5. Delphi函数指针的两种定义(对象方法存在一个隐藏参数self,所以不能相互赋值)

    delphi中经常见到以下两种定义 Type TMouseProc = procedure (X,Y:integer); TMouseEvent = procedure (X,Y:integer) o ...

  6. spring学习总结(mybatis,事务,测试JUnit4,日志log4j&slf4j,定时任务quartz&spring-task,jetty,Restful-jersey等)

    在实战中学习,模仿博客园的部分功能.包括用户的注册,登陆:发表新随笔,阅读随笔:发表评论,以及定时任务等.Entity层设计3张表,分别为user表(用户),essay表(随笔)以及comment表( ...

  7. JAVA GUI学习 - JSplitPane分屏组件学习

    public class JSplitPaneKnow extends JFrame { JSplitPane jSplitPane; JPanel jPanelRed; JPanel jPanelB ...

  8. 如何开始一个模块化可扩展的Web App(转)

    原文链接:http://avnpc.com/pages/start-a-modular-extensible-webapp 日志未经声明,均为AlloVince原创.版权采用『 知识共享署名-非商业性 ...

  9. Android学习笔记(十七)——使用意图调用内置应用程序

    使用意图调用内置应用程序 1.创建一个新的Android项目并命名为Intents,在main.xml文件里加入两个Button: <Button android:id="@+id/b ...

  10. ThinkPHP - 事务操作

    /* * 添加酒店和房型 * */ public function insertAll($arr_hotel=array(),$arr_room=array()){ $model = new Mode ...