HDU-2128

这题写的比较暴力,因为地图大小只有8*8,所以在队列的节点里保存了两张地图,一张用来保存地图的情况,另一张保存在该节点时手上炸弹数量的最大值。如果走到该节点时手上的炸弹数量小于或等于最大值则不再往下走,注意初始化时最大值全部设为-1。


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

struct node{
	int x,y,t,b;
	char map[8][8];
	int flag[8][8];
	bool operator < (const node &a) const{
		return t>a.t;
	}
};

int n,m;
int value[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
char Map[10][10];

int BFS(int sy,int sx){
	priority_queue<node> q;
	int j,l;
	node temp1,temp2;
	temp1.x = sx;
	temp1.y = sy;
	temp1.t = 0;
	temp1.b = 0;
	for(l=0;l<n;l++){
		for(j=0;j<m;j++){
			temp1.map[l][j] = Map[l][j];
			temp1.flag[l][j] = -1;
		}
	}
	temp1.flag[sy][sx] = 0;
	q.push(temp1);
	while(!q.empty()){
		temp1 = q.top();
		q.pop();
		for(int i=0;i<4;i++){
			temp2.x = temp1.x + value[i][0];
			temp2.y = temp1.y + value[i][1];
			temp2.t = temp1.t+1;
			temp2.b = temp1.b;
			for(l=0;l<n;l++){
				for(j=0;j<m;j++){
					temp2.map[l][j] = temp1.map[l][j];
					temp2.flag[l][j] = temp1.flag[l][j];
				}
			}
			if(temp2.x>=0 && temp2.x<m && temp2.y>=0 && temp2.y<n){
				if(temp2.map[temp2.y][temp2.x] == 'D') return temp2.t;
				if(temp2.map[temp2.y][temp2.x] == 'X' && temp2.b != 0){
					temp2.b--;
					temp2.map[temp2.y][temp2.x] = '.';
					temp2.t++;
				}
				if(temp2.map[temp2.y][temp2.x] == 'X') continue;
				if(temp2.map[temp2.y][temp2.x]>='1' && temp2.map[temp2.y][temp2.x]<='9'){
					temp2.b += temp2.map[temp2.y][temp2.x]-'0';
					temp2.map[temp2.y][temp2.x] = '.';
				}
				if(temp2.flag[temp2.y][temp2.x]<temp2.b){
					temp2.flag[temp2.y][temp2.x] = temp2.b;
					q.push(temp2);
				}
			}
		}
	}
	return -1;
}

int main(){
	int i,j;
	while(cin>>n>>m){
		if(n == 0 && m == 0) break;
		for(i=0;i<n;i++) cin>>Map[i];
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				if(Map[i][j] == 'S'){
					cout<<BFS(i,j)<<endl;
					break;
				}
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值