Submission #1268765

#TimeUsernameProblemLanguageResultExecution timeMemory
1268765flashmtMaze (JOI23_ho_t3)C++20
43 / 100
2096 ms47300 KiB
#include <bits/stdc++.h>
#ifdef LOCAL
#include "Debug.h"
#else
#define debug(...) 42
#endif
using namespace std;
const int DX[] = {-1, 0, 1, 0};
const int DY[] = {0, 1, 0, -1};
const int oo = 27081993;

struct DisjointSet
{
  int n;
  vector<int> ds;

  DisjointSet(int n): n(n)
  {
    ds = vector<int>(n);
    for (int i = 0; i < n; i++)
      ds[i] = i;
  }

  int get(int x)
  {
    return x == ds[x] ? x : ds[x] = get(ds[x]);
  }

  int join(int x, int y)
  {
    int dx = get(x), dy = get(y);
    if (dx == dy)
      return 0;
    if (dx < dy)
      swap(dx, dy);
    ds[dy] = dx;
    return 1;
  }
};

int r, c;
string a[2525];
DisjointSet *rows[2525];

int isValid(int x, int y)
{
  return 0 <= x && x < r && 0 <= y && y < c;
}

int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, sx, sy, tx, ty;
  cin >> r >> c >> n;
  cin >> sx >> sy >> tx >> ty;
  sx--; sy--; tx--; ty--;
  for (int i = 0; i < r; i++)
    cin >> a[i];

  for (int i = 0; i < r; i++)
    rows[i] = new DisjointSet(c + 1);

  deque<pair<int, int>> q;
  vector<vector<int>> dist(r, vector<int>(c, oo));
  vector<vector<int>> isBorder(r, vector<int>(c));
  vector<vector<pair<int, int>>> prev(r, vector<pair<int, int>>(c));
  dist[sx][sy] = 0;
  isBorder[sx][sy] = 1;
  q.push_back({sx, sy});
  rows[sx]->join(sy, sy + 1);
  while (!empty(q))
  {
    auto [x, y] = q.front();
    q.pop_front();
    if (x == tx && y == ty)
    {
      cout << dist[x][y] << endl;
      return 0;
    }

    for (int i = 0; i < 4; i++)
    {
      int xx = x + DX[i], yy = y + DY[i];
      if (isValid(xx, yy) && a[xx][yy] == '.' && dist[x][y] < dist[xx][yy])
      {
        dist[xx][yy] = dist[x][y];
        isBorder[xx][yy] = 1;
        q.push_front({xx, yy});
        rows[xx]->join(yy, yy + 1);
      }
    }

    if (!isBorder[x][y] || (r * c > 150000 && dist[x][y] == 10))
      continue;

    for (int xx = max(x - n, 0); xx <= min(x + n, r - 1); xx++)
    {
      int minY = y - n, maxY = y + n;
      if (abs(xx - x) == n)
      {
        minY++;
        maxY--;
      }
      minY = max(0, minY);
      maxY = min(c - 1, maxY);

      while (1)
      {
        int yy = rows[xx]->get(minY);
        if (yy > maxY)
          break;
        dist[xx][yy] = dist[x][y] + 1;
        if (abs(xx - x) == n || abs(yy - y) == n)
          isBorder[xx][yy] = 1;
        q.push_back({xx, yy});
        rows[xx]->join(yy, yy + 1);
      }
    }
  }
  assert(0);
}
#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...