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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

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