Submission #832996

#TimeUsernameProblemLanguageResultExecution timeMemory
832996WLZMaze (JOI23_ho_t3)C++17
94 / 100
2051 ms134848 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "templates/debug.h"
#else
#define debug(...) 0
#endif

const vector<int> dx = {1, -1, 0, 0};
const vector<int> dy = {0, 0, 1, -1};
const vector<int> dxx = {1, 1, 1, -1, -1, -1, 0, 0};
const vector<int> dyy = {0, 1, -1, 0, 1, -1, -1, 1};

const int INF = 0x3f3f3f3f;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int r, c, n; cin >> r >> c >> n;
  int sr, sc, gr, gc; cin >> sr >> sc >> gr >> gc;
  sr--; sc--; gr--; gc--;
  vector<string> grid(r);
  for (int i = 0; i < r; i++) cin >> grid[i];
  vector< vector<int> > dist(r, vector<int>(c, INF)); dist[sr][sc] = 0;
  priority_queue< tuple<int, int, int>, vector< tuple<int, int, int> >, greater<> > pq; pq.push({0, sr, sc});
  while (!pq.empty()) {
    int d, x, y;
    tie(d, x, y) = pq.top(); pq.pop();
    if (d > dist[x][y]) continue;
    if (d % (n + 1) < n && d % (n + 1) > 0) {
      for (int k = 0; k < 8; k++) {
        int nx = x + dxx[k], ny = y + dyy[k];
        if (nx < 0 || nx >= r || ny < 0 || ny >= c) continue;
        if (d + 1 < dist[nx][ny]) {
          dist[nx][ny] = d + 1;
          pq.push({dist[nx][ny], nx, ny});
        }
      }
    }
    if (d % (n + 1) == 0) {
      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k], ny = y + dy[k];
        if (nx < 0 || nx >= r || ny < 0 || ny >= c) continue;
        if (d + 1 < dist[nx][ny]) {
          dist[nx][ny] = d + 1;
          pq.push({dist[nx][ny], nx, ny});
        }
      }
    }
    if (d % (n + 1) == n) {
      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k], ny = y + dy[k];
        if (nx < 0 || nx >= r || ny < 0 || ny >= c) continue;
        if (d + 2 < dist[nx][ny]) {
          dist[nx][ny] = d + 2;
          pq.push({dist[nx][ny], nx, ny});
        }
      }
    }
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (nx < 0 || nx >= r || ny < 0 || ny >= c || grid[nx][ny] == '#') continue;
      if ((d + n) / (n + 1) * (n + 1) < dist[nx][ny]) {
        dist[nx][ny] = (d + n) / (n + 1) * (n + 1);
        pq.push({dist[nx][ny], nx, ny});
      }
    }
  }
  debug(dist);
  cout << (dist[gr][gc] + n) / (n + 1) << '\n';
  return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:7:20: warning: statement has no effect [-Wunused-value]
    7 | #define debug(...) 0
      |                    ^
Main.cpp:70:3: note: in expansion of macro 'debug'
   70 |   debug(dist);
      |   ^~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...