QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#826342 | #9224. Express Eviction | DylanSmith | WA | 31ms | 3680kb | C++14 | 3.6kb | 2024-12-22 07:57:32 | 2024-12-22 07:57:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
#define lb(x,y) lower_bound(all(x),y)-begin(x)
mt19937 rng;
void solve() {
int N, M; cin >> N >> M;
vector<string> mat(N);
for (int i = 0; i < N; i++) cin >> mat[i];
vector<vector<int>> dist(N + 1, vector<int>(M + 1, INT_MAX));
dist[0][0] = mat[0][0] == '#';
priority_queue<pair<int, pair<int, int>>> pq;
pq.push({0, {0, 0}});
vector<vector<bool>> vis(N + 1, vector<bool>(M + 1, 0));
vector<int> x = {0, 1, 0, -1}, y = {1, 0, -1, 0};
while (!pq.empty()) {
int r = pq.top().second.first, c = pq.top().second.second; pq.pop();
if (vis[r][c]) continue;
vis[r][c] = 1;
set<pair<int, int>> s1;
for (int i = -1; i <= 0; i++) {
for (int j = -1; j <= 0; j++) {
if (r + i >= 0 && r + i < N && c + j >= 0 && c + j < M) s1.insert({r + i, c + j});
}
}
for (int k = 0; k < 4; k++) {
int r2 = r + x[k], c2 = c + y[k];
if (r2 >= 0 && r2 <= N && c2 >= 0 && c2 <= M) {
set<pair<int, int>> s2;
for (int i = -1; i <= 0; i++) {
for (int j = -1; j <= 0; j++) {
if (r2 + i >= 0 && r2 + i < N && c2 + j >= 0 && c2 + j < M) s2.insert({r2 + i, c2 + j});
}
}
int cnt = 0;
for (auto &p : s2) {
if (!s1.count(p) && mat[p.first][p.second] == '#') cnt++;
}
if (dist[r][c] + cnt < dist[r2][c2]) {
dist[r2][c2] = dist[r][c] + cnt;
pq.push({-dist[r2][c2], {r2, c2}});
}
}
}
for (int dx = -1; dx <= 1; dx += 2) {
for (int dy = -1; dy <= 1; dy += 2) {
int r2 = r + dx, c2 = c + dy;
if (r2 >= 0 && r2 <= N && c2 >= 0 && c2 <= M) {
set<pair<int, int>> skip;
for (int i = -1; i <= 0; i++) {
for (int j = -1; j <= 0; j++) {
if (r + i >= 0 && r + i < N && c + j >= 0 && c + j < M) {
skip.insert({r + i, c + j});
}
if (r2 + i >= 0 && r2 + i < N && c2 + j >= 0 && c2 + j < M) {
skip.insert({r2 + i, c2 + j});
}
}
}
vector<vector<bool>> vis2(N + 1, vector<bool>(M + 1, 0));
vis2[r][c] = 1;
queue<pair<int, int>> q;
q.push({r, c});
while (!q.empty()) {
int r3 = q.front().first, c3 = q.front().second; q.pop();
for (int k = 0; k < 4; k++) {
int r4 = r3 + x[k], c4 = c3 + y[k];
if (r4 >= 0 && r4 <= N && c4 >= 0 && c4 <= M) {
bool bad = 0;
for (int i = -1; i <= 0; i++) {
for (int j = -1; j <= 0; j++) {
if (r4 + i >= 0 && r4 + i < N && c4 + j >= 0 && c4 + j < M && mat[r4 + i][c4 + j] == '#' && !skip.count({r4 + i, c4 + j})) {
bad = 1;
}
}
}
if (!bad && !vis2[r4][c4]) {
vis2[r4][c4] = 1;
q.push({r4, c4});
}
}
}
}
if (vis2[r2][c2]) {
set<pair<int, int>> s2;
for (int i = -1; i <= 0; i++) {
for (int j = -1; j <= 0; j++) {
if (r2 + i >= 0 && r2 + i < N && c2 + j >= 0 && c2 + j < M) s2.insert({r2 + i, c2 + j});
}
}
int cnt = 0;
for (auto &p : s2) {
if (!s1.count(p) && mat[p.first][p.second] == '#') cnt++;
}
if (dist[r][c] + cnt < dist[r2][c2]) {
dist[r2][c2] = dist[r][c] + cnt;
pq.push({-dist[r2][c2], {r2, c2}});
}
}
}
}
}
}
cout << dist[N][M] << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
rng = mt19937(chrono::steady_clock::now().time_since_epoch().count());
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3608kb
input:
4 6 .##... .#.... ##.... ....#.
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 7ms
memory: 3680kb
input:
20 30 ...########################### #..########################### ...########################### ...########################### .############################# ...########################### ...########################### #..########################### ...########################### ..#...............
output:
11
result:
ok 1 number(s): "11"
Test #3:
score: -100
Wrong Answer
time: 31ms
memory: 3588kb
input:
35 35 ....###########...#########........ ##..#######################..#####. ....#######################..#####. ...#.....##################..#####. .##......##################.....##. .##..##..#############.....#....##. .....##..#############......##..##. ....#....#############..##..##..##. ####.....
output:
21
result:
wrong answer 1st numbers differ - expected: '16', found: '21'