本地测试样例全对,提交RE,玄关
查看原帖
本地测试样例全对,提交RE,玄关
1129046
lkrkerry楼主2024/6/18 21:11
#include <bits/stdc++.h>

using namespace std;

string gr[505];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int ans1[505 * 505];
bool vis[505 * 505];

int n;

struct node
{
    int len, x;
    bool operator<(const node &rhs) const { return len > rhs.len; }
};

vector<pair<int, int>> g[505 * 505];
vector<pair<int, int>> g2[505 * 505];
inline int pos(int x, int y) { return (x - 1) * n + y; }

void dijs(int st)
{
    memset(ans1, 0x3f, sizeof ans1);
    memset(vis, 0, sizeof vis);
    priority_queue<node> pq;
    ans1[st] = 0;
    pq.push({0, st});
    while (pq.size())
    {
        auto t = pq.top();
        pq.pop();
        int x = t.x;
        if (vis[x])
            continue;
        vis[x] = 1;
        for (auto it : g[x])
        {
            int y = it.first, w = it.second;
            if (vis[y])
                continue;
            if (ans1[y] > ans1[x] + w)
            {
                ans1[y] = ans1[x] + w;
                pq.push({ans1[y], y});
            }
        }
    }
}

void dijs2(int st)
{
    memset(ans1, 0x3f, sizeof ans1);
    memset(vis, 0, sizeof vis);
    priority_queue<node> pq;
    ans1[st] = 0;
    pq.push({0, st});
    while (pq.size())
    {
        auto t = pq.top();
        pq.pop();
        int x = t.x;
        if (vis[x])
            continue;
        vis[x] = 1;
        for (auto it : g2[x])
        {
            int y = it.first, w = it.second;
            if (vis[y])
                continue;
            if (ans1[y] > ans1[x] + w)
            {
                ans1[y] = ans1[x] + w;
                pq.push({ans1[y], y});
            }
        }
    }
}

int main()
{
   // freopen("in36.txt", "r", stdin);
   // freopen("routing.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> gr[i];
        gr[i] = " " + gr[i];
    }
    memset(ans1, 0x3f, sizeof ans1);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            for (int k = 0; k < 4; k++)
            {
                int cx = i + dx[k], cy = j + dy[k];
                vis[pos(cx, cy)] = 1;
                if (cx >= 1 && cx <= n && cy >= 1 && cy <= n)
                {
                    if (gr[cx][cy] == 'B')
                        g[pos(i, j)].emplace_back(pos(cx, cy), 1);
                    else
                        g[pos(i, j)].emplace_back(pos(cx, cy), 0);
                }
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            for (int k = 0; k < 4; k++)
            {
                int cx = i + dx[k], cy = j + dy[k];
                vis[pos(cx, cy)] = 1;
                if (cx >= 1 && cx <= n && cy >= 1 && cy <= n)
                {
                    if (gr[cx][cy] == 'R')
                        g2[pos(i, j)].emplace_back(pos(cx, cy), 1);
                    else
                        g2[pos(i, j)].emplace_back(pos(cx, cy), 0);
                }
            }
        }
    }
    dijs(pos(1, 1));
    int ans = ans1[pos(n, n)];
    dijs2(pos(n, 1));
    cout << ans + ans1[pos(1, n)];
    exit(0);
}

Linux 环境下测试:

2024/6/18 21:11
加载中...