Submission #1196788

#TimeUsernameProblemLanguageResultExecution timeMemory
1196788mmaitiToy (CEOI24_toy)C++20
100 / 100
87 ms31560 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vi vector<int>
#define si set<int>
#define usi unordered_set<int>
#define sll set<ll>
#define usll unordered_set<ll>
#define vb vector<bool>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vvi vector<vector<int>>
#define vvll vector<vector<ll>>

// xh, yh, xv, yv
bool visited[1500][1500];
void solve() {
  int W, H, K, L;
  cin >> W >> H >> K >> L;
  int xh, yh, xv, yv, gx, gy;
  cin >> yh >> xh >> yv >> xv;
  vector<string> grid(H);
  for (int i = 0; i < H; i++)
    cin >> grid[i];

  vector<vi> psum(H + 1, vi(W + 1));
  vector<vi> psumH(H, vi(W)), psumV(H, vi(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (grid[i][j] == '*') {
        gx = i;
        gy = j;
      }
      psum[i + 1][j + 1] =
          (grid[i][j] == 'X') + psum[i][j + 1] + psum[i + 1][j] - psum[i][j];
    }
  }

  vll dx{-1, 0, 1, 0};
  vll dy{0, 1, 0, -1};
  queue<pair<int, int>> q;
  q.push(make_pair(xh, yv));

  // x1, y1 are top left, x2, y2 are bottom right
  // all values are 0 indexed
  auto queryRegion = [&](ll x1, ll y1, ll x2, ll y2) {
    return psum[x2 + 1][y2 + 1] - psum[x1][y2 + 1] - psum[x2 + 1][y1] +
           psum[x1][y1];
  };

  for (int i = 0; i < H - 1; i++) {
    for (int j = 0; j < W - K + 1; j++) {
      if (queryRegion(i, j, i + 1, j + K - 1) == 0) {
        psumH[i][j] += 1;
        if (j + K < W) {
          psumH[i][j + K] -= 1;
        }
      }
    }
  }

  for (int i = 0; i < H - L + 1; i++) {
    for (int j = 0; j < W - 1; j++) {
      if (queryRegion(i, j, i + L - 1, j + 1) == 0) {
        psumV[i][j] += 1;
        if (i + L < H) {
          psumV[i + L][j] -= 1;
        }
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (j > 0)
        psumH[i][j] += psumH[i][j - 1];
      if (i > 0)
        psumV[i][j] += psumV[i - 1][j];
    }
  }

  auto checkCoord = [&](ll x, ll y) {
    return (x >= 0 && x < H && y >= 0 && y < W);
  };
  while (!q.empty()) {
    int cx, cy;
    tie(cx, cy) = q.front();
    q.pop();
    if (cx == gx && cy == gy) {
      cout << "YES\n";
      return;
    }
    for (int i = 0; i < 4; i++) {
      int nxh = cx + dx[i];
      int nyh = cy + dy[i];
      if (!checkCoord(nxh, nyh) || grid[nxh][nyh] == 'X' || visited[nxh][nyh])
        continue;
      if (abs(dy[i]) == 1) {
        if (psumV[cx][min(cy, nyh)] > 0) {
          visited[nxh][nyh] = true;
          q.push({nxh, nyh});
        }
      } else {
        if (psumH[min(cx, nxh)][cy]) {
          visited[nxh][nyh] = true;
          q.push({nxh, nyh});
        }
      }
    }
  }

  cout << "NO\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  solve();
}
#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...