【学习宽搜 不如进来看看】Patrol Robot UVA - 1600(条件最短路;bfs进阶)

题目

Problem
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
Source
https://vjudge.net/problem/UVA-1600

大概翻译一下:

机器人在 m*n 的迷宫内,从出发点(1,1)到终点(m, n),每次只能向 上 下 左 右 其中一个方位移动一格,但他可以连续穿过 k (可以为零)面墙,用 0 和 1 来描绘地图,0表示无障碍,1表示此处有面墙,若能抵达终点,输出最小步数;反之输出 “-1”。

分析:

机器人具有连续穿过 k 面墙的BUFF,那么自然也就带来了一些问题,解决完这些问题,题目也就解决了:

1.当机器人在某个位置向某个方位搜索时(不能原地踏步哈),没墙当然要走,但如果有墙呢?
答:当然也要走,宽搜要求连续遍历全图,地图不大(20*20),不用压缩也可遍历所有可能

2.如果机器人和终点只隔一面墙,穿一面墙再走一步就到了,如果绕路则很有可能就不是最短路,甚至无法到达;但是如果隔了许多面墙或者 k 很小,穿墙就不可取,这时绕路反而是唯一的选择,那么我们该如何抉择?
答:首先我们要遍历所有可能抵达的地方以及抵达该位置可能的穿墙数,因此 穿墙数 和 当前已走步数 没有谁更优先,但最终以到达终点所用步数少者为准。
如何记录呢?因为有了“连续”穿墙的限制,我们需要想办法记录 某条路径上 在某点 已连续穿过的墙的个数,结构体是不错的选择(多开个变量就好)。
与此同时只用二维数组记录步数也是无法满足需求啦,我们可以开三维数组记录 “在该坐标下 已连续穿过 bs 面墙 所需的步数,然后在搜索过程中按 最小值 更新就好。
换句话说,队列内 可能 会同时存在(x, y, 1) (x, y, 0)两个结构体,如果它们步数不同,就没有谁高谁低,因为我们要遍历所有的可能

3.一旦搜索到立刻输出么?会不会队列内还有较优解?
答:队列内有可能存在 穿墙次数比当前少 的路径,但是不可能存在 步数比现在少 的位置了,也就是说,队列内所有结构体的步数差一定是 ≤1 的,所以遍历到终点直接输出就好

4.注意“连续”的概念,如果已经连续穿了2面墙下一步不穿墙,那么连续就被中断了

其他细节参考下码

实现

/* 头文件略 */
using namespace std;
const int INF = 0x3f3f3f3f;
int dx[] = {-1, 0,0,1}; //数组记录四个方位
int dy[] = { 0,-1,1,0};
struct P {
    int x, y; //当前位置横纵坐标
    int bs; //当前位置已连续穿墙数
    P(int x = 0, int y = 0, int bs = 0):
        x(x), y(y), bs(bs) {}
};
int r, c, k;
inline bool in_border (int mx, int my) { //判断出界
    return mx>=0 && mx<r && my>=0 && my<c;
}
int d[25][25][25]; //d[x][y][bs] 表示到达(x,y)位置连续穿bs面墙所需步数 这个是随着搜索而更新的
int s[25][25]; //记录地图
void bfs() {
    memset(d, INF, sizeof(d)); //当然了初始值是要有的
    d[0][0][0] = 0; //终点和起点重合就不用走了
    queue<P> q;
    q.push(P(0, 0, 0));
    while(!q.empty()) {
        P tmp = q.front(); q.pop();
        if(tmp.x == r-1 && tmp.y == c-1) { //如果到了直接输出就好
            cout << d[r-1][c-1][tmp.bs] << endl;
            return;
        }
        for(int i = 0; i < 4; i++) {
            int mx = tmp.x + dx[i], my = tmp.y + dy[i]; //下一步
            if(!in_border(mx, my)) continue;
            if(s[mx][my]) { //下一步要穿墙
                if(tmp.bs == k) continue; //穿不动了
                if(d[mx][my][tmp.bs+1] > d[tmp.x][tmp.y][tmp.bs] + 1) {
                    d[mx][my][tmp.bs+1] = d[tmp.x][tmp.y][tmp.bs] + 1; //更新数组
                    q.push(P(mx, my , tmp.bs+1));
                }
            }
            else { //下一步无障碍
                if(d[mx][my][0] > d[tmp.x][tmp.y][tmp.bs] + 1) {
                    d[mx][my][0] = d[tmp.x][tmp.y][tmp.bs] + 1; //鉴于“连续” 既然无障碍也就不连续了
                    q.push(P(mx, my , 0));
                }
            }
        }
    }
    cout << "-1" << endl; //遍历所有可能 机器人仍无法抵达终点
}
int main() {int T; cin >> T; getchar();
    while(T--) {
        cin >> r >> c >> k;
        for(int i = 0; i < r; i++)
            for(int j = 0; j < c; j++)
                cin >> s[i][j];
        bfs();
    }
    return 0;
}

中国加油!!!ㄟ(≧◇≦)ㄏ

发布了54 篇原创文章 · 获赞 43 · 访问量 1933

猜你喜欢

转载自blog.csdn.net/Jungle_st/article/details/104766975
今日推荐