/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Time Exceeded ≥2100ms ≥532.0 KiB
#3 Time Exceeded ≥2101ms ≥4.852 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
char a[2002][2002];
ll n,m,x,y,ans=1e18;

void find(int i,int j,int dir,ll cost)
{
    if(i>n || i<1 || j>m || j<1) return;
    if(i==n && j==m)
    {
        ans = min(ans,cost);
        return;
    }
    if(a[i][j]=='#') return;
    if(dir==0)
    {
        find(i,j+1,0,cost+x);
        find(i+1,j,2,cost+y);
    }
    else if(dir==1)
    {
        find(i,j-1,1,cost+x);
        find(i+1,j,2,cost+y);
    }
    else
    {
        find(i+1,j,2,cost+x);
        find(i,j+1,0,cost+y);
        find(i,j-1,1,cost+y);
    }
}

void solve()
{
    cin >> n >> m >> x >> y;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++) cin >> a[i][j];
    }
    find(1,2,0,0);
    find(2,1,2,0);
    if(ans >= 1e18) cout<<-1<<endl;
    else cout<<ans<<endl;
    ans = 1e18;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int t; cin >> t; while(t--)  solve(),ans =1e18;
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-31 20:32:57
Judged At
2024-12-31 20:32:57
Judged By
Score
1
Total Time
≥2101ms
Peak Memory
≥4.852 MiB