天天看点

HDU 3345 War Chess

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.

HDU 3345 War Chess

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<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define make(x,y) make_pair(x,y)
#define pii pair<int,int>
#define fi first
#define se second
#define rep(i,j,k) for (int i=j;i<=k;i++)
const int N=105;
int mp[N][N],f[N][N];
char s[N][N];
int T,n,m,t,x,y;
int a[4]={0,0,1,-1};
int b[4]={1,-1,0,0};

bool check(int x,int y)
{
	rep(i,0,3)
	{
		if (mp[x+a[i]][y+b[i]]==-1) return false;
	}
	return true;
}

int main()
{
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d%d",&n,&m,&t);
		memset(mp,0,sizeof(mp));
		memset(f,-1,sizeof(f));
		rep(i,1,n)
		{
			scanf("%s",s[i]+1);
			rep(j,1,m)
			{
				if (s[i][j]=='Y') x=i,y=j,mp[i][j]=1;
				if (s[i][j]=='E') mp[i][j]=-1;
				if (s[i][j]=='.') mp[i][j]=1;
				if (s[i][j]=='T') mp[i][j]=2;
				if (s[i][j]=='R') mp[i][j]=3;
				if (s[i][j]=='#') mp[i][j]=0;
				if (s[i][j]=='P') mp[i][j]=1;
			}
		}
		queue<pii> p; p.push(make(x,y)); f[x][y]=0;
		while (!p.empty())
		{
			pii q=p.front(); p.pop();
			if (!check(q.fi,q.se)&&q!=make(x,y)) continue;
			rep(i,0,3)
			{
				int X=q.fi+a[i],Y=q.se+b[i];
				if (mp[X][Y]<=0) continue;
				if (f[X][Y]==-1||f[X][Y]>f[q.fi][q.se]+mp[X][Y])
				{
					f[X][Y]=f[q.fi][q.se]+mp[X][Y];
					p.push(make(X,Y));
				}
			}
		}
		rep(i,1,n)
		{
			rep(j,1,m)
			{
				if (f[i][j]>0&&f[i][j]<=t&&mp[i][j]>0&&s[i][j]!='P') printf("*");
				else printf("%c",s[i][j]);
			}
			putchar(10);
		}
		putchar(10);
	}
	return 0;
}