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

被折叠的 条评论
为什么被折叠?



