QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#617344#9224. Express Evictionucup-team5062#WA 13ms3728kbC++203.2kb2024-10-06 15:02:422024-10-06 15:02:44

Judging History

你现在查看的是最新测评结果

  • [2024-10-06 15:02:44]
  • 评测
  • 测评结果:WA
  • 用时:13ms
  • 内存:3728kb
  • [2024-10-06 15:02:42]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define REP(i, l, r) for (int i = l; i < r; ++i)

constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};

template <class T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;

constexpr int inf = (1 << 30) - 1;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W;
  cin >> H >> W;
  H += 2;
  W += 2;
  vector grid(H, vector(W, 0));
  REP(i, 1, H - 1) REP(j, 1, W - 1) {
    char c;
    cin >> c;
    grid[i][j] = (c == '#');
  }

  const auto reachability = [&](const vector<vector<int>>& data, int sx, int sy) {
    vector done(H - 1, vector(W - 1, false));
    queue<pair<int, int>> que;
    const auto push = [&](int i, int j) {
      if (done[i][j]) return;
      if (data[i][j] + data[i + 1][j] + data[i][j + 1] + data[i + 1][j + 1]) return;
      done[i][j] = true;
      que.emplace(i, j);
    };
    push(sx, sy);
    while (!que.empty()) {
      const auto [i, j] = que.front();
      que.pop();
      for (int k = 0; k < 4; ++k) {
        const int ni = i + dx[k];
        const int nj = j + dy[k];
        if (ni < 0 || nj < 0 || ni >= H - 1 || nj >= W - 1) continue;
        push(ni, nj);
      }
    }
    return done;
  };

  vector edges(H - 1, vector(W - 1, vector<array<int, 3>>{}));
  REP(i, 0, H - 2) REP(j, 0, W - 2) {
    {
      auto copy = grid;
      REP(x, 0, 3) REP(y, 0, 3) {
        if ((x == 0 && y == 0) || (x == 2 && y == 2)) continue;
        copy[i + x][j + y] = 0;
      }
      auto reach = reachability(copy, i + 1, j);
      if (reach[i][j + 1]) {
        edges[i + 1][j].push_back({i, j + 1, grid[i][j + 1] + grid[i][j + 2] + grid[i + 1][j + 2]});
        edges[i][j + 1].push_back({i + 1, j, grid[i + 1][j] + grid[i + 2][j] + grid[i + 2][j + 1]});
      }
    }
    {
      auto copy = grid;
      REP(x, 0, 3) REP(y, 0, 3) {
        if ((x == 0 && y == 2) || (x == 2 && y == 0)) continue;
        copy[i + x][j + y] = 0;
      }
      auto reach = reachability(copy, i, j);
      if (reach[i + 1][j + 1]) {
        edges[i][j].push_back({i + 1, j + 1, grid[i + 1][j + 2] + grid[i + 2][j + 1] + grid[i + 2][j + 2]});
        edges[i + 1][j + 1].push_back({i, j, grid[i][j] + grid[i][j + 1] + grid[i + 1][j]});
      }
    }
  }

  vector dist(H - 1, vector(W - 1, inf));
  min_heap<array<int, 3>> heap;
  const auto push = [&](int d, int i, int j) {
    if (dist[i][j] > d) {
      dist[i][j] = d;
      heap.push({d, i, j});
    }
  };
  push(grid[0][0] + grid[0][1] + grid[1][0] + grid[1][1], 0, 0);
  while (!heap.empty()) {
    const auto [d, i, j] = heap.top();
    heap.pop();
    for (const auto& [ni, nj, c] : edges[i][j]) {
      push(d + c, ni, nj);
    }
    if (i > 0) {
      push(d + grid[i - 1][j] + grid[i  - 1][j + 1], i - 1, j);
    }
    if (i + 1 < H - 1) {
      push(d + grid[i + 2][j] + grid[i + 2][j + 1], i + 1, j);
    }
    if (j > 0) {
      push(d + grid[i][j - 1] + grid[i + 1][j - 1], i, j - 1);
    }
    if (j + 1 < W - 1) {
      push(d + grid[i][j + 2] + grid[i + 1][j + 2], i, j + 1);
    }
  }

  cout << dist[H - 2][W - 2] << '\n';

  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3540kb

input:

4 6
.##...
.#....
##....
....#.

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

20 30
...###########################
#..###########################
...###########################
...###########################
.#############################
...###########################
...###########################
#..###########################
...###########################
..#...............

output:

11

result:

ok 1 number(s): "11"

Test #3:

score: -100
Wrong Answer
time: 13ms
memory: 3728kb

input:

35 35
....###########...#########........
##..#######################..#####.
....#######################..#####.
...#.....##################..#####.
.##......##################.....##.
.##..##..#############.....#....##.
.....##..#############......##..##.
....#....#############..##..##..##.
####.....

output:

21

result:

wrong answer 1st numbers differ - expected: '16', found: '21'