QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#533076#9224. Express Evictionucup-team004#TL 1788ms29568kbC++204.3kb2024-08-25 16:52:362024-08-25 16:52:38

Judging History

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

  • [2024-08-25 16:52:38]
  • 评测
  • 测评结果:TL
  • 用时:1788ms
  • 内存:29568kb
  • [2024-08-25 16:52:36]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;

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

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m;
    std::cin >> n >> m;
    
    std::vector<std::string> s(n);
    for (int i = 0; i < n; i++) {
        std::cin >> s[i];
    }
    
    std::vector S(n, std::vector<int>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            S[i][j] = (s[i][j] == '#');
        }
    }
    
    std::vector dp(n + 1, std::vector(m + 1, std::vector(n + 1, std::vector(m + 1, -1))));
    
    std::vector cost(n + 1, std::vector(m + 1, std::array<int, 4>{}));
    
    for (int x = 0; x <= n; x++) {
        for (int y = 0; y <= m; y++) {
            for (int dir = 0; dir < 4; dir++) {
                int nx = x + dx[dir];
                int ny = y + dy[dir];
                if (nx < 0 || nx > n || ny < 0 || ny > m) {
                    continue;
                }
                int lx = -1, rx = 0, ly = -1, ry = 0;
                if (dir == 0) {
                    lx++;
                } else if (dir == 1) {
                    ly++;
                } else if (dir == 2) {
                    rx--;
                } else {
                    ry--;
                }
                for (int dx = lx; dx <= rx; dx++) {
                    for (int dy = ly; dy <= ry; dy++) {
                        int ax = nx + dx;
                        int ay = ny + dy;
                        if (0 <= ax && ax < n && 0 <= ay && ay < m) {
                            cost[x][y][dir] += S[ax][ay];
                        }
                    }
                }
            }
        }
    }
    
    std::vector dis(n + 1, std::vector(m + 1, std::vector(n + 1, std::vector(m + 1, -1))));
    for (int sx = 0; sx <= n; sx++) {
        for (int sy = 0; sy <= m; sy++) {
            std::priority_queue<std::array<int, 3>, std::vector<std::array<int, 3>>, std::greater<>> pq;
            pq.push({0, sx, sy});
            while (!pq.empty()) {
                auto [d, x, y] = pq.top();
                pq.pop();
                
                if (dis[sx][sy][x][y] != -1) {
                    continue;
                }
                dis[sx][sy][x][y] = d;
                
                for (int dir = 0; dir < 4; dir++) {
                    int nx = x + dx[dir];
                    int ny = y + dy[dir];
                    if (nx < 0 || nx > n || ny < 0 || ny > m) {
                        continue;
                    }
                    pq.push({d + cost[x][y][dir], nx, ny});
                }
            }
        }
    }
    
    std::priority_queue<std::array<int, 5>, std::vector<std::array<int, 5>>, std::greater<>> pq;
    pq.push({s[0][0] == '.' ? 0 : 1, 0, 0, 0, 0});
    
    int ans = 1E9;
    while (!pq.empty()) {
        auto [d, x, y, a, b] = pq.top();
        pq.pop();
        
        if (dp[x][y][a][b] != -1) {
            continue;
        }
        
        dp[x][y][a][b] = d;
        
        if (x == n && y == m) {
            ans = std::min(ans, d);
        }
        
        for (int dir = 0; dir < 4; dir++) {
            int nx = x + dx[dir];
            int ny = y + dy[dir];
            if (nx < 0 || nx > n || ny < 0 || ny > m) {
                continue;
            }
            pq.push({d + cost[x][y][dir], nx, ny, a, b});
            pq.push({d + cost[x][y][dir], nx, ny, nx, ny});
        }
        
        for (int dx = -1; dx <= 1; dx += 2) {
            for (int dy = -1; dy <= 1; dy += 2) {
                int nx = a + dx;
                int ny = b + dy;
                if (nx < 0 || nx > n || ny < 0 || ny > m) {
                    continue;
                }
                int ax = std::min(a, nx);
                int ay = std::min(b, ny);
                int c = dis[x][y][nx][ny];
                if (ax < x - 1 || ax > x || ay < y - 1 || ay > y) {
                    c -= S[ax][ay];
                }
                assert(c >= 0);
                pq.push({d + c, nx, ny, x, y});
            }
        }
    }
    
    std::cout << ans << "\n";
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3780kb

input:

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

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 1788ms
memory: 29568kb

input:

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

output:

11

result:

ok 1 number(s): "11"

Test #3:

score: -100
Time Limit Exceeded

input:

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

output:


result: