Submission #668364

#TimeUsernameProblemLanguageResultExecution timeMemory
668364Do_you_copy포탈들 (BOI14_portals)C++17
100 / 100
510 ms157132 KiB
/*
    I find it wholesome to be alone the greater part of the time.
    To be in company, even with the best, is soon wearisome and dissipating.
    I love to be alone.
    I never found the companion that was so companionable as solitude.
*/
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1000 + 10;
//const int Mod = 1e9 + 7;
//const int inf =
int n, m;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int far[maxN][maxN][4];
string s[maxN];
int ans[maxN][maxN];

struct TNode{
    int fi, se, w;
    bool operator < (const TNode &other) const{
        return w > other.w;
    }
};

vector <TNode> adj[maxN][maxN];
void Dijkstra(pii u, pii v){
    priority_queue <TNode> PQ;
    PQ.push({u.fi, u.se, 0});
    memset(ans, 0x3f, sizeof(ans));
    ans[u.fi][u.se] = 0;
    while (!PQ.empty()){
        auto r = PQ.top(); PQ.pop();
        if (ans[r.fi][r.se] != r.w) continue;
        for (auto i: adj[r.fi][r.se]){
            if (ans[i.fi][i.se] > ans[r.fi][r.se] + i.w){
                ans[i.fi][i.se] = ans[r.fi][r.se] + i.w;
                PQ.push({i.fi, i.se, ans[i.fi][i.se]});
            }
        }
    }
}

void Solve(int d){
    if (d & 1){
        for (int i = 1; i <= n; ++i){
            for (int j = 1; j <= m; ++j){
                if (s[i][j] == '#'){
                    far[i][j][d] = -1;
                    continue;
                }
                far[i][j][d] = far[i - dx[d]][j - dy[d]][d] + 1;
            }
        }
    }
    else{
        for (int i = n; i >= 1; --i){
            for (int j = m; j >= 1; --j){
                if (s[i][j] == '#'){
                    far[i][j][d] = -1;
                    continue;
                }
                far[i][j][d] = far[i - dx[d]][j - dy[d]][d] + 1;
            }
        }
    }
}

void Init(){
    cin >> n >> m;
    for (int i = 0; i < m + 2; ++i) s[0].pb('#');
    for (int i = 0; i < m + 2; ++i) s[n + 1].pb('#');
    pii u, v;
    for (int i = 1; i <= n; ++i){
        cin >> s[i];
        s[i] = "#" + s[i] + "#";
        for (int j = 1; j <= m; ++j){
            if (s[i][j] == 'S') u = {i, j};
            else if (s[i][j] == 'C') v = {i, j};
        }
    }
    memset(far, -1, sizeof(far));
    for (int i = 0; i < 4; ++i)
        Solve(i);
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= m; ++j){
            pii minn = {0x3f3f3f3f, 0};
            for (int d = 0; d < 4; ++d){
                minn = min(minn, {far[i][j][d], d});
                if (s[i + dx[d]][j + dy[d]] != '#'){
                    adj[i][j].pb({i + dx[d], j + dy[d], 1});
                }
            }
            for (int d = 0; d < 4; ++d){
                if (far[i][j][d] == minn.fi) continue;
                int dist = minn.fi + 1;
                assert(dist > 0);
                adj[i][j].pb({i - far[i][j][d] * dx[d], j - far[i][j][d] * dy[d], dist});
            }
        }
    }
    Dijkstra(u, v);
    cout << ans[v.fi][v.se];
}

#define debug
#define taskname "test"
signed main(){
    faster
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    int tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
        timeout.close();
        #ifndef debug
        cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
        #endif // debug
    }
}

Compilation message (stderr)

portals.cpp: In function 'int main()':
portals.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
portals.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...