HDU 3345 War Chess (BFS)

本文介绍了一个基于WarChess的游戏地图路径搜索问题,并提供了一个使用BFS算法的解决方案。玩家需要根据移动值(MV)和地图上的障碍物(如树木、河流、敌人等),计算出能够到达的所有格子。

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

War Chess

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

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
.*.

Author

shǎ崽

Source

HDU2010省赛集训队选拔赛(校内赛)


题意:

把能走的地方用*标记,输出图。


point:

其他和普通的BFS都差不多!但是,判断附近有没有E的时候要先判断能不能走到那个格子,如果不行还是不能走的。代码写法不同会使题目相对变得复杂T-T,WA了好久。

#include<stdio.h>
#include <queue>
#include <iostream>
#include <string.h>
using namespace std;
const int N =100+5;
char mp[N][N];
int n,m,MV;
int mv[N][N];
int em[N][N];
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
struct node
{
    int x,y;
};
queue<node>q;
void bfs()
{
    while(!q.empty())
    {
        node now=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int xx=now.x+dir[i][0];
            int yy=now.y+dir[i][1];
            if(xx>n||xx<1||yy>m||yy<1||mp[xx][yy]=='#'||mp[xx][yy]=='E'||mp[xx][yy]=='Y') continue;
            if(mp[xx][yy]=='R')
            {
                if(mv[xx][yy]>=mv[now.x][now.y]-3)continue;
                else mv[xx][yy]=mv[now.x][now.y]-3;
            }
            else if(mp[xx][yy]=='T')
            {
                if(mv[xx][yy]>=mv[now.x][now.y]-2)continue;
                else   mv[xx][yy]=mv[now.x][now.y]-2;
            }
            else if(mp[xx][yy]=='.'||mp[xx][yy]=='P')
            {
                if(mv[xx][yy]>=mv[now.x][now.y]-1)continue;
                else   mv[xx][yy]=mv[now.x][now.y]-1;
            }
            if(mv[xx][yy]<=0) continue;
            if(em[xx][yy]==1)
            {
                if(mv[xx][yy]>=0) //因为缺少这句话WA了一小时。
                    mv[xx][yy]=0;
                continue;
            }
            node keep;
            keep.x=xx;
            keep.y=yy;
            q.push(keep);
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(mv,-1,sizeof mv);
        memset(mp,0,sizeof mp);
        memset(em,0,sizeof em);
        scanf("%d %d %d",&n,&m,&MV);
        int bi,bj;
        for(int i=1; i<=n; i++)
        {
            getchar();
            for(int j=1; j<=m; j++)
            {
                scanf("%c",&mp[i][j]);
                if(mp[i][j]=='Y')
                {
                    mv[i][j]=MV;
                    bi=i;
                    bj=j;
                }
                else if(mp[i][j]=='E')
                {
                    if(i+1<=n) em[i+1][j]=1;
                    if(i-1>=1) em[i-1][j]=1;
                    if(j+1<=m) em[i][j+1]=1;
                    if(j-1>=1) em[i][j-1]=1;
                }
            }
        }
        while(!q.empty()) q.pop();
        node b;
        b.x=bi;
        b.y=bj;
        q.push(b);
        bfs();
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(mv[i][j]>=0)
                {
                    if(mp[i][j]=='P'||mp[i][j]=='Y'||mp[i][j]=='#') printf("%c",mp[i][j]);
                    else printf("*");
                }
                else
                    printf("%c",mp[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }

    return 0;
}


代码写的很丑,可以改的更短一点。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值