Submission #668277

# Submission time Handle Problem Language Result Execution time Memory
668277 2022-12-03T13:47:30 Z Do_you_copy Portals (BOI14_portals) C++17
20 / 100
31 ms 64544 KB
/*
    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 = 2000 + 1;
//const int Mod = 1e9 + 7;
//const int inf =
int n, m;
bool visited[maxN][maxN];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int far[maxN][maxN][4];
string s[maxN];
int ans[maxN][maxN];
void bfs(pii u, pii v){
    queue <pii> Q;
    Q.push(u);
    visited[u.fi][u.se] = 1;
    while (!Q.empty()){
        auto r = Q.front(); Q.pop();
        for (int d = 0; d < 4; ++d){
            int i = r.fi + dx[d];
            int j = r.se + dy[d];
            if (s[i][j] != '#' && !visited[i][j]){
                visited[i][j] = 1;
                ans[i][j] = ans[r.fi][r.se] + 1;
                Q.push({i, j});
            }
            i = r.fi - far[r.fi][r.se][d] * dx[d];
            j = r.se - far[r.fi][r.se][d] * dy[d];
            if (visited[i][j]) continue;
            visited[i][j] = 1;
            ans[i][j] = ans[r.fi][r.se] + 1;
            Q.push({i, j});
        }
    }
}

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);
    bfs(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

portals.cpp: In function 'int main()':
portals.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
portals.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 29 ms 63000 KB Output is correct
2 Correct 23 ms 63076 KB Output is correct
3 Correct 25 ms 63052 KB Output is correct
4 Correct 24 ms 63052 KB Output is correct
5 Correct 24 ms 63052 KB Output is correct
6 Incorrect 27 ms 63060 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 23 ms 63076 KB Output is correct
2 Correct 28 ms 63068 KB Output is correct
3 Correct 24 ms 63020 KB Output is correct
4 Correct 28 ms 63224 KB Output is correct
5 Correct 29 ms 63120 KB Output is correct
6 Incorrect 26 ms 63084 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 62976 KB Output is correct
2 Correct 24 ms 63100 KB Output is correct
3 Correct 24 ms 63068 KB Output is correct
4 Correct 31 ms 63024 KB Output is correct
5 Correct 26 ms 64432 KB Output is correct
6 Correct 28 ms 64488 KB Output is correct
7 Correct 27 ms 64476 KB Output is correct
8 Correct 26 ms 64544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 63060 KB Output is correct
2 Correct 24 ms 63124 KB Output is correct
3 Correct 24 ms 63052 KB Output is correct
4 Correct 29 ms 63052 KB Output is correct
5 Correct 27 ms 63116 KB Output is correct
6 Incorrect 28 ms 63060 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 63068 KB Output is correct
2 Correct 24 ms 63120 KB Output is correct
3 Correct 24 ms 63104 KB Output is correct
4 Correct 25 ms 63060 KB Output is correct
5 Correct 26 ms 63052 KB Output is correct
6 Incorrect 24 ms 63060 KB Output isn't correct
7 Halted 0 ms 0 KB -