HDU-2612-Find a way

链接:https://vjudge.net/problem/HDU-2612#author=zhang95986

题意:

hsj和lsh最近迷上了pokemon go的游戏。在双十一大物期中考试来临之前,他们想抓一只稀有土拨鼠来攒攒人品(因为土拨鼠的刷新地点最近来到了哈工程)
但是由于土拨鼠过于强大,他的雷霆半月斩以及惊天浪涛沙都可以轻松的将他们两击败,但是他们两的合击必杀技流影电光闪以及天羽屠鼠舞可以将土拨鼠打至昏迷状态,并可将其捕获。
但是因为这是款按时间付费的游戏,他们需要尽快捕捉到土拨鼠(即他们两到土拨鼠的时间之和需要最少),因此他们找到了你来帮他们解决这个问题。 规定每走一步需要花费11分钟。

思路:

bfs记录两个人到每个土拨鼠点的步数,找到一个和最小的位置。

代码:

#include <iostream>
#include <memory.h>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <queue>
#include <string>

using namespace std;

typedef long long LL;

const int MAXN = 200 + 10;

int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
char Map[MAXN][MAXN];
int vis[MAXN][MAXN];
int res[MAXN][MAXN][2];
int n, m;
struct Node
{
    int _x, _y, _step;
    Node(int x, int y, int step):_x(x), _y(y), _step(step){}
};
vector<pair<int, int> > node;

void Bfs(int sx, int sy, int w)
{
    memset(vis, 0, sizeof(vis));
    queue<Node> que;
    que.push(Node(sx, sy, 0));
    vis[sx][sy] = 1;
    while (!que.empty())
    {
        Node now = que.front();
        if (Map[now._x][now._y] == '@')
            res[now._x][now._y][w] = now._step;
        que.pop();
        for (int i = 0;i < 4;i++)
        {
            int nx = now._x + Next[i][0];
            int ny = now._y + Next[i][1];
            if (nx < 1 || nx > n || ny < 1 || ny > m)
                continue;
            if (Map[nx][ny] == '#' || vis[nx][ny] == 1)
                continue;
            que.push(Node(nx, ny, now._step + 1));
            vis[nx][ny] = 1;
        }
    }
}

int main()
{
    int sx1, sy1, sx2, sy2;

    while (cin >> n >> m)
    {
        memset(res, 0, sizeof(res));
        memset(Map, 0, sizeof(Map));
        node.clear();
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= m;j++)
            {
                cin >> Map[i][j];
                if (Map[i][j] == 'Y')
                    sx1 = i, sy1 = j;
                if (Map[i][j] == 'M')
                    sx2 = i, sy2 = j;
                if (Map[i][j] == '@')
                    node.push_back(make_pair(i, j));
            }
         Bfs(sx1, sy1, 0);
         Bfs(sx2, sy2, 1);
         int re = 999999;

         for (int i = 0;i < node.size();i++)
         {
             if (res[node[i].first][node[i].second][0] != 0 && res[node[i].first][node[i].second][1] != 0)
                 re = min(re, res[node[i].first][node[i].second][0] + res[node[i].first][node[i].second][1]);
         }

         cout << re * 11 << endl;
    }

    return 0;
}

  

原文地址:https://www.cnblogs.com/YDDDD/p/10591192.html