贪婪+回溯--迷宫问题--全部路径和最短路径

1.0

#include
#include
#define R 7//行 
#define C 8//列 
int M[R+2][C+2]={{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,1,1,1,1,1,1},{1,0,0,0,0,0,0,1,1,1},
		{1,1,1,1,1,1,0,1,1,1},{1,1,1,1,1,1,0,1,1,1},{1,1,1,1,1,1,0,1,1,1},
		{1,1,1,1,1,1,0,1,1,1},{1,1,1,1,1,1,0,0,0,1},{1,1,1,1,1,1,1,1,1,1}};
int T[R+2][C+2]={0};//标记试探过的路径
int Move[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//分别表示向南、东、北、西
int tag=0;
int stack[100][2]={0};//声明一个栈空间存取路径 
int top=0;//因为stack[0][0]位置已经赋了初值 
int Min=2147483647;
int result[100][2]={0};

void Count()
{
	if(top

2.0

#include
#define N  6
typedef struct {
	int c,r;
}Stack;
Stack s[N*N];
Stack result[N*N];	
int Min=N*N; 
int top=-1;
int m1[N+2][N+2]={
{0 ,0 ,0 ,0 ,0 ,0 ,0 ,0},
{0 ,1 ,1 ,0 ,0 ,0 ,0 ,0},
{0 ,1 ,1 ,1 ,1 ,0 ,0, 0},
{0, 0 ,0 ,0 ,1 ,0 ,0 ,0},
{0, 0, 0, 0, 1, 1, 0, 0},
{0 ,0, 0, 0 ,0, 1, 0, 0},
{0 ,0 ,0 ,0 ,0 ,1 ,1 ,0},
{0 ,0, 0, 0, 0, 0, 0, 0}
};  
int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};	//南东北西 


void Count()
{
	if(top",s[i].c,s[i].r); 
	}
	printf("\n\n");
}
void Maze(int x,int y)
{
	if(x==6&&y==6){
		Print();
		Count();
		return ;
	}
	for(int i=0;i<4;i++){
		int a=x+move[i][0];
		int b=y+move[i][1];
		if(m1[a][b]==1){
			++top;
			s[top].c=a;
			s[top].r=b;
			m1[a][b]=2;
			Maze(a,b);
			m1[a][b]=1;
			--top;
		}
	}
}

int main(void)
{
	++top;
	s[top].c =s[top].r=1; 
	Maze(1,1);
	//输出最优解
	printf("其中一条最短路径为:\n");
	int i;
	for(i=0;i<=Min;i++)
		printf("(%d,%d)-->",result[i].c,result[i].r); 
	return 0; 
}


你可能感兴趣的:(算法)