[14] 胜利大逃亡
题意:如上图,就是以左上方的点看作坐标原点,构造坐标系,按正常看的x轴正方向,z轴负方向,y轴正方向依次填入,0为路,1为墙,右下角为出口。
算法:BFS
问题:一开始因为之前习惯把t当成组数,但本题中t还代表魔王回来的时间,搞混了,wa了两发才发现。
思路:把基本的二维的BFS扩展成三维即可,可以先特判一下出口不能走的情况,减少运算。
代码:ac
#include<bits/stdc++.h>
using namespace std;
int Map[60][60][60];
int vis[60][60][60];
int a, b, c, t;
int dir[6][3] = { {0,0,1},{0,0,-1}, {0,1,0}, {0,-1,0}, {1,0,0}, {-1,0,0} };
struct pos {int x, y, z;int time;
};
int q;
void bfs();
int main()
{scanf("%d", &q);for (int p = 1; p <= q; p++) {scanf("%d%d%d%d", &a, &b, &c, &t);for(int i=1;i<=a;i++)for(int j=1;j<=b;j++)for (int k = 1; k <= c; k++) {scanf("%d", &Map[i][j][k]);vis[i][j][k] = 0;}bfs();}return 0;
}
void bfs()
{if (Map[a][b][c] == 1) {/*特判出口不能走的情况*/printf("-1\n");return;}queue<pos>qu;pos cur, nex;cur.time = 0;cur.x = 1, cur.y = 1, cur.z = 1;qu.push(cur);vis[1][1][1] = 1;while (!qu.empty()) {cur = qu.front();qu.pop();if (cur.x == a && cur.y == b && cur.z == c) {/*到达*/printf("%d\n", cur.time);return;}if (cur.time >= t) {/*超时,如果恰好到前一个if已经return了,所以这里取到等号*/printf("-1\n");return;}for (int i = 0; i < 6; i++) {nex.time = cur.time + 1;nex.x = cur.x + dir[i][0];nex.y = cur.y + dir[i][1];nex.z = cur.z + dir[i][2];if (Map[nex.x][nex.y][nex.z] == 1)continue;if (nex.x<1 || nex.x>a || nex.y<1 || nex.y>b || nex.z<1 || nex.z>c)continue;if (vis[nex.x][nex.y][nex.z])continue;vis[nex.x][nex.y][nex.z] = 1;qu.push(nex);}}printf("-1\n");
}
[14] 胜利大逃亡相关推荐
- HDU 1429 胜利大逃亡(续) (BFS+位压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 1253 胜利大逃亡
胜利大逃亡 Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 1253 ...
- HDU 1429 胜利大逃亡(续)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1253 胜利大逃亡 题解
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- HDU 1253.胜利大逃亡
胜利大逃亡 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u SubmitStatusP ...
- HDU-1429 胜利大逃亡(续)
A - 胜利大逃亡(续) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- [ACM] hdu 1253 胜利大逃亡 (三维BFS)
胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这但是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,能够被表示 ...
- HDOJ 1253 HDU 1253 胜利大逃亡 ACM 1253 IN HDU
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目描述: ...
- hdu 1429 胜利大逃亡(续) bfs+状态压缩
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
最新文章
- java菱形乱码 编码_JAVA:编码与乱码问题
- 区块链技术特点之去中心化特性
- Python打造最强表白程序
- python中的float和eval_python中eval和float_python中eval与json.loads对json的处理
- vue input file onchange_vue常用指令汇总
- ML 自学者周刊:第 4 期
- python中布尔类型是特殊的_Python中的特殊方法以及应用详解
- Delphi7函数大全
- android 新闻功能列表,android根据提供的接口获取新闻列表
- TypeScript 之泛型
- java异常代码_Java异常(示例代码)
- C语言 扫雷游戏(代码+注释)
- 蓝牙 - 注册SIG账号
- 比较全的OA系统功能模块列表
- 自动驾驶常用传感器介绍
- 龙卷风路径_ae龙卷风路径
- 双手指代脑区,读文献参考,大脑分区记不住怎么办
- HC-05蓝牙模块与STM32控制led灯亮详解
- Impala中的invalidate metadata 和refrsh
- qbo base board debug