Carrying Out A Task(bfs)

题目描述
During the process of the military exercise, there is a ship on the sea level .The ship will go to certain place to carry out a task. For every action, the ship has two ways to sail. They are normal sailing and accelerated sailing. The normal speed of the ship is certain, when the ship sails normally, it can only move 1 step to the adjacent normal sea level. The ship can also accelerate. There are 2 kinds of accelerated sailings, one is moving forward d steps (d <= 5) in a straight line, and it must move forward d steps exactly every time it accelerates, The d steps must be on the normal sea level, otherwise, it can not accelerate. The other is accelerating while getting through the undercurrent. There are a lot of undercurrents on the sea, and entering the undercurrent area needs to accelerate when the ship is 1 step to the undercurrent. However, the ship itself will be damaged more or less by the undercurrent, After entering the undercurrent, the speed of the ship will become normal immediately. Every time it accelerates, the ship has to consume a certain B energy, and when it starts up ,it carries certain B energy.

While the ship is sailing on the sea, it needs to consume a certain A energy. One unit of distance will consume one unit of A energy, and when the ship starts up, it carries enough A energy.

There are many reefs on the sea, and the ship can not get through.

Now the ship is required to sail to the certain place, of course, to minimize the damage to the ship itself is a priority because the cost of ships is very expensive. The damage is, of course, the smaller, the better. At the same time, an attempt should be made to control the consumption of A energy to the smallest amount during the whole process because the cost of A energy is much more expensive than that of B energy, and you can use B energy which the ship carried when it started up as you wish.

Now the question is to work out the minimal times of action from the departure point to the destination under the condition that to minimize the damage to the ship is a priority and then the consumption of A energy to the smallest degree.
在这里插入图片描述
输入
The input file contains several test cases, the first line contains an integer T, indicates the number of test cases. In each case the first line includes two integers n, m (5 <= n, m <= 20), which indicate the size of the sea level for military exercises, and n rows and m columns are the current state of the sea level (‘S’ indicates the ship’s initial position, ‘E’ indicates the destination place, ‘#’ indicates the reefs, ‘*’ indicates the undercurrent , ’ ’ the normal sea level), followed a line with a number d in it, it indicates the distance of the first kind of acceleration, then another line includes two integers, indicate that the initial value of the B energy and the value of the B energy needed while accelerating every time.

输出
Output an integer to indicate the smallest times of action, and if can not reach the task place, then output “can not reach!”

样例输入
2
5 10
##########
#E #
####### #
#S #
##########
5
10 2
6 10
##########
#E #
#
#######
#
######*#
#S #
##########
5
3 2

样例输出
8
can not reach!

思路
bfs搜索,用优先队列根据船只的损坏程度,a燃油的消耗量,与操作次数进行排序,当到达终点时直接输出,即可保证结果为最优,注意搜索时的条件

代码实现

#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
const int N=30;
typedef pair<int,int> P;
 
int n,m,t,d,sumb,useb;
char ma[N][N];
int ar[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int dmg[N][N],aco[N][N],bco[N][N];
 
struct node
{
    int x,y,res,dmg,op,acost,bcost;
    node(){}
    node(int tx,int ty,int ts,int td,int tp,int ta,int tb)
    {
        x=tx,y=ty,res=ts,dmg=td,op=tp,acost=ta,bcost=tb;
    }
    bool friend operator < (node a,node b)
    {
        if(a.dmg!=b.dmg) return a.dmg>b.dmg;
        if(a.acost!=b.acost) return a.acost>b.acost;
        return a.op>b.op;
    }
}s,e;

int bfs()
{
    for(int i=0;i<=n;i++)
        for(int j=0;j<=m;j++) dmg[i][j]=bco[i][j]=aco[i][j]=410;
    priority_queue<node>q;
    dmg[s.x][s.y]=bco[s.x][s.y]=aco[s.x][s.y]=0;
    s.res=sumb,s.dmg=0,s.op=0,s.acost=0;
    q.push(s);
    while(!q.empty())
    {
        node top=q.top();
        q.pop();
        if(top.x==e.x && top.y==e.y) return top.op;
        for(int i=0;i<4;i++)
        {
            node tmp=top;
            if(top.res>=useb)
            {
                int j;
                for(j=0;j<d;j++)
                {
                    tmp.x+=ar[i][0],tmp.y+=ar[i][1];
                    if(tmp.x<=0 || tmp.x>n || tmp.y<=0 || tmp.y>m || ma[tmp.x][tmp.y]=='#' || ma[tmp.x][tmp.y]=='*') break;
                }
                if(j==d)
                {
                    if(dmg[tmp.x][tmp.y]>tmp.dmg || (dmg[tmp.x][tmp.y]==tmp.dmg && aco[tmp.x][tmp.y]>tmp.acost+d) ||(dmg[tmp.x][tmp.y]==tmp.dmg && aco[tmp.x][tmp.y]==tmp.acost+d && bco[tmp.x][tmp.y]>tmp.bcost+1))
                    {
                        dmg[tmp.x][tmp.y]=tmp.dmg;
                        aco[tmp.x][tmp.y]=tmp.acost+d;
                        bco[tmp.x][tmp.y]=tmp.bcost+1;
                        tmp.res=top.res-useb;
                        tmp.op=top.op+1;
                        tmp.acost+=d,
                        tmp.bcost++;
                        q.push(tmp);
                    }
                }
            }
            tmp=top;
            tmp.x=top.x+ar[i][0],tmp.y=top.y+ar[i][1];
            if(tmp.x<=0 || tmp.x>n || tmp.y<=0 || tmp.y>m || ma[tmp.x][tmp.y]=='#' ) continue;
            if(ma[tmp.x][tmp.y]!='*'&&(dmg[tmp.x][tmp.y]>tmp.dmg || (dmg[tmp.x][tmp.y]==tmp.dmg && aco[tmp.x][tmp.y]>tmp.acost+1) ||(dmg[tmp.x][tmp.y]==tmp.dmg && aco[tmp.x][tmp.y]==tmp.acost+1 && bco[tmp.x][tmp.y]>tmp.bcost)))
            {
                dmg[tmp.x][tmp.y]=tmp.dmg;
                aco[tmp.x][tmp.y]=tmp.acost+1;
                bco[tmp.x][tmp.y]=tmp.bcost;
                tmp.op=top.op+1;
                tmp.acost=top.acost+1;
                tmp.bcost=top.bcost;
                q.push(tmp);
            }
            else if(ma[tmp.x][tmp.y]=='*' && top.res>=useb && (dmg[tmp.x][tmp.y]>tmp.dmg || (dmg[tmp.x][tmp.y]==tmp.dmg && aco[tmp.x][tmp.y]>tmp.acost+1) ||(dmg[tmp.x][tmp.y]==tmp.dmg && aco[tmp.x][tmp.y]==tmp.acost+1 && bco[tmp.x][tmp.y]>tmp.bcost+1)))
            {
                dmg[tmp.x][tmp.y]=tmp.dmg+1;
                aco[tmp.x][tmp.y]=tmp.acost+1;
                bco[tmp.x][tmp.y]=tmp.bcost+1;
                tmp.dmg=top.dmg+1;
                tmp.res=top.res-useb;
                tmp.op=top.op+1;
                tmp.acost=top.acost+1;
                tmp.bcost=top.bcost+1;
                q.push(tmp);
            }
        }
    }
    return -1;
}
 
int main()
{
    int T;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        scanf("%d %d",&n,&m);
        getchar();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                ma[i][j]=getchar();
                if(ma[i][j] == 'E') e.x=i,e.y=j;
                if(ma[i][j] == 'S') s.x=i,s.y=j;
            }
            getchar();
        }
        scanf("%d\n%d %d",&d,&sumb,&useb);
        getchar();
        int ans = bfs();
        if(ans == -1) printf("can not reach!\n");
        else printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值