War Chess

这个题我做的时候错了很多次,这种没有固定的终点的题一般来说都得用优先队列,有固定的终点的直接用队列搜索出其最短的距离就ok了,这道题有个坑点就是当你走到那一步的时候,如果它周围的四个方向上有敌人的话,那么它的hp就会直接更新为零。所以,当你走到下一步的时候我们得再搜一下它的四个方向是不是有有敌人,然后判断它的hp的值,当然前提是得你能走到那个位置,不然的话其它的东西都是白搭,以后做题的时候一定要分析清题的所有意思,再去做题,争取做到一次性就ac。

 

War chess is hh's favorite game: 
In this game, there is an N * M battle map, and every player has his own Moving Val (MV). In each round, every player can move in four directions as long as he has enough MV. To simplify the problem, you are given your position and asked to output which grids you can arrive. 
 
In the map: 
'Y' is your current position (there is one and only one Y in the given map). 
'.' is a normal grid. It costs you 1 MV to enter in this gird. 
'T' is a tree. It costs you 2 MV to enter in this gird. 
'R' is a river. It costs you 3 MV to enter in this gird. 
'#' is an obstacle. You can never enter in this gird. 
'E's are your enemies. You cannot move across your enemy, because once you enter the grids which are adjacent with 'E', you will lose all your MV. Here “adjacent” means two grids share a common edge. 
'P's are your partners. You can move across your partner, but you cannot stay in the same grid with him final, because there can only be one person in one grid.You can assume the Ps must stand on '.' . so ,it also costs you 1 MV to enter this grid.

Input

The first line of the inputs is T, which stands for the number of test cases you need to solve. 
Then T cases follow: 
Each test case starts with a line contains three numbers N,M and MV (2<= N , M <=100,0<=MV<= 65536) which indicate the size of the map and Y's MV.Then a N*M two-dimensional array follows, which describe the whole map.

Output

Output the N*M map, using '*'s to replace all the grids 'Y' can arrive (except the 'Y' grid itself). Output a blank line after each case.

Sample Input

5
3 3 100
...
.E.
..Y

5 6 4
......
....PR
..E.PY
...ETT
....TT

2 2 100
.E
EY

5 5 2
.....
..P..
.PYP.
..P..
.....

3 3 1
.E.
EYE
...

Sample Output

...
.E*
.*Y

...***
..**P*
..E*PY
...E**
....T*

.E
EY

..*..
.*P*.
*PYP*
.*P*.
..*..

.E.
EYE
.*.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
char mp[110][110];
int book[110][110],n,m,hp;
int next1[4][2]={0,1,1,0,0,-1,-1,0};
struct node
{
    bool friend operator <(node a,node b)
    {
        return a.hp<b.hp;
    }
    int x;
    int y;
    int hp;
};
priority_queue<node>que;
void bfs(int x,int y ,int hp)
{
    int i,j,k,tx,ty;
    node cur,tem;
    tem.x=x;
    tem.y=y;
    tem.hp=hp;
    book[x][y]=1;
    que.push(tem);
    while(!que.empty())
    {
        cur=que.top();
        que.pop();
        if(cur.hp<=0)  continue;
        for(k=0;k<4;k++)
        {
           tem.x=cur.x+next1[k][0];
           tem.y=cur.y+next1[k][1];
           if(tem.x<0||tem.x>=n||tem.y<0||tem.y>=m||mp[tem.x][tem.y]=='E'||book[tem.x][tem.y]||mp[tem.x][tem.y]=='#') continue;
           if(mp[tem.x][tem.y]=='.') tem.hp=cur.hp-1;
            if(mp[tem.x][tem.y]=='T') tem.hp=cur.hp-2;
            if(mp[tem.x][tem.y]=='R')  tem.hp=cur.hp-3;
            if(mp[tem.x][tem.y]=='P')  tem.hp=cur.hp-1;
            if(tem.hp<0) continue;
           for(i=0;i<4;i++)
           {
               tx=tem.x+next1[i][0];
               ty=tem.y+next1[i][1];
               if(tx<0||tx>=n||ty<0||ty>=m) continue;
               if(mp[tx][ty]=='E')
               {
                   book[tx][ty]=1;
                   tem.hp=0;
                   break;
               }
           }
           que.push(tem);
           book[tem.x][tem.y]=1;
           if(mp[tem.x][tem.y]!='P')
           {
               mp[tem.x][tem.y]='*';
           }
        }
    }
}
int main()
{
    int t,i,j,k,sx,sy;
    scanf("%d",&t);
    while(t--)
    {
        memset(book,0,sizeof(book));
        memset(mp,0,sizeof(mp));
        scanf("%d%d%d",&n,&m,&hp);
        for(i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
            for(j=0;j<m;j++)
            {
                if(mp[i][j]=='Y')
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        while(!que.empty())  que.pop();
        bfs(sx,sy,hp);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                printf("%c",mp[i][j]);
            }
            printf("\n");
        }
       printf("\n");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值