Submission #1343828

#TimeUsernameProblemLanguageResultExecution timeMemory
1343828hoangtien69Robots (APIO13_robots)C++20
10 / 100
1 ms580 KiB
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define tiii tuple<int,int,int>
#define int long long 
const int MAXN = 505;
const int INF = 1e18 + 5;

int n, h, w;
char a[MAXN][MAXN];
int dp[MAXN][MAXN][10][10];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
bool vis[MAXN][MAXN][4];
pii his[MAXN][MAXN][4];

pii findd(int i, int j, int d)
{
    if (his[i][j][d].first != 0)
    {
        return his[i][j][d];
    }
    if (vis[i][j][d])
    {
        return {-INF, -INF};
    }
    vis[i][j][d] = true;
    int nd = d;
    if (a[i][j] == 'c')
    {
       nd = (d + 1) % 4;
    }
    else if (a[i][j] == 'C')
    {
        nd = (d + 3) % 4;
    }
    int ni = i + dx[nd];
    int nj = j + dy[nd];
    pii res;
    if (ni >= 1 and ni <= h and nj >= 1 and nj <= w and a[ni][nj] != 'x')
    {
        res = findd(ni, nj, nd);
    }
    else
    {
        res = {i, j};
    }
    vis[i][j][d] = false;
    return his[i][j][d] = res;
}
void dijsktra(int l, int r)
{
    priority_queue<tiii, vector<tiii>, greater<tiii>> pq;
    for (int i = 1; i <= h; i++)
    {
        for (int j = 1; j <= w; j++)
        {
            if (dp[i][j][l][r] < INF)
            {
                pq.push({dp[i][j][l][r], i, j});
            }
        }
    }
    while(!pq.empty())
    {
        auto[d, i, j] = pq.top();
        pq.pop();
        if (d > dp[i][j][l][r])
        {
            continue;
        }
        for (int direct = 0; direct < 4; direct++)
        {
            auto [ni, nj] = findd(i, j, direct);
            if (ni != -INF and dp[ni][nj][l][r] > dp[i][j][l][r] + 1)
            {
                dp[ni][nj][l][r] = dp[i][j][l][r] + 1;
                pq.push({dp[ni][nj][l][r], ni, nj});
            }
        }
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> w >> h;
    for (int i = 1; i <= h; i++)
    {
        for (int j = 1; j <= w; j++)
        {
            for (int l = 1; l <= n; l++)
            {
                for (int r = 1; r <= n; r++)
                {
                    dp[i][j][l][r] = INF;
                }
            }
        }
    }
    for (int i = 1; i <= h; i++)
    {
        string s;
        cin >> s;
        for (int j = 0; j < w; j++)
        {
            a[i][j + 1] = s[j];
            if (a[i][j + 1] >= '0' and a[i][j + 1] <= '9')
            {
                int cur = a[i][j + 1] - '0';
                dp[i][j + 1][cur][cur] = 0;
            }
        }
    }
    for (int len = 1; len <= n; len++)
    {
       for (int l = 1; l + len - 1 <= n; l++)
       {
           int r = l + len - 1;
           for (int i = 1; i <= h; i++)
           {
               for (int j = 1; j <= w; j++)
               {
                   for (int k = l; k < r; k++)
                   {
                       dp[i][j][l][r] = min(dp[i][j][l][r], dp[i][j][l][k] + dp[i][j][k + 1][r]);
                   }
               }
           }
           dijsktra(l, r);
       }
    }
    int ans = INF;
    for (int i = 1; i <= h; i++)
    {
        for (int j = 1; j <= w; j++)
        {
            ans = min(ans, dp[i][j][1][n]);
        }
    }
    if (ans == INF)
    {
        cout << -1;
    }
    else
    {
        cout << ans;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...