最短路

· · 个人记录

P1735 字母迷宫

非常经典的bfs。

海洋之门,时空之门正常处理即可,注意海洋之门是只花费一步,跳两格。

天堂之门:由于有聚气要消耗一步,所以在结构体中多开一个bool量表示是否聚气,如果要聚气,就要continue,相当于原地踏步,不进行移动。

多起点一起入队,特判终点或起点是否为障碍。

#include<bits/stdc++.h>
using namespace std;
int n;
char mp[1001][1001];
bool vis[1001][1001];
int dx1[4]={0,0,1,-1};
int dy1[4]={1,-1,0,0};
int dx2[4]={1,1,-1,-1};
int dy2[4]={-1,1,1,-1};
struct node {
    int x,y,step;
    bool bq;
};
bool check(int x,int y){
    return (x>=1&&x<=n&&y>=1&&y<=n&&mp[x][y]!='*'&&vis[x][y]==0);
}
int bfs(){
    queue<node>q;
    if(mp[1][1]!='*'){
    //  vis[1][1]=1;
        q.push({1,1,1,0});
    }
    if(mp[1][n]!='*'){
    //  vis[1][n]=1;
        q.push({1,n,1,0});
    }
    if(mp[n][1]!='*'){
    //  vis[n][1]=1;
        q.push({n,1,1,0});
    }
    while(!q.empty()){
        node t=q.front();
        q.pop();
        int tx=t.x,ty=t.y,t_st=t.step;

        int k=1;
        if(mp[tx][ty]=='A'||mp[tx][ty]=='B'){
            if(mp[tx][ty]=='B'){
                k++;
            }
            for(int i=0;i<4;i++){
                int nx=tx+dx1[i]*k,ny=ty+dy1[i]*k;
                if(check(nx,ny)){
                    vis[nx][ny]=1;
                    if(nx==n&&ny==n){
                        return t_st+1;
                    }

                    q.push({nx,ny,t_st+1,0});
                }
            }
        }else if(mp[tx][ty]=='C'){
            if(t.bq==0){
                q.push({tx,ty,t_st+1,1});
                continue;
            }
            for(int i=0;i<4;i++){
                int nx=tx+dx2[i],ny=ty+dy2[i];
                if(check(nx,ny)){
                    vis[nx][ny]=1;
                    if(nx==n&&ny==n){
                        return t_st+1;
                    }

                    q.push({nx,ny,t_st+1,0});
                }
            }
        }
    }
    return -1;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>mp[i][j];
        }
    }
    if(mp[n][n]=='*'){
        cout<<"No answer";
        return 0;
    }
    int ans=bfs();
    if(ans==-1){
        cout<<"No answer";
    }
    else cout<<ans;
    return 0;
}

T2:Rescue

使用算法:bfs+优先队列

为什么使用优先队列:由于普通bfs只能处理边权相同的情况,本题中普通点边权为1,而守卫边权为2,所以使用优先队列,保证每次扩展的都是距离最短的点,实质是dijkstra。

时间复杂度o(nmlog(nm))

#include<bits/stdc++.h>
using namespace std;

int n, m;
char mp[201][201];
int sx, sy, ex, ey;
int dis[201][201];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

struct node {
    int x, y, dis;
    bool operator<(const node& other) const {
        return dis > other.dis;
    }
};

bool check(int x, int y) {
    return (x >= 0 && x < n && y >= 0 && y < m && mp[x][y] != '#');
}

void bfs() {

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            dis[i][j] = 1e9;
        }
    }

    priority_queue<node> q;
    q.push({sx, sy, 0});
    dis[sx][sy] = 0;

    while(!q.empty()) {
        node t = q.top();
        q.pop();
        if(t.dis > dis[t.x][t.y]) continue;
        if(t.x == ex && t.y == ey) {
            cout << t.dis << endl;
            return;
        }

        for(int i = 0; i < 4; i++) {
            int nx = t.x + dx[i];
            int ny = t.y + dy[i];

            if(!check(nx, ny)) continue;

            int new_dis = t.dis + 1;
            if(mp[nx][ny] == 'x') {
                new_dis = t.dis + 2; 
            }

            if(new_dis < dis[nx][ny]) {
                dis[nx][ny] = new_dis;
                q.push({nx, ny, new_dis});
            }
        }
    }
    cout << "Poor ANGEL has to stay in the prison all his life." << endl;
}

int main() {
    while(cin >> n >> m) {
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                cin >> mp[i][j];
                if(mp[i][j] == 'a') {
                    ex = i, ey = j; 
                }
                if(mp[i][j] == 'r') {
                    sx = i, sy = j;
                }
            }
        }
        bfs();
    }
    return 0;
}