题目链接:点击链接

三维的BFS,刚开始一直超内存,超无语......  改了n多次终于AC了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int map[50][50][50];
int d[6][3] = { {1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
int flag,x,y,z,num;
struct node
{
int x,y,z;
int time;
};
void bfs()
{
queue <node> q;
node s,temp;
s.x = 0;
s.y = 0;
s.z = 0;
s.time = 0;
q.push(s);
while(!q.empty())
{
temp = q.front();
q.pop();
if(temp.x == x - 1 && temp.y == y - 1 && temp.z == z - 1 && temp.time <= num)
{
printf("%d\n",temp.time);
flag = 1;
return ;
}
for(int i = 0 ; i < 6 ; i ++)
{
s = temp;
s.x += d[i][0];
s.y += d[i][1];
s.z += d[i][2];
if(s.x < 0 || s.x >= x || s.y < 0 || s.y >= y || s.z < 0 || s.z >= z || map[s.x][s.y][s.z] == 1)
continue;
s.time ++;
if(s.time >= num && temp.x != x - 1 && temp.y != y - 1 && temp.z != z - 1 ) continue;
map[s.x][s.y][s.z] = 1;
q.push(s);
}
}
}
int main()
{
int T,i,j,k;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&x,&y,&z,&num);
for(i = 0 ; i < x ; i ++)
for(j = 0 ; j < y ; j ++)
for(k = 0 ; k < z ; k ++)
scanf("%d",&map[i][j][k]);
flag = 0;
bfs();
if(!flag) printf("-1\n");
}
return 0;
}

最新文章

  1. Asp.Net Core 发布和部署(Linux + Jexus )
  2. e-chart 本地加载中国地图
  3. SharePoint 根据时间筛选
  4. -webkit-appearance、sselect
  5. Using HiveServer2 - Authentication
  6. ASP.NET MVC中错误日志信息记录
  7. 关于linux的systemd的一些事
  8. Windows定时器
  9. Spark Streaming揭秘 Day12 数据安全容错(Executor篇)
  10. 从头学Qt Quick系列
  11. 阿里巴巴Java开发手册评述
  12. 浅谈JS的继承
  13. 获取JSON格式的树形
  14. 27. Spring Boot 缓存注解详解: @Cacheable、@CachePut、 @CacheEvict、@Caching、@CacheConfig
  15. [JOISC2014]たのしい家庭菜園
  16. poj3264 倍增法(ST表)裸题
  17. opencv——对象计数
  18. WIP 004 - Quote/Policy Search
  19. python 打包文件
  20. 收藏 19 个 ES6常用的简写技巧

热门文章

  1. Monkey源码分析之事件注入
  2. Linux忘记rootpassword
  3. SSI框架总结
  4. codeforces #257 C称号Jzzhu and Chocolate
  5. SSMS2008插件开发(1)--介绍
  6. rem测试用实现移动端自适应页面
  7. 读书笔记—CLR via C#线程27章节
  8. Android项目---HtmlParse
  9. jsonp总结
  10. QMVC