HDU 5040 Instrusive(BFS+优先队列)

题意比较啰嗦。

就是搜索加上一些特殊的条件,比如可以在原地不动,也就是在原地呆一秒,如果有监控也可以花3秒的时间走过去。

这种类型的题目还是比较常见的。以下代码b[i][j][x]表示格子i行j列在x时刻有监控照的到。因为只有4个方向,所以只需要时间对4取模就行。具体细节见代码。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 550;
const int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};
char mp[maxn][maxn];
int b[maxn][maxn][4];
int T, n, kase;
bool vis[maxn][maxn][4];
struct Node {
    int x, y, step;
    bool operator < (const Node &b) const {
        return step > b.step;
    }
};
Node tar;
int bfs(int x, int y)
{
    memset(vis, false, sizeof(vis));
    priority_queue<Node> Q;
    Node cur, nex;
    cur.x = x; cur.y = y; cur.step = 0;
    vis[x][y][0] = true;
    Q.push(cur);
    while (!Q.empty())
    {
        cur = Q.top(); Q.pop();
        if (tar.x == cur.x && tar.y == cur.y) return cur.step;
        for (int i = 0; i < 5; i++)//最后一个状态是可以呆在原地不动
        {
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
            if (mp[nx][ny] == '#') continue;
            nex = cur;
            if (b[nx][ny][cur.step % 4] || b[cur.x][cur.y][cur.step % 4])//如果下一个位置有监控或者当前这个位置监控的到
            {
                if (nx == cur.x && ny == cur.y && !vis[nx][ny][(cur.step + 1) % 4])//如果当前这个位置监控的到,
                {
                    nex.step++;
                    vis[nx][ny][nex.step % 4] = true;
                    Q.push(nex);
                }
                else if (!vis[nx][ny][(cur.step + 3) % 4])//如果下一个监控的到
                {
                    nex.x = nx; nex.y = ny;
                    nex.step += 3;
                    vis[nx][ny][nex.step % 4] = true;
                    Q.push(nex);
                }
            }
            else if (!vis[nx][ny][(cur.step + 1) % 4])//否则直接走
            {
                nex.x = nx; nex.y = ny; nex.step++;
                vis[nx][ny][nex.step % 4] = true;
                Q.push(nex);
            }
        }
    }
    return -1;
}
int main()
{
    scanf("%d", &T);
    while (T--)
    {
        int x, y;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
            scanf("%s", mp[i]);
        memset(b, 0, sizeof(b));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (mp[i][j] == 'M')
                {
                    x = i; y = j;
                }
                else if (mp[i][j] == 'N')
                {
                    b[i][j][0] = b[i][j][1] = b[i][j][2] = b[i][j][3] = 1;//它本身这个位置不管任意时刻都被监控到
                    if (i - 1 >= 0) b[i - 1][j][0] = 1;//表示紧挨着它上面的那个在1秒的时候被监控到
                    if (j + 1 < n) b[i][j + 1][1] = 1;//紧挨着它右边的在第2秒的时候,意思就是下一秒
                    if (i + 1 < n) b[i + 1][j][2] = 1;//下两秒
                    if (j - 1 >= 0) b[i][j - 1][3] = 1;//下三秒在忘左边的位置被监控的到
                }
                else if (mp[i][j] == 'E')
                {
                    b[i][j][0] = b[i][j][1] = b[i][j][2] = b[i][j][3] = 1;
                    if (i - 1 >= 0) b[i - 1][j][3] = 1;
                    if (j + 1 < n) b[i][j + 1][0] = 1;
                    if (i + 1 < n) b[i + 1][j][1] = 1;
                    if (j - 1 >= 0) b[i][j - 1][2] = 1; 
                }
                else if (mp[i][j] == 'S')
                {
                    b[i][j][0] = b[i][j][1] = b[i][j][2] = b[i][j][3] = 1;
                    if (i - 1 >= 0) b[i - 1][j][2] = 1;
                    if (j + 1 < n) b[i][j + 1][3] = 1;
                    if (i + 1 < n) b[i + 1][j][0] = 1;
                    if (j - 1 >= 0) b[i][j - 1][1] = 1; 
                }
                else if (mp[i][j] == 'W')
                {
                    b[i][j][0] = b[i][j][1] = b[i][j][2] = b[i][j][3] = 1;
                    if (i - 1 >= 0) b[i - 1][j][1] = 1;
                    if (j + 1 < n) b[i][j + 1][2] = 1;
                    if (i + 1 < n) b[i + 1][j][3] = 1;
                    if (j - 1 >= 0) b[i][j - 1][0] = 1; 
                }
                else if (mp[i][j] == 'T')
                {
                    tar.x = i; tar.y = j;
                }
            }
        }
        printf("Case #%d: %d\n", ++kase, bfs(x, y));
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Howe-Young/p/4833671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值