Submission #49659

# Submission time Handle Problem Language Result Execution time Memory
49659 2018-06-01T18:14:22 Z model_code None (JOI16_skating) C++17
100 / 100
410 ms 55892 KB
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

const int INF = 1001001001;

int in() { int x; scanf("%d", &x); return x; }

const int MAX = 2048;

char F[MAX][MAX];
int U[MAX][MAX], R[MAX][MAX], D[MAX][MAX], L[MAX][MAX], A[MAX][MAX];

int main() {
  const int H = in();
  const int W = in();
  for (int i = 0; i < H; ++i) {
    scanf("%s", F[i]);
    for (int j = 0; j < W; ++j) {
      A[i][j] = INF;
    }
  }

  const int sr = in() - 1;
  const int sc = in() - 1;
  const int gr = in() - 1;
  const int gc = in() - 1;

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (F[i][j] == '#') {
        U[i][j] = i;
        L[i][j] = j;
      } else {
        U[i][j] = U[i - 1][j];
        L[i][j] = L[i][j - 1];
      }
    }
  }

  for (int i = H - 1; i >= 0; --i) {
    for (int j = W - 1; j >= 0; --j) {
      if (F[i][j] == '#') {
        D[i][j] = i;
        R[i][j] = j;
      } else {
        D[i][j] = D[i + 1][j];
        R[i][j] = R[i][j + 1];
      }
    }
  }

  struct state {
    int r, c, s;
    state(int r, int c, int s) : r(r), c(c), s(s) { }
    bool operator < (const state& o) const {
      return s > o.s;
    }
  };
  priority_queue<state> Q;

  Q.emplace(sr, sc, 0);
  while (!Q.empty()) {
    const state s = Q.top(); Q.pop();
    if (s.r == gr && s.c == gc) {
      printf("%d\n", s.s);
      return 0;
    }

    const auto update = [&] (const int rr, const int cc, const int ss) {
      if ((s.r == rr && s.c == cc) || F[rr][cc] == '#') return;
      if (ss < A[rr][cc]) {
        A[rr][cc] = ss;
        Q.emplace(rr, cc, ss);
      }
    };

    update(U[s.r][s.c] + 1, s.c, s.s + 1);
    update(D[s.r][s.c] - 1, s.c, s.s + 1);
    update(s.r, L[s.r][s.c] + 1, s.s + 1);
    update(s.r, R[s.r][s.c] - 1, s.s + 1);

    const static int dr[] = {1, 0, -1, 0}, dc[] = {0, 1, 0, -1};
    for (int d = 0; d < 4; ++d) {
      update(s.r + dr[d], s.c + dc[d], s.s + 2);
    }
  }

  puts("-1");
  return 0;
}

Compilation message

skating.cpp: In function 'int in()':
skating.cpp:9:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 int in() { int x; scanf("%d", &x); return x; }
                   ~~~~~^~~~~~~~~~
skating.cpp: In function 'int main()':
skating.cpp:20:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", F[i]);
     ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 616 KB Output is correct
3 Correct 2 ms 684 KB Output is correct
4 Correct 2 ms 860 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 896 KB Output is correct
7 Correct 3 ms 896 KB Output is correct
8 Correct 2 ms 896 KB Output is correct
9 Correct 2 ms 896 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 616 KB Output is correct
3 Correct 2 ms 684 KB Output is correct
4 Correct 2 ms 860 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 896 KB Output is correct
7 Correct 3 ms 896 KB Output is correct
8 Correct 2 ms 896 KB Output is correct
9 Correct 2 ms 896 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
11 Correct 9 ms 5116 KB Output is correct
12 Correct 14 ms 5140 KB Output is correct
13 Correct 9 ms 5292 KB Output is correct
14 Correct 10 ms 5292 KB Output is correct
15 Correct 6 ms 5292 KB Output is correct
16 Correct 13 ms 5360 KB Output is correct
17 Correct 10 ms 5360 KB Output is correct
18 Correct 10 ms 5360 KB Output is correct
19 Correct 11 ms 5360 KB Output is correct
20 Correct 8 ms 5360 KB Output is correct
21 Correct 8 ms 5360 KB Output is correct
22 Correct 7 ms 5360 KB Output is correct
23 Correct 11 ms 5360 KB Output is correct
24 Correct 16 ms 5360 KB Output is correct
25 Correct 11 ms 5360 KB Output is correct
26 Correct 10 ms 5360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 616 KB Output is correct
3 Correct 2 ms 684 KB Output is correct
4 Correct 2 ms 860 KB Output is correct
5 Correct 2 ms 896 KB Output is correct
6 Correct 2 ms 896 KB Output is correct
7 Correct 3 ms 896 KB Output is correct
8 Correct 2 ms 896 KB Output is correct
9 Correct 2 ms 896 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
11 Correct 9 ms 5116 KB Output is correct
12 Correct 14 ms 5140 KB Output is correct
13 Correct 9 ms 5292 KB Output is correct
14 Correct 10 ms 5292 KB Output is correct
15 Correct 6 ms 5292 KB Output is correct
16 Correct 13 ms 5360 KB Output is correct
17 Correct 10 ms 5360 KB Output is correct
18 Correct 10 ms 5360 KB Output is correct
19 Correct 11 ms 5360 KB Output is correct
20 Correct 8 ms 5360 KB Output is correct
21 Correct 8 ms 5360 KB Output is correct
22 Correct 7 ms 5360 KB Output is correct
23 Correct 11 ms 5360 KB Output is correct
24 Correct 16 ms 5360 KB Output is correct
25 Correct 11 ms 5360 KB Output is correct
26 Correct 10 ms 5360 KB Output is correct
27 Correct 201 ms 44332 KB Output is correct
28 Correct 360 ms 46028 KB Output is correct
29 Correct 46 ms 46564 KB Output is correct
30 Correct 124 ms 47604 KB Output is correct
31 Correct 47 ms 47880 KB Output is correct
32 Correct 276 ms 48976 KB Output is correct
33 Correct 42 ms 48976 KB Output is correct
34 Correct 98 ms 50236 KB Output is correct
35 Correct 210 ms 51376 KB Output is correct
36 Correct 173 ms 52264 KB Output is correct
37 Correct 76 ms 53060 KB Output is correct
38 Correct 410 ms 54860 KB Output is correct
39 Correct 99 ms 54928 KB Output is correct
40 Correct 52 ms 55892 KB Output is correct