acm1072题

**

问题描述:Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes. Given the layout of the labyrinth and Ignatius’ start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1. Here are some rules: 1. We can assume the labyrinth is a 2 array. 2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too. 3. If Ignatius get to the exit when the exploding time turns to 0, he can’t get out of the labyrinth. 4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can’t use the equipment to reset the bomb. 5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish. 6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.

**

代码:
#include<iostream>
#include<queue>
using namespace std;
int map[10][10];//迷宫 
int m,n;//迷宫的大小 
int sx,sy,ex,ey;//分别为起点和终点的横纵坐标 
int dir[4][2]{{1,0},{0,1},{-1,0},{0,-1}}; 
struct node{
	int x,y;
	int step;
	int time;
};
bool judge(node nod){//如果超出了界面那就返回false 
	if(nod.x<0||nod.x>=m||nod.y<0||nod.y>=n)
	return false;
	if(map[nod.x][nod.y]==0)
	return false;
	return true;
}
void bfs(){
	node cur,next;
	cur.x=sx;
	cur.y=sy;
	cur.time=6;
	cur.step=0;
	queue<node> Q;
	Q.push(cur);
	while(!Q.empty()){
		cur=Q.front();
		Q.pop();
		if(cur.x==ex&&cur.y==ey){
			cout<<cur.step<<endl;
			return;
		}
		for(int i=0;i<4;i++){
			next.x=cur.x+dir[i][0];
			next.y=cur.y+dir[i][1];
			next.time=cur.time-1;
			if(judge(next)&&next.time>0){//如果next到达了终点但是time为0也属于无效 
				if(map[next.x][next.y]==4){
					next.step=cur.step+1;
					next.time=6;
					map[next.x][next.y]=0;
				}else{
					next.step=cur.step+1;
				}
				Q.push(next);
			}
		} 
		
	}
	cout<<"-1"<<endl;
}
int main(){
	int k;
	cin>>k;
	while(k--){
		cin>>m>>n;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				cin>>map[i][j];
			}
		}
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				if(map[i][j]==2){
					sx=i;
					sy=j;
				}
					if(map[i][j]==3){
					ex=i;
					ey=j;
				}
			}
		}
		bfs();
	}
	return 0;
}
``

> 分析:一般解决迷宫的最短问题使用bfs知识,所以会涉及到队列的知识。
> 这个问题教于一般问题只不过是逻辑稍微了复杂一点。当到达数字为4的时候,要给时间重新设置。同时又因为可以会走到重复的路线,所以一个点可能会经过几次,所以不需要设置该点已经访问。最后一个关键点在于如果到达了终点但是时间为页也属于失败,所以需要进行一个判断。

发布了27 篇原创文章 · 获赞 17 · 访问量 175

猜你喜欢

转载自blog.csdn.net/weixin_42918559/article/details/103979727