Tempter of the Bone II HDU - 2128

Tempter of the Bone II

题目链接:HDU - 2128
题意:在迷宫中由起点走到终点, 可以用炸弹炸墙;
结构体中多加一个mp存每个状态下的地图(原图不能改 );
还有就是vis加一个最高值, 就是说每个状态可能走多次, 搜的题解是20, 我试了一下不同的值发现最小要大于12;但没弄能这个值是怎么计算出的;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
int N, M, s_x, s_y, e_x, e_y;
char G[10][10];
int vis[10][10][600];
struct node{
	int x, y, ex, step;
	char mp[10][10];
	bool operator < (const node &a) const{
		return step>a.step;
	}
};
int dir[4][2]={1, 0, 0, 1, -1, 0, 0, -1};
priority_queue<node> que;
void bfs(){
	while(!que.empty()) que.pop();
	node temp;
	temp.x=s_x, temp.y=s_y, temp.ex=0, temp.step=0, temp.step=0;
	for(int i=0; i<N; i++){
		for(int j=0; j<M; j++){
			temp.mp[i][j]=G[i][j];
		}
	}
	que.push(temp);
	vis[s_x][s_y][0]++;
	while(!que.empty()){
		temp=que.top();
		if(temp.x==e_x&&temp.y==e_y){
			printf("%d\n", temp.step);
			return;
		}
		for(int i=0; i<4; i++){
			temp.step++;
			temp.x+=dir[i][0];
			temp.y+=dir[i][1];
			if(temp.x<N&&temp.x>=0&&temp.y<M&&temp.y>=0&&vis[temp.x][temp.y][temp.ex]<=20){//这里我试了试最小可以改成13;但不知原因
				if(temp.mp[temp.x][temp.y]=='X'&&temp.ex>0){
					vis[temp.x][temp.y][temp.ex]++;
					temp.ex--;
					temp.mp[temp.x][temp.y]='.';
					vis[temp.x][temp.y][temp.ex]++;
					temp.step++;
					que.push(temp);
				}
				else if(temp.mp[temp.x][temp.y]=='.'){
					vis[temp.x][temp.y][temp.ex]++;
					que.push(temp);
				}
				else if(temp.mp[temp.x][temp.y]<='9'&&temp.mp[temp.x][temp.y]>='0'){
					vis[temp.x][temp.y][temp.ex]++;
					temp.ex+=temp.mp[temp.x][temp.y]-'0';
					temp.mp[temp.x][temp.y]='.';
					vis[temp.x][temp.y][temp.ex]++;
					que.push(temp);
				}
			}
			temp=que.top();
		}
		que.pop();
	}
	printf("-1\n");
	return;
}
int main(){
	while(scanf("%d%d", &N, &M), N&&M){
		for(int i=0; i<N; i++){
			scanf("%s", G[i]);
			for(int j=0; j<M; j++){
				if(G[i][j]=='S') s_x=i, s_y=j, G[i][j]='.';
				if(G[i][j]=='D') e_x=i, e_y=j, G[i][j]='.';
			}
		}
		memset(vis, 0, sizeof(vis));
		bfs();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值