1 条题解

  • 12
    @ 2023-4-17 17:24:59

    广搜套路题,就是在求最短路的基础上多了一个查克拉的限制,我们定义一个结构体,里面存储到了每个点的坐标,步数,以及查克拉,然后去周围点的时候对正常的通路和大蛇丸手下,做不同处理即可。第一次走到终点,直接输出步数即可。

    #include <bits/stdc++.h>
    #define ll long long
    #define inf 0x3f3f3f3f
    #define pb push_back
    #define fi first
    #define se second
    #define re register
    using namespace std;
    typedef pair<ll, ll> pi;
    int m, n, t, sx, sy, ex, ey;
    char a[205][205];
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    struct node
    {
        int x, y, time, hp;
    };
    bool vis[205][205], flag;
    queue<node> q;
    void bfs()
    {
        q.push({sx, sy, 0, t});
        vis[sx][sy] = 1;
        while (q.size())
        {
            node now = q.front();
            q.pop();
            if (now.x == ex && now.y == ey)
            {
                flag = true;
                cout << now.time;
                return ;
            }
            for (int i = 0; i < 4; i++)
            {
                int nx = now.x + dx[i], ny = now.y + dy[i];
                if (nx < 1 || nx > m || ny < 1 || ny > n) continue;
                if (!vis[nx][ny])
                {
                    vis[nx][ny] = 1;
                    if (a[nx][ny] == '*' || a[nx][ny] == '+') q.push({nx, ny, now.time + 1, now.hp});
                    else if (a[nx][ny] == '#' && now.hp > 0) q.push({nx, ny, now.time + 1, now.hp - 1}); 
                }
            }
        }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> m >> n >> t;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                cin >> a[i][j];
                if (a[i][j] == '@') sx = i, sy = j;
                if (a[i][j] == '+') ex = i, ey = j;
            }
        }
        bfs();
        if (!flag) cout << -1;
        return 0;
    }
    
    • 1

    信息

    ID
    345
    时间
    1000ms
    内存
    256MiB
    难度
    5
    标签
    递交数
    164
    已通过
    58
    上传者