bfs
来源:互联网 发布:周朝 历史 知乎 编辑:程序博客网 时间:2024/06/05 00:35
首先是校赛的一道裸的四维bfs
Problem Description
蓝色空间号和万有引力号进入了四维水洼,发现了四维物体--魔戒。
这里我们把飞船和魔戒都抽象为四维空间中的一个点,分别标为 "S" 和 "E"。空间中可能存在障碍物,标为 "#",其他为可以通过的位置。
现在他们想要尽快到达魔戒进行探索,你能帮他们算出最小时间是最少吗?我们认为飞船每秒只能沿某个坐标轴方向移动一个单位,且不能越出四维空间。
Input
输入数据有多组(数据组数不超过 30),到 EOF 结束。
每组输入 4 个数 x, y, z, w 代表四维空间的尺寸(1 <= x, y, z, w <= 30)。
接下来的空间地图输入按照 x, y, z, w 轴的顺序依次给出,你只要按照下面的坐标关系循环读入即可。
for 0, x-1
for 0, y-1
for 0, z-1
for 0, w-1
保证 "S" 和 "E" 唯一。
Output
对于每组数据,输出一行,到达魔戒所需的最短时间。
如果无法到达,输出 "WTF"(不包括引号)。
Example Input
2 2 2 2...S..#.#..E.#..2 2 2 2...S#.##E..##...
Example Output
13
//by bLue#include <cstdio>#include <cstring>#include <queue>using namespace std;const int MAX = 30;struct info {int x, y, z, w;int step;} s;int x, y, z, w;char g[MAX][MAX][MAX][MAX+1];bool vis[MAX][MAX][MAX][MAX];int dirt[8][4] = {{0, 0, 0, 1}, {0, 0, 1, 0}, {0, 1, 0, 0}, {1, 0, 0, 0},{0, 0, 0, -1}, {0, 0, -1, 0}, {0, -1, 0, 0}, {-1, 0, 0, 0},};bool Judge(info p) {if(p.x>=0 && p.x<x&& p.y>=0 && p.y<y&& p.z>=0 && p.z<z&& p.w>=0 && p.w<w&& g[p.x][p.y][p.z][p.w]!='#')return true;else return false;}int BFS() {queue<info> q;q.push(s);memset(vis, false, sizeof(vis));vis[s.x][s.y][s.z][s.w] = true;while(!q.empty()) {info f = q.front();q.pop();if(g[f.x][f.y][f.z][f.w] == 'E')return f.step;for(int i=0; i<8; ++i) {info nxt = f;nxt.x = f.x + dirt[i][0];nxt.y = f.y + dirt[i][1];nxt.z = f.z + dirt[i][2];nxt.w = f.w + dirt[i][3];if(Judge(nxt) && !vis[nxt.x][nxt.y][nxt.z][nxt.w]) {nxt.step++;q.push(nxt);vis[nxt.x][nxt.y][nxt.z][nxt.w] = true;}}}return -1;}int main(int argc, char const *argv[]) {while(~ scanf("%d %d %d %d", &x, &y, &z, &w)) {bool found = false;for(int i=0; i<x; ++i) {for(int j=0; j<y; ++j) {for(int k=0; k<z; ++k) {scanf("%s", g[i][j][k]);for(int l=0; l<w && !found; ++l) {if(g[i][j][k][l] == 'S') {s = (info){i, j, k, l, 0};found = true;}}}}}int ans = BFS();if(ans != -1) printf("%d\n", ans);else printf("WTF\n");}return 0;}
大概是有一个判断judge函数和一个bfs就能很好的搞出来,不过记录步数是不是一定要用结构体?
阅读全文
1 0
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- BFS
- BFS
- BFS
- BFS
- bfs
- BFS
- BFS
- MySQL创建存储过程时出错(...use near ' 'at line)
- aapt 命令可应用于查看apk包名、主activity、版本等很多信息
- 假如机器有感情
- linux 3.12内核里系统调用常见的问题
- Latex 中连加符号的上下界问题总结
- bfs
- 卸载串口,usb转接口等驱动程序
- 淘淘屋
- Fragment使用接口回调传值
- Android图片轮播控件
- QTAtom学习
- 字典树入门详解+一道入门水题
- Webstorm 快捷健设置
- Spring单元测试加载配置文件启动异常