HDU - 3345 War Chess 广搜+优先队列

本文介绍了一款战棋游戏中的玩家移动算法实现,玩家可以在给定的移动值范围内进行移动,通过广度优先搜索算法确定可达格子,并考虑了不同地形对移动值的影响。

摘要生成于 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
.*.

 

bfs扩展四个方向,消耗mv最少优先级最高,加入优先队列,优先扩展mv最高点。保证*扩展到最远边界。

代码150+。。写出来还蛮有成就感的。。 

StatusAccepted
Time15ms
Memory1620kB
Length3170
LangG++
Submitted
Shared
RemoteRunId21228675

  

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char a[105][105]; int b[105][105]; int t[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct Node{ int x,y,mv; friend bool operator<(Node a,Node b) { return a.mv<b.mv; } }node;
int main() { int t1,n,m,mvp,f,i,j,k,l; priority_queue<Node> q; scanf("%d",&t1); while(t1--){ scanf("%d%d%d",&n,&m,&mvp); for(i=0;i<n;i++){ getchar(); scanf("%s",a[i]); } memset(b,0,sizeof(b)); for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(a[i][j]=='Y'){ b[i][j]=1; node.x=i; node.y=j; node.mv=mvp; q.push(node); while(q.size()){ for(k=0;k<4;k++){ int tx=q.top().x+t[k][0]; int ty=q.top().y+t[k][1]; if(tx<0||ty<0||tx>=n||ty>=m) continue; if(a[tx][ty]=='.'&&b[tx][ty]==0){ if(q.top().mv-1<0) continue; f=0; for(l=0;l<4;l++){ int ttx=tx+t[l][0]; int tty=ty+t[l][1]; if(ttx<0||tty<0||ttx>=n||tty>=m) continue; if(a[ttx][tty]=='E'){ b[tx][ty]=1; a[tx][ty]='*'; f=1; continue; } } if(f==1) continue; b[tx][ty]=1; a[tx][ty]='*'; node.x=tx; node.y=ty; node.mv=q.top().mv-1; q.push(node); } else if(a[tx][ty]=='T'&&b[tx][ty]==0){ if(q.top().mv-2<0) continue; f=0; for(l=0;l<4;l++){ int ttx=tx+t[l][0]; int tty=ty+t[l][1]; if(ttx<0||tty<0||ttx>=n||tty>=m) continue; if(a[ttx][tty]=='E'){ b[tx][ty]=1; a[tx][ty]='*'; f=1; continue; } } if(f==1) continue; b[tx][ty]=1; a[tx][ty]='*'; node.x=tx; node.y=ty; node.mv=q.top().mv-2; q.push(node); } else if(a[tx][ty]=='R'&&b[tx][ty]==0){ if(q.top().mv-3<0) continue; f=0; for(l=0;l<4;l++){ int ttx=tx+t[l][0]; int tty=ty+t[l][1]; if(ttx<0||tty<0||ttx>=n||tty>=m) continue; if(a[ttx][tty]=='E'){ b[tx][ty]=1; a[tx][ty]='*'; f=1; continue; } } if(f==1) continue; b[tx][ty]=1; a[tx][ty]='*'; node.x=tx; node.y=ty; node.mv=q.top().mv-3; q.push(node); } else if(a[tx][ty]=='#'&&b[tx][ty]==0){ b[tx][ty]=1; continue; } else if(a[tx][ty]=='E'&&b[tx][ty]==0){ b[tx][ty]=1; continue; } else if(a[tx][ty]=='P'&&b[tx][ty]==0){ if(q.top().mv-1<=0) continue; f=0; for(l=0;l<4;l++){ int ttx=tx+t[l][0]; int tty=ty+t[l][1]; if(ttx<0||tty<0||ttx>=n||tty>=m) continue; if(a[ttx][tty]=='E'){ b[tx][ty]=1; f=1; continue; } } if(f==1) continue; b[tx][ty]=1; node.x=tx; node.y=ty; node.mv=q.top().mv-1; q.push(node); } } q.pop(); } } } } for(i=0;i<n;i++){ printf("%s\n",a[i]); } printf("\n"); } return 0; }

 

转载于:https://www.cnblogs.com/yzm10/p/7216135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值