/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 58ms 816.0 KiB
#3 Accepted 360ms 142.773 MiB
#4 Accepted 361ms 142.77 MiB
#5 Accepted 357ms 142.555 MiB
#6 Accepted 59ms 788.0 KiB
#7 Accepted 35ms 780.0 KiB
#8 Accepted 36ms 784.0 KiB
#9 Accepted 367ms 136.77 MiB
#10 Accepted 342ms 135.863 MiB
#11 Accepted 368ms 136.27 MiB
#12 Accepted 335ms 134.16 MiB
#13 Accepted 342ms 134.773 MiB
#14 Accepted 53ms 4.301 MiB
#15 Accepted 340ms 135.625 MiB
#16 Accepted 368ms 136.992 MiB
#17 Accepted 372ms 136.812 MiB
#18 Accepted 337ms 135.605 MiB
#19 Accepted 336ms 134.77 MiB
#20 Accepted 318ms 122.875 MiB
#21 Accepted 298ms 124.438 MiB
#22 Accepted 333ms 125.762 MiB
#23 Accepted 53ms 4.27 MiB
#24 Accepted 52ms 4.363 MiB
#25 Accepted 301ms 125.062 MiB
#26 Accepted 297ms 124.289 MiB
#27 Accepted 306ms 126.215 MiB
#28 Accepted 298ms 123.617 MiB
#29 Accepted 308ms 125.352 MiB
#30 Accepted 295ms 122.41 MiB
#31 Accepted 305ms 125.656 MiB
#32 Accepted 293ms 124.625 MiB
#33 Accepted 307ms 121.422 MiB
#34 Accepted 343ms 122.738 MiB
#35 Accepted 324ms 125.305 MiB
#36 Accepted 297ms 123.312 MiB
#37 Accepted 295ms 121.973 MiB
#38 Accepted 295ms 122.77 MiB
#39 Accepted 52ms 4.363 MiB
#40 Accepted 52ms 4.27 MiB
#41 Accepted 263ms 115.469 MiB
#42 Accepted 52ms 4.27 MiB
#43 Accepted 261ms 113.344 MiB
#44 Accepted 264ms 113.27 MiB
#45 Accepted 338ms 135.18 MiB
#46 Accepted 343ms 137.109 MiB
#47 Accepted 342ms 135.77 MiB
#48 Accepted 339ms 135.387 MiB
#49 Accepted 361ms 136.523 MiB
#50 Accepted 351ms 135.656 MiB
#51 Accepted 334ms 135.59 MiB
#52 Accepted 338ms 136.52 MiB
#53 Accepted 343ms 135.77 MiB

Code

#include "bits/stdc++.h"

#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#define fast_io std::ios::sync_with_stdio(0);std::cin.tie(0)
#define lli long long int
#define flush fflush(stdout)
#define new_line printf("\n")
#define yn(a, b) printf("%s\n", (a) >= (b) ? "Yes":"No")
#define amodm(a, M) (((a)%M+M)%M)
// #define int lli

using pii = std::pair<int,int>;

const int MOD = 1000000007;
const int mxN = 2005;

int n, m, x, y, stat;
char g[mxN][mxN];
lli dp[mxN][mxN][3];
int vis[mxN][mxN][3];

lli inf = 1e13;

bool OK(int r, int c) {
    if (std::min(r,c) < 0 || (r == n) || (c == m)) return 0;
    if (g[r][c] == '#') return 0;
    return 1;
}

lli fn(int r, int c, int mv) {
    if ((r == n-1) && (c == m-1)) return 0;
    if ((r >= n) || (c >= m)) return inf;
    if (r < 0 || c < 0) return inf;
    if (g[r][c] == '#') return inf;
    if (vis[r][c][mv] == stat) return dp[r][c][mv];
    
    lli ans = inf;
    if ((r == 0) && (c == 0)) {
        if (OK(r+1, c)) {
            ans = std::min(ans, fn(r+2, c, 0) + x);
            ans = std::min(ans, fn(r+1, c+1, 2) + y);
        }
        if (OK(r, c+1)) {
            ans = std::min(ans, fn(r, c+2, 2) + x);
            ans = std::min(ans, fn(r+1, c+1, 0) + y);
        }
    } else {
        if (mv == 0) {
            ans = std::min(ans, fn(r+1, c, 0) + x);
            ans = std::min(ans, fn(r, c+1, 2) + y);
            ans = std::min(ans, fn(r, c-1, 1) + y);
        } else if (mv == 1) {
            ans = std::min(ans, fn(r, c-1, 1) + x);
            ans = std::min(ans, fn(r+1, c, 0) + y);
        } else {
            ans = std::min(ans, fn(r, c+1, 2) + x);
            ans = std::min(ans, fn(r+1, c, 0) + y);
        }
    }
    
    vis[r][c][mv] = stat;
    return dp[r][c][mv] = ans;
}

signed main() {
    fast_io;
    int testCases=1;
    // scanf("%d",&testCases);
    std::cin >> testCases;
    
    for (int TC = 1; TC <= testCases; TC++) {
        stat++;
        // scanf("%d%d",&n,&m);
        // scanf("%d%d",&x,&y);
        std::cin >> n >> m >> x >> y;
        
        for (int i = 0; i < n; i++) {
            std::cin >> std::ws;
            for (int j = 0; j < m; j++) std::cin >> g[i][j];
        }
    
        // for (int i = 0; i < n; i++) {
        //     for (int j = 0; j < m; ++j) std::cout << g[i][j];
        //     std::cout << '\n';
        // }
    
        lli ans = fn(0, 0, 0);
        if (ans >= inf) ans = -1;
        
        std::cout << ans << '\n';
        
    }
    
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 17:57:59
Judged At
2025-01-02 17:57:59
Judged By
Score
100
Total Time
372ms
Peak Memory
142.773 MiB