Patrol Robot(BFS/DFS)

A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (ij)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 (xy) to (x + 1, y)(xy + 1)(x - 1, y) or (xy - 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 (mn). 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$ \le$mn$ \le$20). The second line contains an integer number k (0$ \le$k$ \le$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 ofaij is 1 if there is an obstacle on the cell (ij), and is 0 otherwise.

Output 

For each data set, if there exists a way for the robot to reach the cell (mn), 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(m和n的范围都在1到20的闭区间内)的网格的左上角(1,1)走到右下角(m,n)。网格中的一些格子是空地,用0表示,其它格子是障碍,用1表示。机器人每次可以往四个方向走一格,但不能连续地穿越k( [0,20] )个障碍,求最短路长度。起点和终点保证是空地。


思路:用bfs搜索即可,由于不能连续地穿越k个障碍,所以在原本的vis2维数组上面再添加1维,变成3维数组,表示穿越的墙的层数(障碍)。开三维数组的原因是因为可能存在穿越不同障碍到达(x,y)点所以每种都要标记一下


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn =1e6;
const ll inf=0x3f3f3f3f;
int n,m,k,t;
int maze[25][25];
int vis[25][25][25];
int dir[4][2]={1,0,0,1,-1,0,0,-1};
struct node
{
    int x;
    int y;
    int step;
    int layer;
};
int BFS()
{
    memset(vis,0,sizeof(vis));
    node head,start,t;
    start.x=1;
    start.y=1;
    start.step=0;
    start.layer=0;
    vis[1][1][0]=1;
    queue<node> Q;
    Q.push(start);

    while(!Q.empty())
    {
        head=Q.front();
        Q.pop();
        if(head.x==n && head.y==m)
            return head.step;
        for(int i=0;i<4;i++)
        {
            int xx=head.x+dir[i][0];
            int yy=head.y+dir[i][1];
            int layer=head.layer;
            if(maze[xx][yy])
                layer++;
            else
                layer=0;
            if(layer<=k && xx>=1 && xx<=n && yy>=1 && yy<=m && !vis[xx][yy][layer])
            {
                vis[xx][yy][layer]=1;
                t.x=xx;
                t.y=yy;
                t.step=head.step+1;
                t.layer=layer;
                Q.push(t);
            }

        }
    }


    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",&maze[i][j]);
           }
       }
       printf("%d\n",BFS());
   }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn =1e6;
const ll inf=0x3f3f3f3f;
int mp[25][25], vis[25][25][25];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int n, m, k;

int dfs(int x, int y, int stp, int tmp)
{
    if(x==n && y==m) return stp;

    int minn=inf;
    for(int i=0;i<4;i++)
    {
        int tx=x+dx[i];
        int ty=y+dy[i];
        int tp=tmp;

        if(tx<1 || ty<1 || tx>n || ty>m) continue;
        else{
            if(mp[tx][ty]==1) tp++;
            else tp=0;

            if((vis[tx][ty][tp]==0 || vis[tx][ty][tp]>stp+1) && tp<=k)
            {
                vis[tx][ty][tp]=stp+1;
                minn=min(minn, dfs(tx, ty, stp+1, tp));
            }

        }

    }

    return minn;

}
int main()
{
    int t; cin>>t;
    while(t--){
        cin>>n>>m>>k;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin>>mp[i][j];
        memset(vis,0,sizeof(vis));

        int t=dfs(1, 1, 0, 0);

        if(t==inf) puts("-1");
        else cout<<t<<endl;
    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41021816/article/details/80627513