/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 380.0 KiB
#2 Accepted 59ms 808.0 KiB
#3 Accepted 355ms 142.578 MiB
#4 Accepted 359ms 142.773 MiB
#5 Accepted 375ms 142.621 MiB
#6 Accepted 57ms 792.0 KiB
#7 Accepted 37ms 788.0 KiB
#8 Accepted 39ms 788.0 KiB
#9 Accepted 371ms 136.727 MiB
#10 Accepted 344ms 135.758 MiB
#11 Accepted 365ms 136.039 MiB
#12 Accepted 362ms 134.141 MiB
#13 Accepted 364ms 134.859 MiB
#14 Accepted 53ms 4.363 MiB
#15 Accepted 342ms 135.66 MiB
#16 Accepted 377ms 136.867 MiB
#17 Accepted 341ms 137.008 MiB
#18 Accepted 354ms 135.715 MiB
#19 Accepted 366ms 134.82 MiB
#20 Accepted 329ms 122.809 MiB
#21 Accepted 299ms 124.359 MiB
#22 Accepted 336ms 125.773 MiB
#23 Accepted 54ms 4.238 MiB
#24 Accepted 69ms 4.359 MiB
#25 Accepted 306ms 125.09 MiB
#26 Accepted 322ms 124.219 MiB
#27 Accepted 301ms 126.211 MiB
#28 Accepted 301ms 123.586 MiB
#29 Accepted 303ms 125.203 MiB
#30 Accepted 312ms 122.426 MiB
#31 Accepted 301ms 125.758 MiB
#32 Accepted 301ms 124.633 MiB
#33 Accepted 322ms 121.418 MiB
#34 Accepted 295ms 122.539 MiB
#35 Accepted 305ms 125.309 MiB
#36 Accepted 303ms 123.445 MiB
#37 Accepted 296ms 122.066 MiB
#38 Accepted 297ms 122.84 MiB
#39 Accepted 81ms 4.18 MiB
#40 Accepted 54ms 4.316 MiB
#41 Accepted 272ms 115.367 MiB
#42 Accepted 54ms 4.34 MiB
#43 Accepted 278ms 113.371 MiB
#44 Accepted 260ms 113.148 MiB
#45 Accepted 369ms 135.148 MiB
#46 Accepted 346ms 137.184 MiB
#47 Accepted 349ms 135.59 MiB
#48 Accepted 343ms 135.371 MiB
#49 Accepted 341ms 136.359 MiB
#50 Accepted 348ms 135.695 MiB
#51 Accepted 355ms 135.785 MiB
#52 Accepted 366ms 136.633 MiB
#53 Accepted 364ms 135.734 MiB

Code

// I AM A MUSLIM

#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
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 16:53:37
Judged At
2025-01-02 16:53:37
Judged By
Score
100
Total Time
377ms
Peak Memory
142.773 MiB