1600 - Patrol Robot

题目传送门

A robot has to patrol around a rectangular area which is in a form of m × n 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 ≤ m, n ≤ 20). The second line contains an integer number k (0 ≤ k ≤ 20). The i-th 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

题意

题目给你一个图,就是让你类似于走迷宫的样子,让你从 ( 1 1 (1,1) 点走到 n m (n,m) 点的最小步数(不能走到目的地则输出 1 -1 ),地图上1表示有障碍物,0表示没有障碍物,提上还会给你一个k表示,在走的过程中可以走障碍物,但是不能连续走超过k个障碍物。

思路

如果没有后面那个约束条件的话,其实就是一个很普通的bfs,但是加了后面的条件之后,我们其实可以用vis数组标记,普通bfs中vis数组标记的是走过的路,不能再走回去,但是我们可以再开一个维度,表示走到这个点是在连续走了几个障碍物的情况下走到这的。vis[i][j][k]就是走到在连续走了k个障碍物之后,走到了(i,j)这个点,就这和普通bfs有点小小的改动区别,别的就是什么坑了。

AC代码

#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<math.h>
#include<map>
using namespace std;
#define LL long long
const double PI=acos(-1.0);
const double eps=1e-8;
const int N=1e6+10;
int mp[150][150];
int vis[150][150][150];
int n,m,k;
struct zxc
{
    int x,y,step,w;
}st,en;
int mv[4][2]={1,0,0,1,-1,0,0,-1};
int bfs()
{   queue<zxc>q;
    memset(vis,0,sizeof(vis));
    q.push(st);
    vis[1][1][0]=1;
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        if(st.x==n&&st.y==m)
        {
            return st.step-1;
        }
        for(int i=0;i<4;i++)
        {
            en.x=st.x+mv[i][0];
            en.y=st.y+mv[i][1];
            if(en.x>0&&en.x<=n&&en.y>0&&en.y<=m)
            {
                    if(mp[en.x][en.y]==0)
                    {
                        en.w=0;
                        if(vis[en.x][en.y][0]==0)
                        {
                            vis[en.x][en.y][0]=1;
                            en.step=st.step+1;
                            q.push(en);
                        }

                    }
                    else
                    {
                        if(st.w+1<=k)
                        {
                            en.w=st.w+1;
                            if(vis[en.x][en.y][en.w]==0)
                            {
                                vis[en.x][en.y][en.w]=1;
                                en.step=st.step+1;
                                q.push(en);
                            }
                        }
                    }
            }

        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&mp[i][j]);
            }
        }
        st.x=1,st.y=1;
        st.w=0;
        st.step=1;
        printf("%d\n",bfs());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43402296/article/details/105783049