1. 程式人生 > >2018, XI Samara Regional Intercollegiate Programming Contest H題- Safe Path

2018, XI Samara Regional Intercollegiate Programming Contest H題- Safe Path

題意:問你能否從起始點到達出發點。注意,在地圖的一些地區有怪獸,只要怪獸能在d步內到達你現在的位置,你會被立刻殺死。。

分析:BFS裸題,注意,N*M是2e5大小,把地圖進行壓縮一下~

程式碼如下:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int INF = 1e9;
const int maxn = 2e5+233;
queue<int>que;
int dis[maxn];
bool vis[maxn];
int n,m,d;
int S,F,tot;
char s[maxn];

inline bool check(int x){
   return x>=0 && x<n*m;
}

void bfs1(){
   int dir[] = {-1,1,-m,m};
   int now,tmp;
   while (!que.empty()) {
       now = que.front();
       que.pop();
       if (dis[now]>=d) continue;
       for (int i=0; i<4; i++) {
           tmp = dir[i]+now;
           if (!check(tmp)) continue;
           if (i<=1 && now/m != tmp/m) continue;
           if (dis[now]+1>=dis[tmp]) continue;
           dis[tmp] = dis[now]+1;
           vis[tmp] = 1;
           que.push(tmp);
       }
   }
}

int bfs2(){
    int dir[] = {-1,1,-m,m};
    while (!que.empty()) que.pop();
    dis[S] = 0;
    vis[S] = 1;
    que.push(S);

    int now,tmp;
    while (!que.empty()) {
        now = que.front();
        que.pop();
        if (now == F) return dis[now];
        for (int i=0; i<4; i++) {
            tmp = now + dir[i];
            if (!check(tmp)) continue;
            if (i<=1 && now/m != tmp/m) continue;
            if (vis[tmp]) continue;
            vis[tmp]  = 1;
            dis[tmp] = dis[now]+1;
            que.push(tmp);
        }
    }

    return -1;
}

int main(){
    scanf("%d%d%d",&n,&m,&d);
    memset(dis,0x7f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    while (!que.empty()) que.pop();
    tot = 0;
    for (int i=0; i<n; i++) {
        scanf("%s",s);
        for (int j=0; j<m; j++) {
            if (s[j]=='S') S = tot;
            else  if (s[j]=='F') F = tot;
            else  if (s[j]=='M') dis[tot] = 0, vis[tot] = 1,que.push(tot);
            tot++;
        }
    }

    bfs1();
    if (vis[S] || vis[F]) { printf("-1\n"); return 0; }
    printf("%d\n",bfs2());
    return 0;
}