ac的war chess hdu3345

本文介绍了一款战棋游戏中的路径规划算法实现,玩家在限定的移动值内,根据地图上的不同地形(如草地、树林、河流等)消耗不同的移动点数,避开敌人和障碍物,最终达到能够到达的所有格子。文章详细解释了如何使用广度优先搜索算法来解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
InputThe 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.OutputOutput 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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
int dire[4][2] = { 1,0, -1,0, 0,1, 0,-1};
char a[105][105];
int visit[105][105];
int n, m , mvv;
int xx, yy;
struct pla
{
    int x;
    int y;
    int mv;
     bool operator<(const pla &oth)const
    {
        return mv<oth.mv;
    }
}st;
int check( int x, int y)
{
   if(x<0 || y<0 || x>=n || y>=m)
        return 0;
    else
        return 1;
}


bool judge(int i,int j){
    int k;
    for(int k=0;k<4;k++){
        pla nex;
        nex.x=i+dire[k][1],nex.y=j+dire[k][0];
        if(check(nex.x,nex.y) && a[nex.x][nex.y] == 'E') return true;
    }
    return false;
}


void bfs()
{
    priority_queue<pla>q;
    pla now,next;
    memset(visit,0,sizeof(visit));
    q.push(st);
    visit[st.x][st.y]=1;
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(now.mv<=0)
            continue;
        for(int k=0;k<4;k++)
        {
            next.x=now.x+dire[k][1] , next.y=now.y+dire[k][0] , next.mv=now.mv;
            if(visit[next.x][next.y] || !check(next.x,next.y) || a[next.x][next.y] == 'E' || a[next.x][next.y] == '#')
                continue;
            if(a[next.x][next.y] == '.')
            next.mv--;
            else if(a[next.x][next.y]=='T')
             next.mv-=2;
            else if(a[next.x][next.y]=='R')
            next.mv-=3;
            else if(a[next.x][next.y]=='P')
            next.mv--;if(next.mv<0)
            continue;
            if(judge(next.x,next.y))
             next.mv=0;
            if(a[next.x][next.y] != 'P')
            a[next.x][next.y]='*';
            visit[next.x][next.y]=1;q.push(next);
        }
    }
}




int main()
{
    int t;
    scanf("%d", &t);
    while( t -- )
    {
        memset( a, 0, sizeof( a ));
        memset( visit, 0, sizeof( visit));
        int i, j;
        scanf("%d%d%d", &n, &m, &mvv);
         for(i = 0; i<n; i++)
        {
            scanf("%s",a[i]);
            for(j = 0; a[i][j]; j++)
            {
                if(a[i][j] == 'Y')
                {
                    xx = i;
                    yy = j;
                    st.x = i;
                    st.y = j;
                    st.mv = mvv;
                }
            }
        }
        bfs( );
    for(i = 0; i<n; i++)
            printf("%s\n",a[i]);
        printf("\n");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值