H - Patrol Robot 搜索

H - Patrol Robot

A robot has to patrol around a rectangular area which is in a form of m × n grid (m rows and ncolumns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (i, j) denotesthe cell in row i and column j in the grid. At each step, the robot can only move from one cell to anadjacent 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 thegrid contain obstacles. In order to move to a cell containing obstacle, the robot has to switch to turbomode. 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) fromcell (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 setswhich 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 nextm 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 theinteger 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

10 

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的次数。

还有一点是将走过的0变成2,后面就不会再走了。

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int d[4][2]={1,0,0,1,-1,0,0,-1},a,b,c,Map[22][22];

struct yun
{
    int x,y,s,w;
};

int bfs(int x,int y)
{
    yun st,en;
    queue<yun>Q;
    st.x=x;
    st.y=y;
    st.s=0;
    st.w=c;
    Q.push(st);
    while(Q.size())
    {
        st=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            int dx=st.x+d[i][0];
            int dy=st.y+d[i][1];
            if(dx<0||dy<0||dx>=a||dy>=b||Map[dx][dy]==2)continue;//别忘加Map[dx][dy]==2;
            if(Map[dx][dy]==1)
            {
                en.w=st.w-1;
                if(en.w<0)continue;
            }
            if(Map[dx][dy]==0)
            {
                en.w=c;
                Map[dx][dy]=2;
            }
            en.s=st.s+1;
            if(dx==a-1&&dy==b-1)
                return en.s;
            en.x=dx;
            en.y=dy;
            Q.push(en);
        }
    }
    return -1;
}

int main()
{
    int m,i,j;
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d%d",&a,&b,&c);
        for(i=0;i<a;i++)
            for(j=0;j<b;j++)
                scanf("%d",&Map[i][j]);
        printf("%d\n",bfs(0,0));
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值