AtCoder Beginner Contest 184 E - Third Avenue

官方题解

 

还参考了这位大佬的代码(算是一样了)AtCoder Beginner Contest 184 E Third Avenue | 多传送门搜索_一只酷酷光儿的博客-CSDN博客

传送门搜索把我整蒙了,首先为每一个位置设置一个最小到达时间数组(vis),用bfs不断去更新vis数组,搜索的时候,只要达到一个字母位置,就可以达到所有和它一样字母的位置,最后答案就在相应的位置坐标上。

// #pragma GCC optimize(2)
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <random>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cout.tie(0);
#define lson(x) (x << 1)
#define rson(x) (x << 1 | 1)
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e3 + 10;
const int maxm = 1e7 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
// int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int sx, sy, fx, fy;

int n, m;
char a[maxn][maxn];
int vis[maxn][maxn];
bool inqueue[maxn];
vector<pair<int, int>> v[26];
bool check(int x, int y)
{
    if (x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != '#')
        return true;
    return false;
}
int BFS()
{
    memset(vis, -1, sizeof vis);
    vis[sx][sy] = 0;
    queue<P> q;
    q.push(make_pair(sx, sy));
    while (!q.empty())
    {
        P now = q.front();
        q.pop();
        int x = now.first;
        int y = now.second;
        for (int i = 0; i < 4; i++)
        {
            int tx = x + dis[i][0];
            int ty = y + dis[i][1];
            if (check(tx, ty) && (vis[tx][ty] == -1 || vis[tx][ty] > vis[x][y] + 1))
            {
                if (a[tx][ty] != '.')
                {
                    if (a[tx][ty] == 'G')
                    {
                        if (vis[tx][ty] == -1)
                            vis[tx][ty] = vis[x][y] + 1;
                        else
                            vis[tx][ty] = min(vis[tx][ty], vis[x][y] + 1);
                    }
                    else
                    {
                        int id = a[tx][ty] - 'a';
                        if (inqueue[id])
                            vis[tx][ty] = min(vis[x][y] + 1, vis[tx][ty]);
                        else
                        {
                            for (int k = 0; k < v[id].size(); k++)
                            {
                                int ttx = v[id][k].first;
                                int tty = v[id][k].second;
                                if (tx == ttx && ty == tty)
                                    vis[tx][ty] = vis[x][y] + 1;
                                else
                                    vis[ttx][tty] = vis[x][y] + 2;
                                q.push(make_pair(ttx, tty));
                            }
                            inqueue[id] = true;
                        }
                    }
                }
                else
                {
                    vis[tx][ty] = vis[x][y] + 1;
                    q.push(make_pair(tx, ty));
                }
            }
        }
    }
    return vis[fx][fy];
}
int main()
{
#ifdef WXY
    freopen("in.txt", "r", stdin);
    //  freopen("out.txt", "w", stdout);
#endif
    IO;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            char c = a[i][j];
            if (islower(c))
            {
                int t = c - 'a';
                v[t].push_back(make_pair(i, j));
            }
            else if (c == 'S')
                sx = i, sy = j;
            else if (c == 'G')
                fx = i, fy = j;
        }
    }
    cout << BFS();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值