hdu 3345 War chess(bfs)

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 NM 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
…EPY
…E
*
…T

.E
EY


.P.
PYP
.P.

.E.
EYE
.*.

题意:Y为起始点,MV为移动能量,经过同伴和空地消耗一点能量,经过森林消耗两点,小河三点,如果旁边四格有敌人,能量变为0,而且不能停留在同伴所在的格子,最后输出时将所有能停留的格子变为*

数据规模达到了200*200 反正是100以上的肯定就是广搜了,深搜wa了好几发,其次要用优先队列,毕竟规模大,如果用最小的步数,必须用优先队列,对于有多个终点,终点不确定的,要用优先队列。!!!!重点。
还有一个坑点是,思维上一个漏洞,搜索是走到这个位置在判断这个位置可不可行,我wa了很多的原因在于我每次都先判走的这个地方旁边有没有E,这是错的,必须先判树呀小河呀这些我有没有足够的点走到这个位置,如果走得到在判断附近有没有e,最后再加进去。搜索的套路最后在最后加进去,而且平时我都是边加边判断是否到了终点,还有个方法实在每次派出的时候判断,有时会更方便。当然有时我那种规模会少很多。

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define f(i,a,b) for(int i = a; i < b; i++)
typedef pair<int,int> P;
#define ll long long
int n, m, mv;
string mp[110];
int vis[110][110];
int sx,sy;

struct node{
	int x, y, step;
	bool operator < (const node &rhs) const{
		return step < rhs.step;
	}
};

priority_queue<node> Q;
int nxt[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};

int check1(int x, int y)
{
	if(x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || mp[x][y] == '#') return 1;
	return 0;
}

int check2(int x, int y)
{
	ff(i,0,3)
	{
		int xx = x + nxt[i][0];
		int yy = y + nxt[i][1];
		if(check1(xx,yy)) continue;
		if(mp[xx][yy] == 'E') return 1;
	}
	return 0;
}

void bfs(int x, int y)
{
	memset(vis,0,sizeof(vis));
	while(!Q.empty()) Q.pop();
	Q.push((node){x,y,mv});
	vis[x][y] = 1;
	while(!Q.empty())
	{
		node top = Q.top();
		Q.pop();
		if(top.step == 0) continue;
		ff(i,0,3)
		{
			int xx = top.x + nxt[i][0];
			int yy = top.y + nxt[i][1];
			if(check1(xx,yy)) continue;
			if(mp[xx][yy] == 'T' && top.step > 1)
			{
				if(check2(xx,yy))
					Q.push((node){xx,yy,0});
				else Q.push((node){xx,yy,top.step - 2});
				mp[xx][yy] = '*';
				vis[xx][yy] = 1;
			}
			if(mp[xx][yy] == 'R' && top.step > 2)
			{
				if(check2(xx,yy)) Q.push((node){xx,yy,0});
				else Q.push((node){xx,yy,top.step - 3});
				mp[xx][yy] = '*';
				vis[xx][yy] = 1;
			}
			if((mp[xx][yy] == '.' || mp[xx][yy] == 'P')&&top.step > 0)
			{
				if(check2(xx,yy)) Q.push((node){xx,yy,0});
				else Q.push((node){xx,yy,top.step - 1});
				if(mp[xx][yy] == '.') mp[xx][yy] = '*';
				vis[xx][yy] = 1;
			}
		}
	}
}


int main()
{
    int t;
    cin >> t;
    while(t--){
    	cin >> n >> m >> mv;
    	f(i,0,n)
    	{ 
    		cin >> mp[i];
    		f(j,0,m) 
    			if(mp[i][j] == 'Y') sx = i, sy = j;
    	}
    	bfs(sx,sy);
    	f(i,0,n) cout << mp[i] << endl;
    	puts("");
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值