---
title: "迷宫（多出口）"
author: "vergilwang"
date: "2011-12-10T12:09:00Z"
tags: []
categories: ["互联网"]
slug: "untitled"
draft: false
lastmod: "2014-02-15T16:23:57Z"
id: 2011419
url: "https://vergilwang.iteye.com/blog/2011419"
---

/*  
\* =====================================================================================  
\*  
\* Filename: maze.c  
\*  
\* Description:  
\*  
\* Version: 1.0  
\* Created: 2011年12月09日 21时33分32秒  
\* Revision: none  
\* Compiler: gcc  
\*  
\* Author: YOUR NAME (),  
\* Company:  
\*  
\* =====================================================================================  
\*/  
#include &lt;stdio.h&gt;  
#include &lt;stdlib.h&gt;  
int b\[5]\[15]= {0};  
int cnt\_path = 0;  
int min = 65535;  
/\*stack\*/  
struct stack\_node  
{  
int x;  
int y;  
struct stack\_node \*next\_ptr;  
};

void push(struct stack\_node \*\*top\_ptr, int x, int y)  
{  
struct stack\_node \*new\_ptr;  
new\_ptr = malloc(sizeof(struct stack\_node));  
if (new\_ptr != NULL)  
{  
new\_ptr-&gt;x = x;  
new\_ptr-&gt;y = y;  
new\_ptr-&gt;next\_ptr = \*top\_ptr;  
\*top\_ptr = new\_ptr;

}  
}  
void pop(struct stack\_node\** top\_ptr)  
{  
struct stack\_node* temp\_ptr;  
temp\_ptr = \*top\_ptr;  
\*top\_ptr = (\*top\_ptr)-&gt;next\_ptr;  
free(temp\_ptr);  
}

void print\_stack(struct stack\_node* current\_ptr)  
{  
if(current\_ptr == NULL)  
{  
printf("the stack is empty.\\n");  
}  
else  
{  
while(current\_ptr != NULL)  
{  
printf("(%d,%d)\\n", current\_ptr-&gt;x, current\_ptr-&gt;y);  
current\_ptr = current\_ptr-&gt;next\_ptr;  
}  
printf("NULL\\n ");  
}  
}  
int is\_empty(struct stack\_node * top\_ptr)  
{  
return top\_ptr == NULL;  
}

int find(int a\[5]\[15], int i, int j, int n, int m, struct stack\_node \*stack\_ptr)  
{

b\[i]\[j] = 1;  
cnt\_path++;  
push(&stack\_ptr, i+1, j+1);

if (a\[i]\[j] == 2)  
{  
print\_stack(stack\_ptr);  
printf("%d\\n", cnt\_path);  
cnt\_path--;  
return 0;  
}

if (j + 1 &lt; m)//右  
{  
if (0 != a\[i]\[j+1] && 0 == b\[i]\[j+1] )  
{  
find(a, i, j+1, n, m,stack\_ptr);  
}  
}

if (i + 1 &lt; n)//下  
{  
if (0 != a\[i+1]\[j] && 0 == b\[i+1]\[j])  
{  
find(a, i+1, j, n, m,stack\_ptr);  
}  
}

if (j - 1 &gt;= 0 )//左  
{  
if (0 != a\[i]\[j-1] && 0 == b\[i]\[j-1])  
{  
find(a, i, j-1, n, m,stack\_ptr);  
}  
}

if (i - 1 &gt;= 0)//向上  
{  
if (0 != a\[i-1]\[j] && 0 == b\[i-1]\[j])  
{  
find(a, i-1, j, n, m,stack\_ptr);  
}  
}

b\[i]\[j] = 0;  
cnt\_path--;  
pop(&stack\_ptr);

}

int main()  
{  
int n,m;  
n = 5;  
m = 15; //exit 2 start 3  
struct stack\_node * stack\_ptr = NULL;//在第第一次push的时候NULL会被复制到top\_ptr-&gt;next\_ptr中  
int a\[5]\[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  
3,1,1,1,1,1,1,1,1,1,1,1,2,0,0,  
0,0,1,0,0,1,0,0,0,0,0,0,1,1,0,  
0,0,1,0,1,1,1,1,1,1,1,1,1,0,0,  
0,0,0,0,0,0,0,0,0,0,0,0,2,0,0};

find(a, 1, 0, n, m, stack\_ptr);//1，0代表出发的坐标，也就是3的坐标

}