NYOJ 483 Nightmare 【广搜】+【无标记】

Nightmare

时间限制:1000 ms  |  内存限制:65535 KB

难度:4

描述

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.

输入
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.

输出
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.

样例输入
2
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
样例输出
4
-1
896378 长木 Nightmare Accepted 4 308 C/C++ 06-15 21:03:39

896368
长木 Nightmare WrongAnswer -- -- C/C++ 06-15 20:54:57

896363

长木 Nightmare WrongAnswer -- -- C/C++ 06-15 20:49:37

896360

长木 Nightmare WrongAnswer -- -- C/C++ 06-15 20:46:04

896339

长木 Nightmare WrongAnswer -- -- C/C++ 06-15 20:14:06

896333

长木 Nightmare WrongAnswer -- -- C/C++ 06-15 20:02:33

896306

长木 Nightmare WrongAnswer -- -- C/C++ 06-15 19:06:14

说多泪啊(┬_┬),之前几次WA都是数组模拟队列时结果错用成了栈。。

AC:

#include <stdio.h>
#include <queue>
using std::queue;
int map[10][10], t, m, n, id;
int mov[][2] = {1, 0, -1, 0, 0, 1, 0, -1};
struct Node{
	int x, y, steps, time;
} start;
queue<Node> Q;

int check(int i, int j){
	if(i < 0 || j < 0 || i >= m || j >= n)
		return 0;
	return map[i][j];
}

int BFS(){
	Node now, temp;
	while(!Q.empty()){
		now = Q.front();
		Q.pop();
		if(now.time == 1) continue; //cut
		for(int i = 0; i < 4; ++i){
			temp = now;
			temp.x += mov[i][0];
			temp.y += mov[i][1];
			--temp.time; ++temp.steps;
			if(check(temp.x, temp.y)){
				if(map[temp.x][temp.y] == 3) return temp.steps;
				else if(map[temp.x][temp.y] == 4){ //防止无限次充电
					map[temp.x][temp.y] = 1;
					temp.time = 6;
				}
				Q.push(temp);
			}
		}
	}
	return -1;
}

int main(){
	scanf("%d", &t);
	while(t--){
		scanf("%d%d", &m, &n);
		start.steps = 0;
		start.time = 6;
		for(int i = 0; i < m; ++i)
			for(int j = 0; j < n; ++j){
				scanf("%d", &map[i][j]);
				if(map[i][j] == 2){
					start.x = i; start.y = j;
				}
			}
		while(!Q.empty()) Q.pop();
		Q.push(start);
		printf("%d\n", BFS());
	}
	return 0;
}        

WA:

#include <stdio.h>
int map[10][10], t, m, n, id;
int mov[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
struct Node{
	int x, y, steps, time;
} Q[70], start;

int check(int i, int j){
	if(i < 0 || j < 0 || i >= m || j >= n)
		return 0;
	return map[i][j];
}

int BFS(){
	Node now, temp;
	while(id){
		now = Q[--id]; //这哪是出队啊,⊙﹏⊙b汗
		if(map[now.x][now.y] == 3) return now.steps;
		if(now.time == 1) continue; //cut
		for(int i = 0; i < 4; ++i){
			temp = now;
			temp.x += mov[i][0];
			temp.y += mov[i][1];
			--temp.time; ++temp.steps;
			if(check(temp.x, temp.y)){
				if(map[temp.x][temp.y] == 4){ //防止无限次充电
					map[temp.x][temp.y] = 1; //不能赋值为0,否则可能无限循环
					temp.time = 6;
				}
				Q[id++] = temp;
			}
		}
	}
	return -1;
}

int main(){
	scanf("%d", &t);
	while(t--){
		scanf("%d%d", &m, &n);
		start.steps = id = 0;
		start.time = 6;
		for(int i = 0; i < m; ++i)
			for(int j = 0; j < n; ++j){
				scanf("%d", &map[i][j]);
				if(map[i][j] == 2){
					start.x = i; start.y = j;
				}
			}
		Q[id++] = start;
		printf("%d\n", BFS());
	}
	return 0;
}

NYOJ 483 Nightmare 【广搜】+【无标记】

时间: 2025-01-07 22:24:28

NYOJ 483 Nightmare 【广搜】+【无标记】的相关文章

nyoj 523 双向广搜

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstdio> #include<queue> using namespace std; /* 用普通搜索TLE,已知起点和终点,可以考虑双向广搜或A*算法加速搜索 双向广搜,一个方向从出发点向终点搜索,一个方向从终点向出发点搜索,搜索到相同的结点时,即找到最短路径. */ const int N

nyoj 483 Nightmare【bfs+优先队列】

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 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 e

HDU 3085 Nightmare Ⅱ (双向广搜)

题意:有M,G两人和鬼魂(Z)在n*m的方格内,M每秒走3步,G每秒走一步,鬼魂每秒走2步,问是否能 不遇到鬼魂下两人相遇,鬼魂可以穿墙(X),人不可以.初始鬼魂有2个. #include<stdio.h> #include<string.h> #include<string> #include<queue> #include<map> #include<iostream> #include<algorithm> #def

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 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 consists of empty space

深搜 ,广搜,队列 nyoj 27 水池数目

水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中共有几个水池. 输入 第一行输入一个整数N,表示共有N组测试数据 每一组数据都是先输入该地图的行数m(0<m<100)与列数n(0<n<100),然后,输入接下来的m行每行输入n个数,表示此处有水还是没水(1表示此处是水

nyoj 999——师傅又被妖怪抓走了——————【双广搜】

师傅又被妖怪抓走了 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟空便为师傅去化斋,等悟空回来,悟净慌慌张张的对悟空说:“不好了,不好了”,还没等悟净说完,悟空说:“师傅又被妖怪抓走了”,悟净:“NO!” ,悟空一脸茫然,悟净:“师傅和二师兄都被妖怪抓走了”.悟空(晕!).为了防止悟空救人,妖怪先把唐憎和八戒分别藏起来,如果悟空在T分钟之后还没找到人,那必定是被妖怪吃

nyoj 592 spiral grid(广搜)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=592 解决以下问题后就方便用广搜解: 1.将数字坐标化,10000坐标为(0,0),这样就可以通过数字获得其坐标 2.通过一个坐标知道在这个位置上的数字是否为素数 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<queue> us

Sicily 1151 解题报告(魔板,广搜)

      I.     原题中文大意 魔板由2*4个方块组成,用1到8的数字表示不同的块. 其初始状态是 1 2 3 4 8 7 6 5 对魔板可进行三种基本操作,这三种基本操作,可将任一种状态装换成另一种状态. A (上下行互换) B (行循环右移一格) C (中间四块顺时针转90) 8 7 6 5 1 2 3 4 4 1 2 3 5 8 7 6 1 7 2 4 8 6 3 5 II.       算法思想及解题用到的主要数据结构 广度优先搜索,已搜索过的节点不再进行记录,保证第一个找到的解

迷宫问题(深搜 广搜)

题目描述: 给出一个m*n的迷宫图和一个入口,一个出口. 编一个程序,打印一条从迷宫入口到出口的路径. -1表示走不通,0表示能走,只能上下左右走: 无路可走输出"no way": 样例输入: 8 5-1 -1 -1 -1 -1 0 0 0 0 -1-1 -1 -1 0 -1-1 0 0 0 -1-1 0 0 1 -1-1 0 0 0 -1-1 -1 -1 0 -1-1 0 0 0 -12 18 4 8 5-1 -1 -1 -1 -10 0 0 0 -1-1 -1 -1 0 -1-1