hdoj 1072 Nightmare 【bfs】

Nightmare

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8568    Accepted Submission(s): 4107

Problem Description

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.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.

There are five integers which indicate the different type of area in the labyrinth:

0: The area is a wall, Ignatius should not walk on it.

1: The area contains nothing, Ignatius can walk on it.

2: Ignatius‘ start position, Ignatius starts his escape from this position.

3: The exit of the labyrinth, Ignatius‘ target position.

4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.

Output

For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.

Sample Input

3
3 3
2 1 1
1 1 0
1 1 3
4 8
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
5 8
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1
1 1 4 1 1 1 1 1 

Sample Output

4
-1
13

分析:

0时墙 2是小明,1的地方可以走,3是出口。迷宫中有炸弹,将在6分钟后爆炸,小明需要在爆炸前逃出迷宫,其中每走一步,时间减1,遇到4可以重置炸弹时间为6,求出小明逃出迷宫的最小步数,逃不出来输出“-1”。

wrong answer 代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

int map[10][10];
int vis[10][10];
int n,m;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int ex,ey,sx,sy;
int sb;
int ans;
struct node{
	int x;
	int y;
	int step;
	int minute;
	friend bool operator<(node a,node b)
	{
		a.step>b.step;
	}
};

int  judge(int x,int y)
{
	if(x<0||x>=n)
	return 0;
	if(y<0||y>=m)
	return 0;
	if(map[x][y]==0)
	return 0;
	return 1;
}

void bfs(int c,int d)
{
    node a,b;
	priority_queue<node>q;
	a.x=c;
	a.y=d;
	a.minute=6;
	a.step=0;
	q.push(a);
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		if(a.minute==1)
			continue;
		if(a.x==sx&&a.y==sy)
		{
		printf("%d\n",a.step);
			return;
		}
//		vis[a.x][a.y]=1;
		for(int i=0;i<4;i++)
		{
			b.x=a.x+dir[i][0];
			b.y=a.y+dir[i][1];
		    if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&map[b.x][b.y]!=0)
		    {
		    	if(map[b.x][b.y]==4)
		    	{
		    		b.minute=6;
		    		map[b.x][b.y]=1;
		    	}
		    	else
		    	b.minute=a.minute-1;
		    	b.step=a.step+1;
		    	q.push(b);
		    }
		}

	}
	printf("-1\n");
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
//	while(scanf("%d%d",&n,&m)!=EOF)
		int i,j;
		ans=0;
		scanf("%d%d",&n,&m);
		memset(vis,0,sizeof(vis));
		sb=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				scanf("%d",&map[i][j]);
				if(map[i][j]==2)
				ex=i,ey=j;
				if(map[i][j]==3)
				sx=i,sy=j;
			}
		}
		bfs(ex,ey);
//		if(!sb)
//		printf("%d\n",ans);
//		else
//		printf("-1\n");
    }
	return 0;
}

正确代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int map[15][15];
int dir[4][2]={1,0,-1,0,0,-1,0,1};
int n,m;
int ex,ey,sx,sy;

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

void bfs()
{
	node a,b;
	priority_queue<node>q;
	a.x=ex;
	a.y=ey;
	a.time=6;
	a.step=0;
	q.push(a);
	while(!q.empty())
	{
		a=q.top();
		q.pop();
		if(a.x==sx&&a.y==sy)
		{
			printf("%d\n",a.step);
			return;
		}
		if(a.time==1)
		continue;
		for(int i=0;i<4;i++)
		{
			b.x=a.x+dir[i][0];
			b.y=a.y+dir[i][1];
			if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&map[b.x][b.y]!=0)
			{
				if(map[b.x][b.y]==4)
				{
					b.time=6;
					map[b.x][b.y]=1;
				}
				else
				b.time=a.time-1;
				b.step=a.step+1;
				q.push(b);
			}
		}
	}
	printf("-1\n");
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int i,j;
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				scanf("%d",&map[i][j]);
				if(map[i][j]==2)
				ex=i,ey=j;
				if(map[i][j]==3)
				sx=i,sy=j;
			}
		}
		bfs();
	}
	return 0;
}

版权声明:博主情人,外人误碰!!!

时间: 2024-08-27 19:54:07

hdoj 1072 Nightmare 【bfs】的相关文章

HDU1072 Nightmare 【BFS】

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7367    Accepted Submission(s): 3530 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ti

HDOJ 1495 非常可乐 【BFS】

非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5954    Accepted Submission(s): 2428 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要

Hdoj 1424 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18493    Accepted Submission(s): 6606 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

【BFS】uva10047The Monocycle

/* 本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同, 会处于不同的状态::::::::: 把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;; ------------------------------------------------------------------------ 在方向上我们把前,左,右编号为0,1,2:::: 颜色,从蓝色开始编号为0,1,2,3:::::::::: ------------------