hdu 3442 Three Kingdoms 优先级队列+BFS

Problem Description
Three Kingdoms is a funny game. Often Liu Bei is weak and has to run away,so in the game Liu Bei has a skill called "Dunzou". This time you are playing the role of Liu Bei.As Cao Cao's power is so strong, there is nothing you can do but escaping. Please select an optimal path to achieve the purpose .
To simplify the problem, Liu Bei can only move in one of the four direction (up, down,right,left) each time. The map contains the following characters:
‘A’ : Representative of watchtower, which has an attack range of 2(measured by Manhattan distance),and an attack damage of 1.
‘B’ : Representative of Fort, which has an attack range of 3(measured by Manhattan distance),and an attack damage of 2.
‘C’ : Representative of Flame, which has an attack damage of 3 to those who step onto it. 
‘D’ : Representative of Archer, which has anattack range of 2(measured by Manhattan distance), and an attack damage of 4.
‘E’ : Representative of Ordinary soldier, which has anattack range of 1(measured by Manhattan distance), and an attack damage of 5.
‘$’ : Representative of Liu Bei.
‘!’ : Representative of Destination.
'#' : Representative of obstacles
‘.’ : Representative of floor.
Liu Bei can not enter watchtower, forts, Archers, ordinary soldiers,But he can step onto flame or floor.
Some important rules you should note:
1.  Liu Bei will not be hurt by the same thing twice.For example,if Liu Bei has been hurt by one watchtower before,then he step into the attack range of some watchtower later,he will not be hurt. 
2.  When Liu Bei reach the destination,you should first judge the attack damage at the destination then finish the game.
3.  You needn’t judge the attack damage at the start position.
Please choose a path which LiuBei would cost the least HP.
 

Input
In the first line there is an integer T, indicates the number of test cases.(T<=60)
In each case,the first line of the input contains two integer n,m(1<=n,m<=50),reperesenting the size of map(n*m).Then follow n lines,each line contain m characters.
There may be some blank lines between each case.
 

Output
For each test case , output “Case d: “ where d is the case number counted from one.If Liu Bei can reach the destination then output the minimum HP LiuBei may lose, otherwise output -1.
 

Sample Input
  
  
1 4 3 .$. ACB ACB .!.
 

Sample Output
  
  
Case 1: 6

//


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=10000;
struct Node
{
    int x,y;
    int hurt;
    int vis[10];//受伤害情况
    friend bool operator <(Node h,Node k)
    {
        return h.hurt>k.hurt;
    }
};
int n,m;
char str[60][60];
int atk[60][60][10];//1:x,y能被attack攻击
int mark[60][60];//
Node st,ed;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};
int hurt[5]={1,2,3,4,5};
int judge(int x,int y,int vis[10])
{
    int ans=0;
    for(int i=0;i<5;i++)
    {
        if(atk[x][y][i]&&vis[i]==0)
        {
            ans+=hurt[i];
            vis[i]=1;
        }
    }
    return ans;
}
int OK(int x,int y)//in frame
{
    if(x>=0&&x<n&&y>=0&&y<m) return 1;
    return 0;
}
void dfs(int x,int y,int step,int attack)//攻击范围
{
    atk[x][y][attack]=1;
    if(step==0) return ;//注意这句话的位置
    for(int i=0;i<4;i++)
    {
        int tx=x+xx[i],ty=y+yy[i];
        if(OK(tx,ty)) dfs(tx,ty,step-1,attack);
    }
}
int BFS()
{
    memset(st.vis,0,sizeof(st.vis));
    priority_queue<Node> q;
    Node cur,nxt,tmp;
    q.push(st);
    memset(mark,0,sizeof(mark));
    mark[st.x][st.y]=1;
    while(!q.empty())
    {
        cur=q.top();q.pop();
        if(cur.x==ed.x&&cur.y==ed.y) return cur.hurt;
        for(int i=0;i<4;i++)
        {
            nxt.x=cur.x+xx[i],nxt.y=cur.y+yy[i];
            if(!OK(nxt.x,nxt.y)||mark[nxt.x][nxt.y]) continue;
            memcpy(nxt.vis,cur.vis,sizeof(cur.vis));
            if(str[nxt.x][nxt.y]=='C'||str[nxt.x][nxt.y]=='.')
            {
                mark[nxt.x][nxt.y]=1;
                nxt.hurt=cur.hurt+judge(nxt.x,nxt.y,nxt.vis);
                q.push(nxt);
            }
            else if(str[nxt.x][nxt.y]=='!')
            {
                // mark[nxt.x][nxt.y]=1;  这里不一定是最优解
                nxt.hurt=cur.hurt+judge(nxt.x,nxt.y,nxt.vis);
                q.push(nxt);
            }
        }
    }
    return -1;
}
int main()
{
    int ci,pl=1;scanf("%d",&ci);
    while(ci--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++) scanf("%s",str[i]);
        memset(atk,0,sizeof(atk));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(str[i][j]=='$')
                {
                    st.x=i,st.y=j;
                    st.hurt=0;
                }
                else if(str[i][j]=='!')
                {
                    ed.x=i,ed.y=j;
                }
                else if(str[i][j]=='A')
                {
                    dfs(i,j,2,0);
                }
                else if(str[i][j]=='B')
                {
                    dfs(i,j,3,1);
                }
                else if(str[i][j]=='C')
                {
                    dfs(i,j,0,2);
                }
                else if(str[i][j]=='D')
                {
                    dfs(i,j,2,3);
                }
                else if(str[i][j]=='E')
                {
                    dfs(i,j,1,4);
                }
            }
        }
        int ans=BFS();
        printf("Case %d: %d\n",pl++,ans);
    }
    return 0;
}

  • 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、付费专栏及课程。

余额充值