hdoj1240 Asteroids!
来源:互联网 发布:电脑文件恢复软件 编辑:程序博客网 时间:2024/06/02 04:01
题意:
BFS的小小变形,场景由二维迷宫变成三维空间的迷宫,基本框架是不变的.
这个题目就是考耐心嘛,毕竟就像一篇阅读理解,只需要看懂那个slice就是指z轴上不同点的平面就可以了.另外,需要注意题目中给出的起点的顺序是:start_y(列), start_x(行), start_z,这个需要跟自己的数组中的每一维的意义对应起来,否则结果就会不对.然后还要注意,每走到一个点,都会有6个方向的点(上\下\左\右\前\后)待探索.
代码:
// 0ms, 1804K// BFS on a cubic maze#include <iostream>#include <cstring>#include <queue>using namespace std;struct Location { int x, y, z, step; Location(int x, int y, int z, int s) : x(x), y(y), z(z), step(s) { }};const int FAIL_VALUE = -1;const int DIR[6][3] = { {1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1} }; // 6 directions every explorationconst int MAX_DIMEN(11);char maze[MAX_DIMEN][MAX_DIMEN][MAX_DIMEN];bool isVisited[MAX_DIMEN][MAX_DIMEN][MAX_DIMEN];int dimen;int _x2, _y2, _z2; // coordinates of target pointint _x1, __y1, _z1; // coordinates of start pointbool isValid(const int& x, const int& y, const int& z) { return x >= 0 && y >= 0 && z >= 0 && x < dimen && y < dimen && z < dimen;}int bfs() { memset(isVisited, false, sizeof isVisited); // initial visit-array queue<Location> locations; isVisited[_x1][__y1][_z1] = true; locations.push( Location(_x1, __y1, _z1, 0) ); while (!locations.empty()) { Location cur = locations.front(); //cout << _debug << "\t" << cur.step << endl; locations.pop(); if (cur.x == _x2 && cur.y == _y2 && cur.z == _z2) { // if arriving at the target point return cur.step; } for (int dir = 0; dir < 6; ++dir) { int xx = cur.x + DIR[dir][0]; int yy = cur.y + DIR[dir][1]; int zz = cur.z + DIR[dir][2]; if (isValid(xx, yy, zz) && !isVisited[zz][xx][yy] && maze[zz][xx][yy] == 'O') { // those which can reach isVisited[zz][xx][yy] = true; locations.push( Location(xx, yy, zz, cur.step + 1) ); } } } return FAIL_VALUE;}int main() {// freopen("in.txt", "r", stdin); char op[10]; while (cin >> op >> dimen) { // 'START N' /* input phase */ int x, y, z; for (int z = 0; z < dimen; ++z) { for (int x = 0; x < dimen; ++x) { for (int y = 0; y < dimen; ++y) { cin >> maze[z][x][y]; } } } cin >> __y1 >> _x1 >> _z1; // mind the order of input... cin >> _y2 >> _x2 >> _z2; cin >> op; // 'END' /* process phase */ int need_step = bfs(); if (need_step == FAIL_VALUE) { cout << "NO ROUTE" << endl; } else { cout << dimen << " " << need_step << endl; } } return 0;}
0 0
- HDOJ1240 Asteroids!
- hdoj1240 Asteroids!
- hdoj1240
- hdoj1240--BFS
- Asteroids!
- Asteroids!
- Asteroids
- Asteroids
- Asteroids!
- Asteroids
- Asteroids
- Asteroids
- Asteroids!
- Asteroids!
- Asteroids
- Asteroids
- Asteroids
- Asteroids
- 企业Linux系统管理员常用命令大全实例解析(2)
- caffe Data layer prefetch queue empty重启
- 71道经典Android面试题和答案,重要知识点都包含了
- vector 详细用法 C++
- PHP PSR-2 代码风格规范 (中文版)
- hdoj1240 Asteroids!
- C#面试题
- 小猫统计定制主从表编辑界面,上手特简单(后台编程好麻烦)
- JNI学习积累之三 ---- 操作JNI函数以及复杂对象传递
- IOS中通知中心NSNotificationCenter应用总结
- qwebpage中在新窗口打开连接失败
- Item 03:尽可能使用const
- android 关于如何设置ListView中item高度问题的解决方法
- BZOJ3170~3172 TJOI2013 DAY1