제출 #1346154

#제출 시각아이디문제언어결과실행 시간메모리
1346154kawhiet포탈들 (BOI14_portals)C++20
0 / 100
1 ms344 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 47
#endif

constexpr int dx[] = {1, -1, 0, 0};
constexpr int dy[] = {0, 0, 1, -1};
constexpr int inf = 1e9;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    n += 2;
    m += 2;
    vector<string> s(n);
    for (int i = 0; i < n; i++) {
        s[i].assign(m, '#');
        if (i > 0 && i < n - 1) {
            string t;
            cin >> t;
            s[i] = '#' + t + '#';
        }
    }
    vector<vector<int>> d(n, vector<int>(m, inf));
    queue<array<int, 2>> q;
    auto inside = [&](int i, int j) {
        return (i >= 0 && i < n && j >= 0 && j < m && d[i][j] == inf);
    };
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (s[i][j] == 'S') {
                d[i][j] = 0;
                q.push({i, j});
            }
        }
    }
    while (!q.empty()) {
        auto [x, y] = q.front();
        q.pop();
        if (s[x][y] == 'C') {
            cout << d[x][y] << '\n';
            return 0;
        }
        for (int k = 0; k < 4; k++) {
            int i = x + dx[k];
            int j = y + dy[k];
            if (s[i][j] == '#') {
                int x_nw = x, y_nw = y;
                while (s[x_nw - dx[k]][y_nw - dy[k]] != '#') {
                    x_nw -= dx[k];
                    y_nw -= dy[k];
                }
                if (d[x_nw][y_nw] > d[x][y] + 1) {
                    d[x_nw][y_nw] = d[x][y] + 1;
                    q.push({x_nw, y_nw});
                }
            } else {
                if (d[i][j] > d[x][y] + 1) {
                    d[i][j] = d[x][y] + 1;
                    q.push({i, j});
                }
            }
        }
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...