Submission #833014

#TimeUsernameProblemLanguageResultExecution timeMemory
833014WLZMaze (JOI23_ho_t3)C++17
100 / 100
946 ms99492 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, 1, 1, -1, -1};
const vector<int> dy = {0, 0, 1, -1, -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});
  deque< pair<int, int> > q, q2; q.push_front({sr, sc});
  while (!q.empty() || !q2.empty()) {
    int d, x, y;
    if (q.empty()) swap(q, q2);
    if (q.empty()) tie(x, y) = q2.front(), q2.pop_front();
    else {
      tie(x, y) = q.front(); q.pop_front();
    }
    d = dist[x][y];
    for (int k = 0; k < 8; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (nx < 0 || nx >= r || ny < 0 || ny >= c) continue;
      if (0 < d % (n + 1) && d % (n + 1) < n && d + 1 < dist[nx][ny]) {
        dist[nx][ny] = d + 1;
        q.push_back({nx, ny});
      }
      if (k >= 4) continue;
      if (d % (n + 1) == 0 && d + 1 < dist[nx][ny]) {
        dist[nx][ny] = d + 1;
        q.push_back({nx, ny});
      }
      if (d % (n + 1) == n && d + 2 < dist[nx][ny]) {
        dist[nx][ny] = d + 2;
        q2.push_front({nx, ny});
      }
      if (grid[nx][ny] == '.' && (d + n) / (n + 1) * (n + 1) < dist[nx][ny]) {
        dist[nx][ny] = (d + n) / (n + 1) * (n + 1);
        if (d % (n + 1) == 0) q.push_front({nx, ny});
        else q2.push_back({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:58:3: note: in expansion of macro 'debug'
   58 |   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...