巡逻机器人(BFS)

时间:2022-01-24 20:59:58

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

Description

巡逻机器人(BFS)
 

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 (1巡逻机器人(BFS)m, n巡逻机器人(BFS)20). The second line contains an integer number k(0巡逻机器人(BFS)k巡逻机器人(BFS)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(1巡逻机器人(BFS)m, n巡逻机器人(BFS)20)网格的左上角(1,1)走到右下角(M,N)。网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示)。
机器人每次可以往4个方向走一格,但不能连续的穿越k(0巡逻机器人(BFS)k巡逻机器人(BFS)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的代码才会。还是要多看一些别人的优秀代码来仿写,加油吧,争取自己能写好的代码。