首页 > 其他分享 >hdu1240 Asteroids!--DFS & BFS

hdu1240 Asteroids!--DFS & BFS

时间:2022-12-06 19:38:05浏览次数:84  
标签:pp tz tx ty -- Asteroids BFS int include


原题链接: ​​ http://acm.hdu.edu.cn/showproblem.php?pid=1240​


一:题意

三维空间,中o表示可以走,x表示不能走,给出行走的起始点和目的点的坐标,问最少多少步可以从起点到达目的点。


二:AC代码

DFS:

#define _CRT_SECURE_NO_DEPRECATE 
#define _CRT_SECURE_Cy1_OVERLOAD_STANDARD_NAMES 1

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;

int dirx[6] = { 1,-1,0,0,0,0 };
int diry[6] = { 0,0,1,-1,0,0 };
int dirz[6] = { 0,0,0,0,1,-1 };

char ch[11][11][11];
int dis[11][11][11];
int sx, sy, sz;
int dx, dy, dz;
int minn;
int n;

void dfs(int x, int y, int z, int cnt)
{
if (x < 0 || y < 0 || z < 0 || x >= n || y >= n || z >= n)
return;
if (ch[z][x][y] == 'X')
return;
if (x == dx&&y == dy&&z == dz)
{
if (cnt < minn)
minn = cnt;
return;
}
if (cnt > dis[z][x][y])
return;
else
dis[z][x][y] = cnt;

int tx, ty, tz;
for (int i = 0; i < 6; i++)
{
tx = x + dirx[i];
ty = y + diry[i];
tz = z + dirz[i];
dfs(tx, ty, tz, cnt + 1);
}
}

int main()
{
char str[10];
int i, j, k;

while (~scanf("%s %d%*c", str, &n))
{
//getchar();
for (k = 0; k < n; ++k)
{
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
ch[k][i][j] = getchar();
dis[k][i][j] = 99999999;
}
getchar();
}
}

scanf("%d %d %d", &sy, &sx, &sz);
scanf("%d %d %d", &dy, &dx, &dz);
scanf("%s", str);
minn = 99999999;
dfs(sx, sy, sz, 0);

if (minn == 99999999)
printf("NO ROUTE\n");
else
printf("%d %d\n", n, minn);
}

return 0;
}


BFS:

#define _CRT_SECURE_NO_DEPRECATE 
#define _CRT_SECURE_Cy1_OVERLOAD_STANDARD_NAMES 1

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <climits>

using namespace std;

const int MAX = 11;
const int INF = INT_MAX - 3;
const int dirx[6] = { 1,-1,0,0,0,0 }, diry[6] = { 0,0,1,-1,0,0 }, dirz[6] = { 0,0,0,0,1,-1 };

typedef struct Point {
int x, y, z;
}Point;

char map[MAX][MAX][MAX];
int dist[MAX][MAX][MAX];
int sx, sy, sz, dx, dy, dz;
int minx, n;

Point pp;

queue<Point> que;

int bfs(int x, int y, int z)
{
pp.x = x;
pp.y = y;
pp.z = z;
que.push(pp);
int i, tx, ty, tz;
while (!que.empty())
{
pp = que.front();
x = pp.x;
y = pp.y;
z = pp.z;
que.pop();
if (x == dx && y == dy && z == dz)
break;
for (i = 0; i < 6; ++i)
{
tx = x + dirx[i];
ty = y + diry[i];
tz = z + dirz[i];

if (tx < 0 || ty < 0 || tz < 0 || tx >= n || ty >= n || tz >= n)continue;
if (map[tz][tx][ty] == 'X')continue;
if (dist[z][x][y] + 1 > dist[tz][tx][ty])continue;

dist[tz][tx][ty] = dist[z][x][y] + 1;
pp.x = tx;
pp.y = ty;
pp.z = tz;
que.push(pp);
}
}
return dist[dz][dx][dy];
}

int main()
{
char str[10];
int i, j, k;
while (scanf("%s %d%*c", str, &n) != EOF)
{
for (k = 0; k < n; ++k)
{
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
{
map[k][i][j] = getchar();
dist[k][i][j] = 99999999;
}
getchar();
}
}

scanf("%d %d %d", &sy, &sx, &sz);
scanf("%d %d %d", &dy, &dx, &dz);
scanf("%s", str);
dist[sz][sx][sy] = 0;
minx = bfs(sx, sy, sz);

if (minx == 99999999)
printf("NO ROUTE\n");
else
printf("%d %d\n", n, minx);

while (!que.empty())
que.pop();
}
return 0;
}





标签:pp,tz,tx,ty,--,Asteroids,BFS,int,include
From: https://blog.51cto.com/u_11937443/5916544

相关文章

  • hdu1180 诡异的楼梯--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1180​​需要注意这句话:Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.#define_CRT_SECURE_NO_DEPRECATE#def......
  • hdu1242 Rescue--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1242​​一:题意x代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙路花费一秒,x花费两秒问到达终点的最少时间思路:B......
  • 使用Spring Reactor优化推荐流程
    1.背景公司有一个推荐系统Rec,这个系统的主要功能是:向外部系统提供推荐接口根据请求获取推荐策略根据推荐策略完成推荐的召回、过滤、打分、排序阶段Rec作为微服务......
  • hdu1195 Open the Lock--单向BFS & 双向BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1195​​一:题意两个四位数的数字,经过一下三种方式变换,求出变成另一个数字的最小时间。加1;减1;相邻交换其中9+1变......
  • hdu1175 连连看 --DFS/BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1175​​直接上代码,不是很难。#define_CRT_SECURE_NO_DEPRECATE#define_CRT_SECURE_Cy1_OVERLOAD_STANDARD_N......
  • UE4学习笔记23——【动画】Mixamo自动绑骨并导入虚幻4
    P61.Mixamo自动绑骨并导入虚幻4P61需要插件“MixamoAnimationRetargeting”(200多块......)(这节课就简单听听,以后用到了再看)(桥豆麻袋!不用买这个插件,这节课的东西也能......
  • hdu1026 Ignatius and the Princess I --BFS & 记录路径 & 与DFS的比较
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1026​​一:题意一个n*m的矩阵代表一个迷宫,(0,0)是起点,(n-1)(m-1)是终点,每移动一步一秒。迷宫每点意义是:. 该点可以......
  • Angular 表单
    表单中的重要概念1:FormControl:表单控件,封装了表单中的输入,并提供了一些可供操纵的对象2:Validator:验证器,对表单的输入根据自己的需要添加一些限制3:Observer:观察者,......
  • 5.3.1 (2) 函数的单调性(含参函数)
    \({\color{Red}{欢迎到学科网下载资料学习}}\)[【基础过关系列】高二数学同步精品讲义与分层练习(人教A版2019)](https://www.zxxk.com/docpack/2875423.html)\({\col......
  • hdu1010 Tempter of the Bone --DFS & 奇偶剪枝
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1010​​一:题意一个n*m的迷宫,给定一个出发点,一个结束点,一条小狗需要在恰好第k秒走到结束点,如果可以输出YES,不然输......