题解 AT2375 【[AGC014C] Closed Rooms】

· · 题解

一轮为先移动至多 K 次再把 K# 变为 .

这样感觉不好处理,我们把它转换一下:

如果这么做,在接下来的每一轮中,直接解开我要走的那 K 步的格子上的锁就可以了。

所以这题就是问第一轮最多可以走到离边界多近的地方。
直接 bfs 走 K 层,问题解决。
设最近距离为 x,一轮走 K,再加上第一步,答案就是 \lceil \frac{x}{K} \rceil+1

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;

LL vis[805][805] = {0};
char ch[805][805];
LL n,m,k,ans = INF;
LL dx[4] = {0,0,1,-1};
LL dy[4] = {1,-1,0,0};

struct node{
    LL x,y,stp;
}h,t; queue <node> q;
void bfs(LL x,LL y){
    t.x = x; t.y = y; t.stp = 0; q.push(t);
    while(!q.empty()){
        h = q.front(); q.pop();
        if(h.stp > k) continue;
        vis[h.x][h.y] = 1;
        ans = min(ans,min(min(h.x - 1,h.y - 1),min(n - h.x,m - h.y)));
        for(LL i = 0;i < 4;i ++){
            LL tx = h.x + dx[i],ty = h.y + dy[i];
            if(!tx || !ty || tx > n || ty > m) continue;
            if(vis[tx][ty] || ch[tx][ty] == '#') continue;
            vis[tx][ty] = 1;
            t.x = tx; t.y = ty; t.stp = h.stp + 1;
            q.push(t);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    LL sx = 0,sy = 0;
    cin >> n >> m >> k;
    for(LL i = 1;i <= n;i ++){
        for(LL j = 1;j <= m;j ++){
            cin >> ch[i][j];
            if(ch[i][j] == 'S'){ sx = i; sy = j; }
        }
    }
    bfs(sx,sy);
    cout << (ans + k - 1) / k + 1 << endl;
    return 0;
}

原评分 \sf{\color{blue}1905}
感觉没这个难度吧,还是比较好想的,前置知识只有 bfs,代码难度(实现难度)低。