HDU3345:War Chess(BFS+优先队列)

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


挺有意思的一道题,没什么好说的,上代码吧

#include<stdio.h>
#include<queue>
#include<string>
using namespace std;

char map[105][105];
int vis[105][105];
int n,m,mv,sx,sy;
int dir[4][2]={1,0,-1,0,0,1,0,-1};

struct node
{
	int x,y,mv;
	friend bool operator <(node a,node b)
	{
		return a.mv<b.mv;
	}
};

int check(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m)
		return 1;
	return 0;
}

int judge(int x,int y)
{  
    if(!check(x-1,y) && map[x-1][y] == 'E')  
        return 1;  
    if(!check(x+1,y) && map[x+1][y] == 'E')  
        return 1;  
    if(!check(x,y-1) && map[x][y-1] == 'E')  
        return 1;  
    if(!check(x,y+1) && map[x][y+1] == 'E')  
        return 1;  
    return 0;  
}  

void bfs()
{
	priority_queue<node>Q;
	node a,nex;
	memset(vis,0,sizeof(vis));
	a.x=sx;
	a.y=sy;
	a.mv=mv;
	vis[sx][sy]=1;
	Q.push(a);
	while(!Q.empty())
	{
		a=Q.top();
		Q.pop();
		if(a.mv<=0)
			continue;
		for(int i=0;i<4;++i)
		{
			nex=a;
			nex.x+=dir[i][0];
			nex.y+=dir[i][1];
			if(check(nex.x,nex.y)||vis[nex.x][nex.y]||map[nex.x][nex.y]=='#'||map[nex.x][nex.y]=='E')
				continue;
			if(map[nex.x][nex.y]=='T')   nex.mv-=2;
			else if(map[nex.x][nex.y]=='.'||map[nex.x][nex.y]=='P')   nex.mv--;
			else if(map[nex.x][nex.y]=='R')   nex.mv-=3;
			
			if(nex.mv<0)
                continue;
			if(judge(nex.x,nex.y))
                nex.mv = 0;  
			if(map[nex.x][nex.y]!='P')
				map[nex.x][nex.y]='*';
			vis[nex.x][nex.y]=1;
			Q.push(nex);
		}
	}
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&n,&m,&mv);
		for(int i=0;i<n;++i)
		{
			scanf("%s",map[i]);
			for(int j=0;map[i][j];++j)
			{
				if(map[i][j]=='Y')
				{
					sx=i;
					sy=j;
				}
			}
		}
		
		bfs();
		for(int i=0;i<n;++i)
			printf("%s\n",map[i]);
		puts("");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值