POJ9384 迷宫问题(基础BFS)
来源:互联网 发布:维生素 抑郁症 知乎 编辑:程序博客网 时间:2024/06/05 08:21
本文出自:http://blog.csdn.net/svitter
题目:让你从(0, 0)走到(4,4),并且输出路径。输入数据:二位数组的迷宫;输出数据:路径;
题解:简单的BFS
注意:
1.去重;
2.墙不能走;
3.记录前一个节点
代码:
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;int maze[6][6];bool visit[5][5];struct node{ node *pre; int i; int j; node(){} node(int a, int b, node*c):i(a), j(b), pre(c){}};//1.去重//2.是否墙壁//inline bool judge(int i, int j, int temp){ if(temp < 0 || temp > 4) return false; //i, j 可以通行,并且没有访问过 if(maze[i][j] != 1 && !visit[i][j]) return true; return false;}void output(){}int main(){ int i, j; for(i = 0; i < 5; i++) for(j = 0; j < 5; j++) scanf("%d", &maze[i][j]); node queue[1000]; memset(visit, 0, sizeof(visit)); int front , rear; front = rear = -1; node cur; int temp; queue[++rear] = node(4, 4, NULL); while(front != rear) { cur = queue[++front]; i = cur.i; j = cur.j; if(i == 0 && j == 0) break; temp = cur.i+1; if(judge(temp, j, temp)) { queue[++rear] = node(temp, j, &queue[front]); visit[temp][j] = 1; } temp = cur.j+1; if(judge(i, temp, temp)) { queue[++rear] = node(i, temp, &queue[front]); visit[i][temp] = 1; } temp = cur.i-1; if(judge(temp, j, temp)) { queue[++rear] = node(temp, j, &queue[front]); visit[temp][j] = 1; } temp = cur .j-1; if(judge(i, temp, temp)) { queue[++rear] = node(i, temp, &queue[front]); visit[i][temp] = 1; } } node *tmp; tmp = &queue[front]; while(tmp != NULL) { printf("(%d, %d)\n", tmp->i, tmp->j); tmp = tmp->pre; } return 0;}
0 0
- POJ9384 迷宫问题(基础BFS)
- POJ 迷宫问题+基础bfs
- POJ3984 迷宫问题 BFS
- 经典迷宫问题BFS
- 迷宫问题 bfs
- 迷宫问题BFS暴搜
- BFS | 3984 | 迷宫问题
- hncu1102:迷宫问题(BFS)
- POJ3984 迷宫问题 BFS
- poj3984 迷宫问题(BFS)
- 迷宫问题BFS
- 【BFS】迷宫类问题
- 迷宫问题bfs
- POJ3984 迷宫问题【BFS】
- BFS解迷宫问题
- BFS求解迷宫问题
- poj3984迷宫问题(bfs)
- 迷宫问题(BFS)
- Echarts 后台交互
- iOS 学习Block小结
- ORACLE存储过程
- 『黑马程序员』第九话<正则表达式>
- AD9通过Excel快速制作原理图符号
- POJ9384 迷宫问题(基础BFS)
- html显示一个长的错误单词换行,内容自动换行
- 入木三分学网络第一篇--VRRP协议详解-----(2)
- adb修改hosts方法
- linux 进程管理-----pid哈希链表
- C++内存分配方式详解
- hdu1010 Tempter of the Bone
- 二叉树深度广度遍历
- jQuery ajax - ajax() 方法