找一个可以运行的迷宫算法代码,急用

ckt 2006-11-17 09:57:09
如题!
...全文
173 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
HappyTree 2006-11-17
  • 打赏
  • 举报
回复
呵呵,急用,估计自己是懒得写,情有可原!
HappyTree 2006-11-17
  • 打赏
  • 举报
回复
数据文件,maze.dat,
数据如下:
10
0 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 0 1 0 0
0 0 0 1 0 1 0 0 0 0
0 1 0 1 0 1 0 1 1 0
0 1 0 1 0 1 0 1 0 0
0 1 1 1 0 1 0 1 0 1
0 1 0 0 0 1 0 1 0 1
0 1 0 1 1 1 0 1 0 0
1 0 0 0 0 0 0 1 0 0
0 0 0 0 1 1 1 1 0 0
飞哥 2006-11-17
  • 打赏
  • 举报
回复
都三条内裤了,还不会写迷宫?

------
有待加强阿
HappyTree 2006-11-17
  • 打赏
  • 举报
回复
根据书上代码拼凑起来的,可以正确运行
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <stack>
using namespace std;

class Position {
friend bool InputMaze();
friend void OutputPath();
friend bool FindPath();
public:
operator int() const {return row;}
private:
int row, col;
};

template <class T>
void Make2DArray(T ** &x, int rows, int cols)
{
// Create a two-dimensional array.

// create pointers for the rows
x = new T * [rows];

// get memory for each row
for (int i = 0; i < rows; i++)
x[i] = new T [cols];
}

// globals
int **maze, m;
stack<Position> *path; // pointer to stack

void welcome() {};

bool InputMaze()
{
ostringstream oss;
ifstream ifs("maze.dat");

char size[10] = {0};
ifs.getline(size, 10);

oss << ifs.rdbuf();
string str = oss.str();
cout << str;

char *stop = NULL;
m = strtol(size, &stop, 10);
Make2DArray(maze, m + 2, m + 2);

string::iterator pos;
int i = 1, j = 1;
for (pos = str.begin(); pos != str.end(); ++pos)
{
switch(*pos)
{
case '0':
maze[i][j++] = 0;
break;
case '1':
maze[i][j++] = 1;
break;
case '\n':
i++;
j = 1;
break;
default:
break;
}
}

return true;
}

bool FindPath()
{// Find a path from (1,1) to the exit (m,m).
// Return true if successful, false if impossible.
// Throw NoMem exception if inadequate space.

path = new stack<Position>();

// initialize offsets
Position offset[4];
offset[0].row = 0; offset[0].col = 1; // right
offset[1].row = 1; offset[1].col = 0; // down
offset[2].row = 0; offset[2].col = -1; // left
offset[3].row = -1; offset[3].col = 0; // up

// initialize wall of obstacles around maze
for (int i = 0; i <= m+1; i++) {
maze[0][i] = maze[m+1][i] = 1; // bottom and top
maze[i][0] = maze[i][m+1] = 1; // left and right
}

Position here;
here.row = 1;
here.col = 1;
maze[1][1] = 1; // prevent return to entrance
int option = 0; // next move
int LastOption = 3;

// search for a path
while (here.row != m || here.col != m) {// not exit
// find a neighbor to move to
int r, c;
while (option <= LastOption) {
r = here.row + offset[option].row;
c = here.col + offset[option].col;
if (maze[r][c] == 0) break;
option++; // next option
}

// was a neighbor found?
if (option <= LastOption) {// move to maze[r][c]
path->push(here);
here.row = r; here.col = c;
// set to 1 to prevent revisit
maze[r][c] = 1;
option = 0;
}
else {// no neighbor to move to, back up
if (path->empty()) return false;
Position next;
next = path->top();
path->pop();
if (next.row == here.row)
option = 2 + next.col - here.col;
else option = 3 + next.row - here.row;
here = next;
}
}

return true; // at exit
}

void OutputPath()
{
// Output path to exit.
cout << "The path is" << endl;
Position here;
while (!path->empty()) {
here = path->top();
path->pop();
cout << here.row << ' ' << here.col << endl;}
}

void main(void)
{
welcome();
InputMaze();
if (FindPath()) OutputPath();
else cout << "No path" << endl;
}
飞哥 2006-11-17
  • 打赏
  • 举报
回复
我回帖不会扣我分吧
呵呵


别鄙视,楼主不容易

给分还挨骂 ^_^

要是都给我,我一句话都不说:)
HoBoss 2006-11-17
  • 打赏
  • 举报
回复
说实话,我是看楼主有三个角了才贴的,不是说急用吗?
SammyLan 2006-11-17
  • 打赏
  • 举报
回复
无条件BS贴一大段代码的人
中国计算机专业的大学生素质老是提不上去
你们要负上一定的责任
ckt 2006-11-17
  • 打赏
  • 举报
回复
可以了哈
谢谢了
HoBoss 2006-11-17
  • 打赏
  • 举报
回复
书上抄的一段,肯定不能直接运行,不过也差不多了

假定用n×m的矩阵来描述迷宫,位置( 1 , 1 )表示入口,(n,m) 表示出口,n和m分别代表迷宫
的行数和列数。迷宫中的每个位置都可用其行号和列号来指定。在矩阵中,当且仅当在位置
(i,j)处有一个障碍时其值为1,否则其值为0。

bool FindPath()
{ // 寻找从位置( 1 , 1 )到出口( m , m )的路径
// 如果成功则返回true ,否则返回false
// 如果内存不足则引发异常NoMem
path = new Stack<Position>(m * m - 1);
// 对偏移量进行初始化
Position offset [ 4 ] ;
offset[0].row = 0; offset[0].col = 1; //向右
offset[l].row = 1; offset[l].col = 0; // 向下
offset[2].row = 0; offset[2].col = -1; //向左
offset[3].row = -1; offset[3].col = 0; //向上
//在迷宫周围增加一圈障碍物
for (int i = 0; i <= m+l; i++) {
maze[0][i] = maze[m+l][i] = 1; //底和顶
maze[i][0] = maze[i][m+l] = 1; // 左和右
}
Position here;
here.row = 1;
here.col = 1;
maze[i][i] = 1; // 阻止返回入口
int option = 0;
int LastOption = 3;
// 寻找一条路径
while (here.row != m || here.col != m) {// 不是出口
// 寻找并移动到一个相邻位置
int r, c;
while (option <= LastOption) {
r = here.row + offset [option].row;
c = here.col + offset [option].col;
if (maze[r][c] == 0) break;
option++; //下一个选择
}
// 找到一个相邻位置了吗?
if (option <= LastOption) {// 移动到maze[r] [c]
path->Add(here) ;
here.row = r; here.col = c;
// 设置障碍物以阻止再次访问
maze[r][c] = 1;
option = 0;
}
else {//没有可用的相邻位置,回溯
if (path->IsEmpty()) return false;
Position next;
path->Delete(next);
if (next.row == here,row)
option = 2 + next.col - here.col;
else option = 3 + next.row - here.row;
here = next;
}
}
return true;//到达迷宫出口
}
lSaint 2006-11-17
  • 打赏
  • 举报
回复
刚好做过这个题目 呵
#include "stdio.h"
#include "stdlib.h"

typedef struct
{
int *base;
int *top;
int stacksize;
}sqstack;

int InitStack( sqstack *s) //创建栈
{
s->base = (int*)malloc(100*sizeof(int));
if(!s->base) return 0;
s->top = s->base;
s->stacksize=100;
return 1;
}

int push(sqstack *s, int e) // 压栈
{
if(s->top-s->base >= s->stacksize)
{
s->base = (int*)realloc(s->base,
(s->stacksize+10)*sizeof(int));// 栈满 开辟新空间
if(!s->base) return 0;
s->top = s->base + s->stacksize;
s->stacksize = s->stacksize + 10;
}
*(s->top)=e;
s->top=s->top+4;
return 1;
}


int gettop(sqstack *s) // 取栈顶元素
{
int e;
if(s->top==s->base)
return 0;
e = *(s->top-4);
return e;
}


int pop(sqstack *s) // 出栈
{
if(s->top == s->base) return 0;
s->top=s->top-4;
return 1;
}

int main()
{
sqstack *s = (sqstack*)malloc(sizeof(sqstack));
if( !InitStack(s) )
return 0;
int up,down,left,right, // 上下左右走
nowpos, // 当前位置
rows,cols, // 行数 列数
rownum,colnum, // 行号 列号
find=0; // 判断是否找到出口

nowpos = 1;
cols = 4;
rows = 4;

int record[4*4]; // 记录走过路径和地图信息
int map[4*4] = {0,2,0,0, // 地图信息
1,1,1,0,
1,0,0,0,
1,1,3,0};


for(int i=0;i<rows*cols;i++)
record[i]=map[i];

while(!find)
{
up = nowpos - cols;
down = nowpos + cols;
left = nowpos - 1;
right = nowpos + 1;

rownum = nowpos/cols;
colnum = nowpos%cols;

if( up>=0 && record[up] )
{
push(s,up);
record[up]=0;
record[nowpos]=0;
nowpos=up;
if(map[nowpos]==3)
find=1;
}

else if( down<=rows*cols-1 && record[down] )
{
push(s,down);
record[down]=0;
record[nowpos]=0;
nowpos=down;
if(map[nowpos]==3)
find=1;
}

else if( left>=rownum*cols && record[left] )
{
push(s,left);
record[left]=0;
record[nowpos]=0;
nowpos=left;
if(map[nowpos]==3)
find=1;
}

else if( right<=(rownum+1)*cols-1 && record[right] )
{
push(s,right);
record[right]=0;
record[nowpos]=0;
nowpos=right;
if(map[nowpos]==3)
find=1;
}

else
{
if(map[nowpos]==2 || s->top==s->base)
{
printf("NO WAY OUT");
exit(-1);
}
else
{
pop(s);
nowpos = gettop(s);
}
}
} // end while

int k ;
while (s->top!=s->base)
{
k = gettop(s);
pop(s);
printf("%d",k);
printf(" <- ");
}
printf("1");
return 1;
}
1.算法是程序的灵魂,优秀的程序在对海量数据处理时,依然保持高速计算,就需要高效的数据结构和算法支撑。2.网上数据结构和算法的课程不少,但存在两个问题:1)授课方式单一,大多是照着代码念一遍,数据结构和算法本身就比较难理解,对基础好的学员来说,还好一点,对基础不好的学生来说,基本上就是听天书了2)说是讲数据结构和算法,但大多是挂羊头卖狗肉,算法讲的很少。 本课程针对上述问题,有针对性的进行了升级 3)授课方式采用图解+算法游戏的方式,让课程生动有趣好理解 4)系统全面的讲解了数据结构和算法, 除常用数据结构和算法外,还包括程序员常用10大算法:二分查算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法、马踏棋盘算法。可以解决面试遇到的最短路径、最小生成树、最小连通图、动态规划等问题及衍生出的面试题,让你秒杀其他面试小伙伴3.如果你不想永远都是代码工人,就需要花时间来研究下数据结构和算法。教程内容:本教程是使用Java来讲解数据结构和算法,考虑到数据结构和算法较难,授课采用图解加算法游戏的方式。内容包括: 稀疏数组、单向队列、环形队列、单向链表、双向链表、环形链表、约瑟夫问题、栈、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式、递归与回溯、迷宫问题、八皇后问题、算法的时间复杂度、冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、基数排序(桶排序)、堆排序、排序速度分析、二分查、插值查、斐波那契查、散列、哈希表、二叉树、二叉树与数组转换、二叉排序树(BST)、AVL树、线索二叉树、赫夫曼树、赫夫曼编码、多路查树(B树B+树和B*树)、图、图的DFS算法和BFS、程序员常用10大算法、二分查算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法马踏棋盘算法。学习目标:通过学习,学员能掌握主流数据结构和算法的实现机制,开阔编程思路,提高优化程序的能力。

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧