답안 #510042

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
510042 2022-01-14T14:56:07 Z Victor Patkice II (COCI21_patkice2) C++17
110 / 110
194 ms 44472 KB
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)

#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl

#define umap unordered_map
#define uset unordered_set

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;

const int INF = 1'000'000'007;

int rows, cols, dist[2001][2001], parent[2001][2001];
int dr[] = {1, -1, 0, 0, 0};
int dc[] = {0, 0, 1, -1, 0};
string grid[2001];

bool valid(int row, int col) { return 0 <= row && row < rows && 0 <= col && col < cols; }

void path(int row, int col) {
    int p = parent[row][col];
    if (p == -1) return;
    int nr = row - dr[p], nc = col - dc[p];
    grid[nr][nc] = p;
    path(nr, nc);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);

    memset(dist, -1, sizeof(dist));
    memset(parent, -1, sizeof(parent));
    cin >> rows >> cols;
    int sr, sc, er, ec;
    rep(i, 0, rows) {
        cin >> grid[i];
        int j = 0;
        trav(cell, grid[i]) {
            if (cell == 'o') {
                sr = i;
                sc = j;

            } else if (cell == 'x') {
                er = i;
                ec = j;
            }

            cell = cell == 'v' ? 0 : cell == '^' ? 1
                                 : cell == '>'   ? 2
                                 : cell == '<'   ? 3
                                                 : 4;
            ++j;
        }
    }

    deque<ii> q;
    dist[sr][sc] = 0;
    rep(i, 0, 4) if (valid(sr + dr[i], sc + dc[i])) {
        q.emplace_front(sr + dr[i], sc + dc[i]);
        dist[sr + dr[i]][sc + dc[i]] = 0;
    }

    while (!q.empty()) {
        int row, col;
        tie(row, col) = q.front();
        // cout << "row = " << row << " col = " << col << " dist = " << dist[row][col] << endl;
        q.pop_front();

        if (!dist[row][col] == -2) continue;

        if (row == er && col == ec) {
            cout << dist[row][col] << endl;
            path(row, col);
            break;
        }

        int cell = grid[row][col];
        int nr = row + dr[cell], nc = col + dc[cell];

        if (valid(nr, nc)) {
            if (dist[nr][nc] == -1 || dist[row][col] < dist[nr][nc]) {
                dist[nr][nc] = dist[row][col];
                q.emplace_front(nr, nc);
                parent[nr][nc] = cell;
            }
        }

        rep(i, 0, 4) {
            nr = row + dr[i], nc = col + dc[i];
            if (!valid(nr, nc) || dist[nr][nc] != -1) continue;
            dist[nr][nc] = dist[row][col] + 1;
            q.emplace_back(nr, nc);
            parent[nr][nc] = i;
        }
        dist[row][col] = -2;
    }

    rep(i, 0, rows) {
        trav(cell, grid[i]) cell = cell == 0 ? 'v' : cell == 1 ? '^'
                                                 : cell == 2   ? '>'
                                                 : cell == 3   ? '<'
                                                               : '.';
    }
    grid[sr][sc]='o';
    grid[er][ec]='x';
    rep(i, 0, rows) cout << grid[i] << endl;
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:90:29: warning: logical not is only applied to the left hand side of comparison [-Wlogical-not-parentheses]
   90 |         if (!dist[row][col] == -2) continue;
      |                             ^~
Main.cpp:90:13: note: add parentheses around left hand side expression to silence this warning
   90 |         if (!dist[row][col] == -2) continue;
      |             ^~~~~~~~~~~~~~~
      |             (              )
Main.cpp:90:29: warning: comparison of constant '-2' with boolean expression is always false [-Wbool-compare]
   90 |         if (!dist[row][col] == -2) continue;
      |             ~~~~~~~~~~~~~~~~^~~~~
Main.cpp:126:16: warning: 'ec' may be used uninitialized in this function [-Wmaybe-uninitialized]
  126 |     grid[er][ec]='x';
      |                ^
Main.cpp:79:27: warning: 'sc' may be used uninitialized in this function [-Wmaybe-uninitialized]
   79 |     rep(i, 0, 4) if (valid(sr + dr[i], sc + dc[i])) {
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:79:27: warning: 'sr' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 31692 KB Output is correct
2 Correct 14 ms 31608 KB Output is correct
3 Correct 12 ms 31704 KB Output is correct
4 Correct 12 ms 31692 KB Output is correct
5 Correct 13 ms 31668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 31692 KB Output is correct
2 Correct 14 ms 31608 KB Output is correct
3 Correct 12 ms 31704 KB Output is correct
4 Correct 12 ms 31692 KB Output is correct
5 Correct 13 ms 31668 KB Output is correct
6 Correct 29 ms 32716 KB Output is correct
7 Correct 31 ms 33604 KB Output is correct
8 Correct 79 ms 34628 KB Output is correct
9 Correct 98 ms 38792 KB Output is correct
10 Correct 169 ms 38156 KB Output is correct
11 Correct 194 ms 44472 KB Output is correct