题解:P2537 [AHOI2005] 穿越磁场

· · 题解

题目分析

读题可知,这是从机器人位置到矿石求最小代价的问题。首先想到的是 0-1 最短路,但我们发现,虽然 n 只有 100,但是网格很大,高达 10000 \times 10000,很明显不能用二维数组模拟。如果你做过 P1884 Overplanting S,就知道这题要使用离散化,于是我们就能先分析出这样的代码。

//离散化操作
X[++cntx] = sx, X[++cntx] = ex;
Y[++cnty] = sy, Y[++cnty] = ey;
sort(X + 1, X + 1 + cntx);
sort(Y + 1, Y + 1 + cnty);
lenx = unique(X + 1, X + 1 + cntx) - X - 1;
leny = unique(Y + 1, Y + 1 + cnty) - Y - 1;
//取出终点与起点离散化后的坐标
sx = lower_bound(X + 1, X + 1 + lenx, sx) - X;
sy = lower_bound(Y + 1, Y + 1 + leny, sy) - Y;
ex = lower_bound(X + 1, X + 1 + lenx, ex) - X;
ey = lower_bound(Y + 1, Y + 1 + leny, ey) - Y;
calc();//计算每个方向的代价
bfs(sx, sy, ex, ey);//0-1 BFS

接下来我们分析每个方向的代价:

方向 跨越的线 代价存在
右 (i+1,j) X_{i+1} R_{i,j}
左 (i-1,j) X_{i} L_{i,j}
下 (i,j+1) Y_{j+1} D_{i,j}
上 (i,j-1) Y_{j} U_{i,j}

那么在经过磁场边缘时,对应方向的代价就为 1,具体实现看代码。

在存磁场边缘时,记得把地图边界存进去。否则机器人会走出场外。

AC CODE

#include<bits/stdc++.h>
using namespace std;
int n, sx, sy, ex, ey, X[2005], Y[2005], cntx, cnty, L[205][205], R[205][205], U[205][205], D[205][205];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, -1, 1};
int lenx, leny, dis[205][205];
struct Square {
    int x, y, c;
} sq[105];//定义磁场数组
//计算代价
void calc() {
    for(int k=1;k<=n;k++){//枚举磁场
        int xa=lower_bound(X+1,X+1+lenx,sq[k].x)-X;
        int xb=lower_bound(X+1,X+1+lenx,sq[k].x+sq[k].c)-X;
        int ya=lower_bound(Y+1,Y+1+leny,sq[k].y)-Y;
        int yb=lower_bound(Y+1,Y+1+leny,sq[k].y+sq[k].c)-Y;
        //计算各个方向的代价
        for(int i=xa;i<xb;i++){
            D[i][ya-1]=U[i][ya]=1;
            D[i][yb-1]=U[i][yb]=1;
        }
        for(int j=ya;j<yb;j++){
            R[xa-1][j]=L[xa][j]=1;
            R[xb-1][j]=L[xb][j]=1;
        }
    }
}
//0-1 BFS,也可以用Dijkstra
void bfs(int sx, int sy, int tx, int ty) {
    deque<pair<int, int>> dq;
    dq.push_back({sx, sy});
    memset(dis, 0x3f, sizeof dis);
    dis[sx][sy] = 0;
    while(dq.size()) {
        int hx = dq.front().first;
        int hy = dq.front().second;
        dq.pop_front();
        if(hx == tx && hy == ty) {
            cout << dis[hx][hy];
            return;
        }
        for(int i = 0; i<4; i++) {
            int nx = hx + dx[i];
            int ny = hy + dy[i];
            if(nx<1 || lenx<=nx || ny<1 || leny<=ny)
                continue;
            int cont = 0;
            if(i == 0) cont = R[hx][hy];
            if(i == 1) cont = L[hx][hy];
            if(i == 2) cont = U[hx][hy];
            if(i == 3) cont = D[hx][hy];
            if(dis[hx][hy] + cont<dis[nx][ny]) {
                dis[nx][ny] = dis[hx][hy] + cont;
                if(cont) dq.push_back({nx, ny});
                else dq.push_front({nx, ny});
            }
        }
    }
    cout<<-1;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;
    //一定要记得要把地图边界存进去
    X[++cntx] = 0;
    Y[++cnty] = 0;
    X[++cntx] = 10000;
    Y[++cnty] = 10000;
    for (int i = 1; i <= n; i++) {
        cin >> sq[i].x >> sq[i].y >> sq[i].c;
        X[++cntx] = sq[i].x;
        Y[++cnty] = sq[i].y;
        X[++cntx] = sq[i].x + sq[i].c;
        Y[++cnty] = sq[i].y + sq[i].c;
    }
    cin >> sx >> sy >> ex >> ey;
    //离散化操作
    X[++cntx] = sx, X[++cntx] = ex;
    Y[++cnty] = sy, Y[++cnty] = ey;
    sort(X + 1, X + 1 + cntx);
    sort(Y + 1, Y + 1 + cnty);
    lenx = unique(X + 1, X + 1 + cntx) - X - 1;
    leny = unique(Y + 1, Y + 1 + cnty) - Y - 1;
    sx = lower_bound(X + 1, X + 1 + lenx, sx) - X;
    sy = lower_bound(Y + 1, Y + 1 + leny, sy) - Y;
    ex = lower_bound(X + 1, X + 1 + lenx, ex) - X;
    ey = lower_bound(Y + 1, Y + 1 + leny, ey) - Y;
    calc();//计算每个方向的代价
    bfs(sx, sy, ex, ey);//0-1 BFS
    return 0;
}