hdu3442(BFS)

链接:点击打开链接

题意:n*m的矩阵中,有五种防御设备,'A'伤害范围为2,杀伤力为1;'B'伤害范围为3,杀伤力为2;'C'只有当在那一个格时才能收到伤害,杀伤力为3;'D'伤害范围为2,杀伤力为4;'E'伤害范围为1,杀伤力为5,只有'.'和'C'可走,受到过某种防御设备的攻击则不会再被同一设备攻击,'$'为起点,'!'为终点,问走到终点最小受到的伤害

代码:

#include <queue>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m;
int xx[]={1,-1,0,0};
int yy[]={0,0,1,-1};
int sign[55][55][2][2][2][2][2];                //每个点有2^5中状态
char str[55][105];
struct node{
    int x,y,sum,flag0,flag1,flag2,flag3,flag4;
    friend bool operator<(node a,node b){
        return a.sum>b.sum;
    }
};
struct edge{
    int x,y;
};
vector<edge> A,B,D,E;
int judge0(int p,int q){
    int i;
    for(i=0;i<A.size();i++)
    if((abs(A[i].x-p)+abs(A[i].y-q))<=2)
    return 1;
    return 0;
}
int judge1(int p,int q){
    int i;
    for(i=0;i<B.size();i++)
    if((abs(B[i].x-p)+abs(B[i].y-q))<=3)
    return 1;
    return 0;
}
int judge3(int p,int q){
    int i;
    for(i=0;i<D.size();i++)
    if((abs(D[i].x-p)+abs(D[i].y-q))<=2)
    return 1;
    return 0;
}
int judge4(int p,int q){
    int i;
    for(i=0;i<E.size();i++)
    if((abs(E[i].x-p)+abs(E[i].y-q))<=1)
    return 1;
    return 0;
}                                               //判断是否受到攻击
int bfs(node st,node en){
    int i,k,cnt;
    priority_queue<node> qu;
    node cur,temp;
    qu.push(st);
    while(qu.size()){
        cur=qu.top(),qu.pop();
        if(cur.x==en.x&&cur.y==en.y)
        return cur.sum;
        for(i=0;i<4;i++){
            temp.x=cur.x+xx[i];
            temp.y=cur.y+yy[i];
            temp.sum=cur.sum;
            temp.flag0=cur.flag0,temp.flag1=cur.flag1,temp.flag2=cur.flag2,temp.flag3=cur.flag3,temp.flag4=cur.flag4;
            if(sign[temp.x][temp.y][cur.flag0][cur.flag1][cur.flag2][cur.flag3][cur.flag4])
            continue;
            if(temp.x>=1&&temp.x<=n&&temp.y>=1&&temp.y<=m){
            if(str[temp.x][temp.y]=='.'){       //只有'.'和'C'可走
            if(!cur.flag0&&judge0(temp.x,temp.y))
            temp.flag0=1,temp.sum+=1;
            if(!cur.flag1&&judge1(temp.x,temp.y))
            temp.flag1=1,temp.sum+=2;
            if(!cur.flag3&&judge3(temp.x,temp.y))
            temp.flag3=1,temp.sum+=4;
            if(!cur.flag4&&judge4(temp.x,temp.y))
            temp.flag4=1,temp.sum+=5;
            sign[temp.x][temp.y][temp.flag0][temp.flag1][temp.flag2][temp.flag3][temp.flag4]=1;
            qu.push(temp);
            }
            else if(str[temp.x][temp.y]=='C'){
            if(!cur.flag0&&judge0(temp.x,temp.y))
            temp.flag0=1,temp.sum+=1;
            if(!cur.flag1&&judge1(temp.x,temp.y))
            temp.flag1=1,temp.sum+=2;
            if(!cur.flag2)
            temp.flag2=1,temp.sum+=3;
            if(!cur.flag3&&judge3(temp.x,temp.y))
            temp.flag3=1,temp.sum+=4;
            if(!cur.flag4&&judge4(temp.x,temp.y))
            temp.flag4=1,temp.sum+=5;
            sign[temp.x][temp.y][temp.flag0][temp.flag1][temp.flag2][temp.flag3][temp.flag4]=1;
            qu.push(temp);
            }
            }
        }
    }
    return -1;
}
int main(){
    int i,j,t,ans,cas;
    node st,en;
    scanf("%d",&t);
    for(cas=1;cas<=t;cas++){
        A.clear(),B.clear();
        D.clear(),E.clear();
        memset(sign,0,sizeof(sign));
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                cin>>str[i][j];
                if(str[i][j]=='$'){
                st.x=i,st.y=j,st.sum=0;
                st.flag0=st.flag1=st.flag2=st.flag3=st.flag4=0;
                }
                if(str[i][j]=='!'){
                en.x=i,en.y=j,en.sum=0;
                str[i][j]='.';
                }
                if(str[i][j]=='A')              //处理出所有防御装置的位置
                A.push_back((edge){i,j});
                if(str[i][j]=='B')
                B.push_back((edge){i,j});
                if(str[i][j]=='D')
                D.push_back((edge){i,j});
                if(str[i][j]=='E')
                E.push_back((edge){i,j});
            }
        }
        ans=bfs(st,en);
        printf("Case %d: %d\n",cas,ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值