HDU 3345 War Chess (优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3345

 

题意:某人最开始在Y点,拥有mv的能量,如果下一格是同伴或者空地会消耗一点能量,如果是森林则消耗两点能量,小河会消耗三点,如果该点四周存在敌人,则能量清零,而且不能停留在同伴的格子,将所能走到的所有格子全部变为‘*’,输出最后的图

 

思路:使用优先队列进行BFS

 

 

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <utility>
#include <functional>
#pragma comment (linker, "/STACK:1024000000,1024000000")

using namespace std;

const int maxn = 110;
const int inf = 0x3f3f3f3f;

char s[maxn][maxn];
int vis[maxn][maxn];

int n, m, mv;
int sx, sy;

int dx[4] = { -1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};

struct node
{
    int x, y, v;
    node(int x = 0, int y = 0, int v = 0) : x(x), y(y), v(v) {}
    bool operator < (const node &rhs) const
    {
        return v < rhs.v;
    }
};

bool ok(int x, int y)
{
    if (x >= 0 && x < n && y >= 0 && y < m) return true;
    return false;
}

void bfs()
{
    memset(vis, 0, sizeof(vis));
    priority_queue <node> que;
    que.push(node(sx, sy, mv));
    while (!que.empty())
    {
        node cur = que.top();
        que.pop();

        int x = cur.x, y = cur.y, v = cur.v;
        if (s[x][y] != 'P' && (x != sx || y != sy))
            s[x][y] = '*';

        if (v <= 0) continue;
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            int nv = v;
            if (!ok(nx, ny) || vis[nx][ny] || s[nx][ny] == '#' || s[nx][ny] == 'E')
                continue;
            if (s[nx][ny] == '.')  nv--;
            if (s[nx][ny] == 'P')  nv--;
            if (s[nx][ny] == 'T')  nv -= 2;
            if (s[nx][ny] == 'R')  nv -= 3;

            if (nv < 0) continue;

            for (int j = 0; j < 4; j++)
            {
                int ex = nx + dx[j];
                int ey = ny + dy[j];
                if (!ok(ex, ey)) continue;
                if (s[ex][ey] == 'E')
                {
                    nv = 0;
                    break;
                }
            }

            que.push(node(nx, ny, nv));
            vis[nx][ny] = 1;
        }
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d%d", &n, &m, &mv);
        for (int i = 0; i < n; i++)
        {
            scanf("%s", s[i]);
            for (int j = 0; j < m; j++)
                if (s[i][j] == 'Y')
                    sx = i, sy = j;
        }

        bfs();
        for (int i = 0; i < n; i++)
            printf("%s\n", s[i]);
        printf("\n");
    }
    return 0;
}

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值