Data Structure(9.16)

 1.Find the path in a maze

2.others' thinking

First, we need a stack to put the node. initalized a two dimensions array to put the maze. And a stack is include the factorys, "i" to define the node's row  "j" to define the node's colum and "di" is a label, if it's true, that means that there's a node could be put in the path that next to this node, else that means that the next node's next is blocked and the next node's label of the maze could be put -1 to avoid repreat. 

At first, put the begin node into the stack and analysis if the node is the end, if it's, then cout all the nodes int the stack and that's a path that we find in this way. else, we define the

3.(1)solution with the stack method

#include<iostream>

#define Colum 10

#define Row 10

#define Maxsize 100

using namespace std;

int background[Row][Colum]={

1,1,1,1,1,1,1,1,1,1,

1,0,0,1,0,0,0,1,0,1,

1,0,0,1,0,0,0,1,0,1,

1,0,0,0,0,1,1,0,0,1,

1,0,1,1,1,0,0,0,0,1,

1,0,0,0,1,0,0,0,0,1,

1,0,1,0,0,0,1,0,0,1,

1,0,0,1,1,0,1,1,0,1,

1,0,0,0,0,0,0,0,0,1,

1,1,1,1,1,1,1,1,1,1};

struct node{

int di;//记录下一个可走的方向

int i;//记录行

int j;//记录列

}st[Maxsize];

int top=-1;//初始化栈

int findpath(int bi,int bj,int ei,int ej){

top++;

int i,j,di, find=0;

st[top].i=bi;

st[top].j=bj;

st[top].di=-1;

while(top>-1){

if((st[top].i==ei)&&(st[top].j=ej)){

while(top>-1){

cout<<"路径为:"<<endl;

cout<<"("<<st[top].i<<","<<st[top].j<<")    ";

top--;

if(top%5==0){cout<<endl;}


}

return 1;

}

find=0;

while(di<4&&find==0){

di++;

switch(di){

case 0:i=st[top].i-1;j=st[top].j;break;

case 1:i=st[top].i;j=st[top].j+1;break;

case 2:i=st[top].i+1;j=st[top].j;break;

case 3:i=st[top].i;j=st[top].j-1;break;

}

if(background[i][j]==0)find=1;

}

if(find==1){

st[top].di=di;

top++;

st[top].i=i;st[top].j=j;st[top].di=-1;

background[i][j]=-1;

}

else{background[st[top].i][background[st[top].j]]=0;top--;}

}

return 0;

}

int main(){

findpath(1,1,8,8);

return 0;

}

(2)solution with the queue method

To find the shortest path of the maze is meaningful to use a queue to put the nodes that could leads to the destination of the maze.

Define the maze with a two dimensons array. Define the queue structure with a abscissa and a ordinate and an integer variable namly "pri" to indicate the node's previous node's subscript(下标). Define two global variables to indacate the head of the queue and the tail of the queue separately namely "front" and "rear".  Initializate the queue with the begin node's abscissa and ordinate and the -1 equals to the pri.

Then, while the queue is not empty, analysis the tail node if it's equals to the end of the maze, then the output function works. Else, make the value of the node in the maze -1 to avoid repetition. and then to check the arounds nodes' maze value, if it's vales 0, then put the node into the queue and at the same time value the variable "find" 1, and the node's pri values to the front node's subscript.

The output function is to find the head of the queue based the front's node's pri value, and then output the whole nodes that leads to the destination of the maze.



Summary:

Clearly know what you are doing and what do you want and know what will happen and how to get your solution. It's difficult for me. The beginning is always much hard, come on for myself.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 阴阳相隔,生离死别大概是故事最擅长的开始或是结束。 01 微风,长发飘飘,凄美伤感的背影,在一群火辣辣的泳衣中,显...
    妖怪在流浪阅读 3,038评论 0 0
  • 阿努比斯1117阅读 3,674评论 0 1
  • 你学着舍友懒床,殊不知人家就等着毕业进爸妈公司做高管;你学着朋友矫情,殊不知人家除了感情其他都不用自己担心了;你学...
    我是唐一米阅读 793评论 0 0
  • 发现要做的事情越来越多,只能硬着头皮一件件去做,但愿一切顺利,加油啊。
    盐味废柴阅读 803评论 0 0