(迷宫问题的算法实现.docVIP

  • 4
  • 0
  • 约 4页
  • 2016-12-06 发布于北京
  • 举报
(迷宫问题的算法实现

#ifndef MMIGONG_H #define MMIGONG_H #define MAX_SIZE 100 #includeiostream using namespace std; struct Node { int x; int y; int di; }; class Stack { private: int rrow; int ccolm; int top; int count; int minlenght; Node stack[MAX_SIZE]; Node sstack[MAX_SIZE]; public: Stack(); //初始化 //int **InsortMiGong(); //输入迷宫(即一个二维数组) void FindPath(int ab[][10]); //找出迷宫的出路 }; Stack::Stack() //初始化 { rrow=0; ccolm=0; top=-1; count=1; minlenght=MAX_SIZE; } /*int ** Stack::InsortMiGong() //输入迷宫(即一个二维数组) { int row=1,colm=1; while(true) { cout请输入迷宫的行数和列数:; cinrowcolm; if(row=0||colm=0) { cout输入错误!请重新输入:endl; rrow=row; ccolm=colm; continue; } else { rrow=row; ccolm=colm; break; } } int *mg[]; cout请输入迷宫矩阵(只有0和1两个数构成):; for(int i=0;irow;i++) for(int j=0;jcolm;j++) cinmg[i][j]; return mg; }*/ void Stack::FindPath(int ab[][10]) //找出迷宫的出路 { int a,b,di,find,k; top++;stack[top].x=1; stack[top].y=1; stack[top].di=-1; ab[1][1]=-1; while(top-1) { a=stack[top].x; b=stack[top].y; di=stack[top].di; if(a==8b==8) { coutcount++:endl; for(int k=0;k=top;k++) { cout(stack[k].x,stack[k].y); if(!((k+1)%15)) coutendl; } coutendl; if(top+1minlenght) { for(k=0;k=top;k++) sstack[k]=stack[k]; minlenght=top+1; } ab[stack[top].x][stack[top].y]=0; top--; a=stack[top].x; b=stack[top].y; di=stack[top].di; } find=0; while(di8!find) { di++; switch(di) { case 0:a=stack[top].x-1;b=stack[top].y;break; case 1:a=stack[top].x;b=stack[top].y+1;break; case 2:a=stack[top].x+1;b=stack[top].y;break; case 3:a=stack[top].x;b=stack[top].y-1;break; } if(ab[a][b]==0) find=1; } if (find==1) { stack[top].di=di; top++; stack[top].x=a; stack[top].y=b; stack[top].di=-1; ab[a][b]=-1; } else { ab[stack[top].x][stack[top].y]=0; top--; } } coutendl; cout走出迷宫最短的路径是:endl; cout其长度为:minlenghtendl; cout路径是:endl; for(k=0;kminlenght;k++) { cout(sstack

文档评论(0)

1亿VIP精品文档

相关文档