⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 迷宫问题.htm

📁 解决迷宫问题(用c实现)
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0042)http://vip.6to23.com/dcyu/TurboC/DS/2.html -->
<HTML><HEAD><TITLE>Turbo C</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<STYLE type=text/css>BODY {
	SCROLLBAR-FACE-COLOR: #ffffff
}
INPUT {
	FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
INPUT {
	FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
.border {
	BORDER-RIGHT: 1px dotted; BORDER-TOP: 1px dotted; BORDER-LEFT: 1px dotted; COLOR: #000000; BORDER-BOTTOM: 1px dotted; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #efefef
}
.border {
	BORDER-RIGHT: 1px dotted; BORDER-TOP: 1px dotted; BORDER-LEFT: 1px dotted; COLOR: #000000; BORDER-BOTTOM: 1px dotted; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #efefef
}
.input {
	FONT-SIZE: 9pt; FONT-FAMILY: 宋体,Verdana, Arial, Helvetica, sans-serif
}
.links {
	FONT-SIZE: 9pt; COLOR: #000000; FONT-FAMILY: 宋体,Verdana, Arial, Helvetica, sans-serif
}
</STYLE>

<META content="sumipntg 011, default" name="Microsoft Theme">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR></HEAD>
<BODY text=#000066 vLink=#666699 aLink=#990099 link=#3333cc bgColor=#ffffff 
background=迷宫问题.files/sumtextb.htm>
<P> </P>
<DIV align=center>
<P><BR><A href="http://www.6to23.com/vip/asp/"><BR></A></P>
<TABLE height=24 cellSpacing=0 cellPadding=0 width=689 align=center border=0>
  <TBODY>
  <TR>
    <TD width=94>
      <P align=center><FONT color=#0000ff size=2>&nbsp;</FONT><A 
      href="http://vip.6to23.com/dcyu/TurboC/summarize.html"><FONT color=#0000ff 
      size=2>概述</FONT></A></P></TD>
    <TD width=94>
      <P align=center><A 
      href="http://vip.6to23.com/dcyu/TurboC/basic.html"><FONT color=#0000ff 
      size=2>初级篇</FONT></A></P></TD>
    <TD width=94>
      <P align=center><A 
      href="http://vip.6to23.com/dcyu/TurboC/advance.html"><FONT color=#0000ff 
      size=2>中级篇</FONT></A></P></TD>
    <TD width=94>
      <P align=center><A 
      href="http://vip.6to23.com/dcyu/TurboC/senior.html"><FONT color=#0000ff 
      size=2>高级篇</FONT></A></P></TD>
    <TD width=94>
      <P align=center><A 
      href="http://vip.6to23.com/dcyu/TurboC/sutra.html"><FONT color=#0000ff 
      size=2>经典篇</FONT></A></P></TD>
    <TD width=94>
      <P align=center><A 
      href="http://vip.6to23.com/dcyu/TurboC/algorithm.html"><FONT color=#0000ff 
      size=2>算法篇</FONT></A></P></TD>
    <TD width=94>
      <P align=center><FONT size=2>&nbsp;</FONT><A 
      href="http://vip.6to23.com/dcyu/TurboC/DS.html"><FONT color=#0000ff 
      size=2><SPAN 
    style="BACKGROUND-COLOR: #00ff00">数据结构</SPAN></FONT></A></P></TD>
    <TD width=100>
      <P align=center><A href="http://vip.6to23.com/dcyu/index.html"><FONT 
      color=#0000ff size=2>回到首页</FONT></A> </P></TD></TR></TBODY></TABLE>
<TABLE height=542 cellSpacing=0 cellPadding=0 width=619 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width=138 bgColor=#ffffff height=493>  
      <TABLE height=482 cellSpacing=0 cellPadding=0 width=686 
      background=迷宫问题.files/vccode.htm border=0>
        <TBODY>
        <TR>
          <TD align=left width="100%" height=21>2.迷宫问题
            <P><FONT color=#0000ff>问题</FONT>:找到迷宫中的一条出路,找到打印出路径,找不到这打印出Path not 
            found。</P>
            <P><FONT 
            color=#0000ff>算法</FONT>:实现迷宫的算法很多,有用栈实现和回溯实现以及用队列实现的,这里介绍用堆栈实现的</P>
            <P>例子,下面是程序的一个部分。</P>
            <P><BR>typedef struct {&nbsp;<BR>&nbsp;int x;<BR>&nbsp;int y;<BR>} 
            PosType;<BR><BR>typedef struct {&nbsp;<BR>&nbsp;int 
            ord;<BR>&nbsp;PosType seat;<BR>&nbsp;int di;<BR>} 
            SElemType;<BR><BR>typedef struct {<BR>&nbsp;int td;<BR>&nbsp;int 
            foot;<BR>&nbsp;int mark;<BR>} MazeType;<BR><BR>typedef struct 
            {<BR>&nbsp;SElemType *base;<BR>&nbsp;SElemType *top;<BR>&nbsp;int 
            stacksize;<BR>} Stack;<BR><BR>int Maze[20][30];<BR>MazeType 
            maze[20][30];</P>
            <P><BR>Status InitStack(Stack *s)<BR>/* The stack s has been created 
            and is initialized to be empty. 
            */<BR>{<BR>&nbsp;s-&gt;base=(SElemType 
            *)malloc(STACK_INIT_SIZE*sizeof(SElemType));<BR>&nbsp;if(!s-&gt;base) 
            Error("Overflow");<BR>&nbsp;s-&gt;top=s-&gt;base;<BR>&nbsp;s-&gt;stacksize=STACK_INIT_SIZE;<BR>&nbsp;return 
            OK;<BR>} /* InitStack */<BR><BR>Status DestroyStack(Stack *s)<BR>/* 
            The stack s has been destroyed. 
            */<BR>{<BR>&nbsp;s-&gt;top=NULL;<BR>&nbsp;s-&gt;stacksize=0;<BR>&nbsp;free(s-&gt;base);<BR>&nbsp;s-&gt;base=NULL;<BR>&nbsp;return 
            OK;<BR>} /* DestroyStack */<BR><BR>Status ClearStack(Stack *s)<BR>/* 
            The stack has been clear to be maximum. 
            */<BR>{<BR>&nbsp;s-&gt;top=s-&gt;base;<BR>&nbsp;s-&gt;stacksize=STACK_INIT_SIZE;<BR>&nbsp;return 
            OK;<BR>} /* ClearStack */<BR><BR>Boolean StackEmpty(Stack *s)<BR>/* 
            Check if the stack s is empty. 
            */<BR>{<BR>&nbsp;if(s-&gt;top==s-&gt;base) return 
            TRUE;<BR>&nbsp;else return FALSE;<BR>} /* StackEmpty */<BR><BR>int 
            StackLength(Stack *s)<BR>/* Gain the length of the stack s. 
            */<BR>{<BR>&nbsp;if(s-&gt;top&gt;s-&gt;base) return 
            (int)(s-&gt;top-s-&gt;base);<BR>&nbsp;else return 0;<BR>} /* 
            StackLength */<BR><BR>Status Push(Stack *s,SElemType e)<BR>/* The 
            element e has been pushed into the stack s. 
            */<BR>{<BR>&nbsp;if(s-&gt;top-s-&gt;base&gt;=s-&gt;stacksize)<BR>{<BR>&nbsp;s-&gt;base=(SElemType 
            *)realloc(s-&gt;base,<BR>&nbsp;(s-&gt;stacksize+STACKINCREMENT)*sizeof(SElemType));<BR>&nbsp;if(!s-&gt;base) 
            Error("Overflow");<BR>&nbsp;s-&gt;top=s-&gt;base+s-&gt;stacksize;<BR>&nbsp;s-&gt;stacksize+=STACKINCREMENT;<BR>}<BR>&nbsp;*s-&gt;top++=e;<BR>&nbsp;return 
            OK;<BR>} /* Push */<BR><BR>SElemType Pop(Stack *s,SElemType e)<BR>/* 
            The element e has been removed from the stack s. 
            */<BR>{<BR>&nbsp;if(s-&gt;top==s-&gt;base) 
            Error("Pop");<BR>&nbsp;e=*--s-&gt;top;<BR>&nbsp;return e;<BR>} /* 
            Pop */<BR><BR>Status GetTop(Stack *s,SElemType *e)<BR>/* The element 
            e has got to the top of the stack 
            s.*/<BR>{<BR>&nbsp;if(s-&gt;top==s-&gt;base) 
            Error("GetTop");<BR>&nbsp;*e=*(s-&gt;top-1);<BR>&nbsp;return 
            OK;<BR>} /* GetTop */<BR><BR>/* Traverse the stack s using 
            'visiting' function. */<BR>/* Status StackTraverse(Stack *s,Status 
            (* visit)(SElemType *se))<BR>{<BR>&nbsp;SElemType p;<BR>&nbsp;int 
            result;<BR>&nbsp;if(s-&gt;top==s-&gt;base) return 
            ERROR;<BR>&nbsp;p=s-&gt;base;<BR>&nbsp;while(!(p==s-&gt;top))<BR>&nbsp;{<BR>&nbsp; 
            result=(*visit)(p);<BR>&nbsp; p++;<BR>&nbsp;}<BR>&nbsp;return 
            OK;<BR>} */<BR><BR>Boolean Pass(PosType curpos)<BR>/* Check if the 
            current position can be passed. 
            */<BR>{<BR>&nbsp;if(maze[curpos.x][curpos.y].td==1&amp;&amp;<BR>&nbsp;maze[curpos.x][curpos.y].foot==0&amp;&amp;maze[curpos.x][curpos.y].mark==0)<BR>&nbsp;return 
            TRUE;<BR>&nbsp;else return FALSE;<BR>}&nbsp; /* Pass */<BR><BR>void 
            MarkPrint(PosType seat)<BR>/* Mark the position seat. 
            */<BR>{<BR>&nbsp;maze[seat.x][seat.y].mark=-1;<BR>/* Marking '-1' 
            symbolize the current position cannot be put. */<BR>} /* MarkPrint 
            */<BR><BR>void FootPrint(PosType curpos)<BR>/* The foot of the 
            curpos of the maze has been set 'true'. 
            */<BR>{<BR>&nbsp;maze[curpos.x][curpos.y].foot=1;<BR>} /* FootPrint 
            */<BR><BR>PosType NextPos(PosType seat,int 
            di)<BR>{<BR>&nbsp;switch(di)<BR>&nbsp;{<BR>&nbsp; case 1: seat.y++; 
            return seat; /* Eastward */<BR><BR>&nbsp; case 2: seat.x++; return 
            seat; /* Southward */<BR><BR>&nbsp; case 3: seat.y--; return seat; 
            /* Westward */<BR><BR>&nbsp; case 4: seat.x--; return seat; /* 
            Northward */<BR><BR>&nbsp; default: seat.x=0; seat.y=0; return 
            seat;<BR>&nbsp;}<BR>} /* NextPos */<BR><BR><BR>/* The key to the 
            program. */<BR>/* Pre: The maze array &amp; the startplace &amp; the 
            endplace.<BR>Post: Find the one traverse of the maze and perform the 
            mazepath.<BR>Uses: The ADT stack class.<BR>*/<BR><BR>Status 
            MazePath(PosType start,PosType end)<BR>{<BR>&nbsp;PosType 
            curpos;<BR>&nbsp;int curstep;<BR>&nbsp;SElemType e;<BR>&nbsp;Stack 
            *s,stack;<BR>&nbsp;stack.base=(SElemType 
            *)malloc(STACK_INIT_SIZE*sizeof(SElemType));<BR>&nbsp;if(!stack.base) 
            Error("Overflow");<BR>&nbsp;stack.top=stack.base;<BR>&nbsp;stack.stacksize=STACK_INIT_SIZE;<BR>&nbsp;s=&amp;stack;<BR>&nbsp;curpos=start;<BR>&nbsp;curstep=1;<BR>&nbsp;do<BR>&nbsp;{<BR>&nbsp; 
            if(Pass(curpos))<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; 
            FootPrint(curpos);<BR>&nbsp;&nbsp;&nbsp; e.ord=curstep; 
            e.seat=curpos; e.di=1;<BR>&nbsp;&nbsp;&nbsp; 
            gotoxy((curpos.y+1)*2,curpos.x+2);<BR>&nbsp;&nbsp;&nbsp; 
            putch('@');<BR>&nbsp;&nbsp;&nbsp; delay(8000); /* pospone time. 
            */<BR>&nbsp;&nbsp;&nbsp; Push(s,e);<BR>&nbsp;&nbsp;&nbsp; 
            if(curpos.x==end.x&amp;&amp;curpos.y==end.y) /* Proceed recursively. 
            */<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
            DestroyStack(s);<BR>&nbsp;&nbsp;&nbsp;&nbsp; return 
            TRUE;<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; curpos=NextPos(curpos,1); /* 
            Try next position. */<BR>&nbsp;&nbsp; curstep++;<BR>&nbsp; 
            }<BR>&nbsp; else<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; 
            if(!StackEmpty(s))<BR>&nbsp;&nbsp;&nbsp; 
            {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e=Pop(s,e); /* Removed e from s. 
            */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            while(e.di==4&amp;&amp;!StackEmpty(s)) /* Four directions have been 
            checked<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            and s is not empty. */<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
            {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            MarkPrint(e.seat);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            gotoxy((e.seat.y+1)*2,e.seat.x+2);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            putch('@');<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(8000); /* 
            Pospone time. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            gotoxy((e.seat.y+1)*2,e.seat.x+2);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            putch(' ');<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e=Pop(s,e); /* 
            Remove e from s. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            curstep--;<BR>&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
            if(e.di&lt;4) /* The current position hasnot been checked. 
            */<BR>&nbsp;&nbsp;&nbsp;&nbsp; 
            {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            e.di++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Push(s,e); /* Insert 
            e into s. */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            curpos=NextPos(e.seat,e.di); /* Try next position. 
            */<BR>&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; 
            }<BR>&nbsp;&nbsp; 
            }<BR>&nbsp;}<BR>&nbsp;while(!StackEmpty(s));<BR>&nbsp;DestroyStack(s);<BR>&nbsp;return 
            FALSE;<BR>} /* MazePath */<BR></P>
            <P>完整的程序见:</P>
            <P>程序:<A href="http://vip.6to23.com/dcyu/TurboC/DS/maze.zip"><FONT 
            color=#0000ff>maze.zip</FONT></A><FONT 
            color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT 
            color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><A 
            href="http://vip.6to23.com/dcyu/TurboC/DS/1.html"><FONT 
            color=#0000ff>上一页</FONT></A><FONT 
            color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; 
            </FONT><A href="http://vip.6to23.com/dcyu/TurboC/DS/2.html"><FONT 
            color=#0000ff>下一页</FONT></A></P>
            <P> </P></TD></TR>
        <TR>
          <TD align=left width="100%" height=21>  
  </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<HR align=center width=740 color=#8fb373 noShade SIZE=1>

<TABLE cellSpacing=0 cellPadding=0 width="90%" border=0>
  <TBODY>
  <TR bgColor=#949231>
    <TD height=3></TD></TR></TBODY></TABLE>
<TABLE height=37 width=490>
  <TBODY>
  <TR>
    <TD vAlign=top align=left width=486 height=8>
      <P align=center><FONT size=2><FONT face=Arial>Copyright 
      2001-2002</FONT>,<FONT face=Arial>dcyu,&nbsp; All rights reserved</FONT>, 
      上次更新:2002-10-13</FONT>&nbsp;<FONT color=#000000><BR></FONT><FONT 
      color=#000080 size=2>E-mail : <A 
      href="mailto:dcyu@163.net">dcyu@163.net</A>&nbsp;&nbsp; QQ : 
      28009316&nbsp;</FONT></P></TD></TR>
  <TR>
    <TD class=mainfont vAlign=top align=left width=486 height=17>
      <P align=center><FONT size=2>建议使用IE 5.0以上版本进行浏览 
    最佳显示分辨率800*600</FONT></P></TD></TR></TBODY></TABLE>  </DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -