HDU-1253 胜利大逃亡【三维广搜】

news/2024/7/10 6:12:12 标签: 优化, struct, c
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views">

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253

解题思路:

简单的三维广搜࿰c;把握好坐标和数组的关系。

用了输入外挂࿰c;然后加了几个小class="tags" href="/tags/YouHua.html" title=优化>优化:

1.曼哈顿距离class="tags" href="/tags/YouHua.html" title=优化>优化。

2.终点如果不能走࿰c;直接结束-1.


做了这题࿰c;对广搜又有了一点认识。

1.首先广搜是对每一个点只搜索一次࿰c;每次搜索一个点后就不再走。所以不需要像以前那样判断是否已经入队。

2.搜索时候要加上边界࿰c;这样就不用很多判断了。(一般都是用0做边界)

3.广搜的队列自己写比较快࿰c;用数组模拟一下就可以了。

4.广搜的层数用一个结构体来保存࿰c;每次入队时候࿰c;说明层数要在这个点的基础上+1层。


代码如下:

<code class="language-cpp">#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;

const int N = 55;
const int dir[6][3] = {{0, 0, 1}, {0, 0, -1}, {-1, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, -1, 0}};

class="tags" href="/tags/STRUCT.html" title=struct>struct node
{
	int x, y, z;
	int ceng;
}que[50000];

int maze[N][N][N];
int a, b, c, t;
int num;
int head, tail;

int Scan()
{
	int res = 0 , ch;
	while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
	{
		if( ch == EOF )  return 1 << 30 ;
	}
	res = ch - '0' ;
	while( ( ch = getchar() ) >= '0' && ch <= '9' )
		res = res * 10 + ( ch - '0' ) ;
	return res ;
}

int BFS()
{
	node cur;
	cur.x = 1, cur.y = 1, cur.z = 1, cur.ceng = 0;
	maze[cur.x][cur.y][cur.z] = 0;

	que[tail++] = cur;
	while(head < tail)
	{
		node temp = que[head++];

		if(temp.ceng > t) //无法到达
			return -1;
		if(temp.x == a && temp.y == b && temp.z == c && temp.ceng <= t)
			return temp.ceng;

		for(int i = 0; i < 6; ++i)
		{
			node now;
			now.x = temp.x + dir[i][0]; now.y = temp.y + dir[i][1]; now.z = temp.z + dir[i][2];
			if(maze[now.x][now.y][now.z])
			{
				now.ceng = temp.ceng + 1;
				que[tail++] = now;
				maze[now.x][now.y][now.z] = 0;
			}
		}
	}
	return -1;
}

int main()
{
	int ncase;
	ncase = Scan();
	while(ncase--)
	{
		num = head = tail = 0;
		memset(maze, 0, sizeof(maze));
		a = Scan(), b = Scan(), c = Scan(), t = Scan();
		for(int i = 1; i <= a; ++i)
			for(int j = 1; j <= b; ++j)
				for(int k = 1; k <= c; ++k)
				{
					maze[i][j][k] = Scan() ^ 1;
					num += maze[i][j][k];
				}

		if(maze[a][b][c] == 0 || num < a + b + c - 3) //终点无法走
		{
			printf("-1\n");
			continue;
		}
		printf("%d\n", BFS());
	}
	return 0;
}code>


cle>

http://www.niftyadmin.cn/n/1516500.html

相关文章

Integrating your project in the PUBLIC tree

IntroductionHave you ever wondered how you can integrate your code under the PUBLIC tree in Windows Embedded CE 6.0? This can be useful if you want to distribute code/components that are not part of a BSP. In this post I’ll explain how to create a folder…

NYOJ-524 A-B Problem【高精度】

题目链接&#xff1a;http://acm.nyist.net/JudgeOnline/problem.php?pid524 解题思路&#xff1a; JAVA果断水过&#xff0c;看别人用C写了100行&#xff0c;而且稠的很。。。 我的一共才20行&#xff0c;还有头文件什么的。。。 不得不说&#xff0c;JAVA高精度无敌啊。…

NYOJ-525 一道水题【模拟】

题目链接&#xff1a;http://acm.nyist.net/JudgeOnline/problem.php?pid525 解题思路&#xff1a; 用到了字符串截取函数strtok 本来想用sscanf的正则表达式的&#xff0c;但是不会写。。。百度了一下&#xff0c;知道了大概&#xff0c;貌似不能对数字用。估计能把&#…

Windows CE Newsgroups

http://geekswithblogs.net/BruceEitman/archive/2008/06/24/windows-ce-newsgroups.aspx

Nginx/ZooKeeper 负载均衡的差异

Nginx是著名的反向代理服务器&#xff0c;也被广泛的作为负载均衡服务器 ZooKeeper是分布式协调服务框架&#xff0c;有时也被用来做负载均衡 那么他们的区别是什么&#xff1f;如何选择呢&#xff1f; 下面从实际场景看下他们的关系 Nginx的负载均衡配置非常简单&#xff0…

NYOJ-528 找球号(三)【位运算】

题目链接&#xff1a;http://acm.nyist.net/JudgeOnline/problem.php?pid528 解题思路&#xff1a; 给你2个数&#xff0c;怎么判断它是否出现偶数次&#xff1f; 对1个数异或另一个数2次等于本身。 代码如下&#xff1a; #include<iostream> #include<cstring>…

POJ-1470 Closest Common Ancestors【LCA】

题目链接:http://poj.org/problem?id1470 解题思路: 第一次用RMQLCA搞了2天。http://blog.csdn.net/niushuai666/article/details/7424177 早上学了一下Targan离线求LCA。 代码如下&#xff1a; #include<iostream> #include<cstring> #include<vector> …

超强、超详细Redis数据库入门教程

【本教程目录】 1.redis是什么 2.redis的作者何许人也 3.谁在使用redis 4.学会安装redis 5.学会启动redis 6.使用redis客户端 7.redis数据结构 – 简介 8.redis数据结构 – strings 9.redis数据结构 – lists 10.redis数据结构 – 集合 11.redis数据结构 – 有序集合 12.redis…