• 栈,迷宫问题


    #include"Stack.h"
    #define N 6 
    
    int maze[N][N] = { 
    	{0,0,0,0,0,0}, 
    	{0,0,1,1,1,0}, 
    	{0,0,1,0,1,0}, 
    	{0,0,1,1,1,0}, 
    	{0,0,1,0,1,1}, 
    	{0,0,1,0,0,0}, 
    }; 
    
    void MazePrint();
    int MazeCheckIsAccess(Pos pos);
    int MazeGetPath(Pos entry);
    int MazeGetPathR(Pos entry); 
    Stack shortPath;
    void MazeGetShortPath(Pos entry, Stack* path);
    void TestMaze();
    
    ///
    
    void MazePrint(){
    	int i,j;
    	for(i=0;i<6;i++){
    		for(j=0;j<6;j++)
    			printf("%d  ",maze[i][j]);
    		printf("
    ");
    	}
    	printf("
    ");
    }
    
    int MazeCheckIsAccess(Pos pos){
    	if(pos._row>=0&&pos._row<N
    		&&pos._col>=0&&pos._col<N
    		&&maze[pos._row][pos._col]==1)
    		return 1;
    	else
    		return 0;
    }
    
    //一条通路迷宫
    int MazeGetPath(Pos entry){
    	Stack s;
    	Pos next,cur=entry;
    	StackInit(&s);
    	StackPush(&s,cur);
    
    	while(StackEmpty(s)){
    		maze[cur._row][cur._col]=2;
    		next=cur;
    		//		printf("%d,%d  ",cur._row,cur._col);
    		if(next._col==N-1)
    			return 1;
    
    		//up
    		next._row-=1;
    		if(MazeCheckIsAccess(next)){
    			StackPush(&s,next);
    			cur=next;
    			continue;
    		}
    
    		//down
    		next=cur;
    		next._row+=1;
    		if(MazeCheckIsAccess(next)){
    			StackPush(&s,next);
    			cur=next;
    			continue;
    		}
    
    		//left
    		next=cur;
    		next._col-=1;
    		if(MazeCheckIsAccess(next)){
    			StackPush(&s,next);
    			cur=next;
    			continue;
    		}
    
    		//right
    		next=cur;
    		next._col+=1;
    		if(MazeCheckIsAccess(next)){
    			StackPush(&s,next);
    			cur=next;
    			continue;
    		}
    		StackPop(&s);
    		cur=StackTop(&s);
    	}
    	return 0;
    }
    
    //递归解法
    int MazeGetPathR(Pos entry){
    	Pos cur= entry;
    
    	maze[cur._row][cur._col]=2;
    	if(cur._col==N-1)
    		return 1;
    
    	cur._row-=1;
    	if(MazeCheckIsAccess(cur)){
    		if(MazeGetPathR(cur)){
    			return 1;
    		}
    	}
    
    	cur=entry;
    	cur._row+=1;
    	if(MazeCheckIsAccess(cur)){
    		if(MazeGetPathR(cur)){
    			return 1;
    		}
    	}
    
    	cur=entry;
    	cur._col-=1;
    	if(MazeCheckIsAccess(cur)){
    		if(MazeGetPathR(cur)){
    			return 1;
    		}
    	}
    
    	cur=entry;
    	cur._col+=1;
    	if(MazeCheckIsAccess(cur)){
    		if(MazeGetPathR(cur)){
    			return 1;
    		}
    	}
    
    	return 0;
    }
    
    //多路迷宫求最短路径
    Stack shortPath;
    
    void MazeGetShortPath(Pos entry, Stack* path){
    	Pos cur = entry;
    	//栈保存坐标
    	StackPush(path, cur);
    
    	printf("[%d,%d]  ",cur._row,cur._col);
    
    	maze[cur._row][cur._col] = 2;
    	if (cur._col == N - 1)
    	{
    		printf("出口:[%d,%d]
    ", cur._row, cur._col);
    		if (!(StackEmpty(shortPath)) || StackSize(*path) < StackSize(shortPath))
    		{
    			//深浅拷贝问题
    			if (shortPath._arr)
    			{
    				free(shortPath._arr);
    			}
    			//将最短路径保存到shortPath中
    			shortPath._arr =(DataType*)malloc(sizeof(DataType)* path->top);
    			memcpy(shortPath._arr, path->_arr, sizeof(DataType)*path->top);
    			shortPath.top = path->top;
    			shortPath.end = path->end;
    		}
    	}
    	//UP
    	cur._row -= 1;
    	if (MazeCheckIsAccess(cur))
    	{
    		MazeGetShortPath(cur, path);
    
    	}
    
    	//DOWN
    	cur = entry;
    	cur._row += 1;
    	if (MazeCheckIsAccess(cur) == 1)
    	{
    		MazeGetShortPath(cur, path);
    
    	}
    	//LEFT
    	cur = entry;
    	cur._col -= 1;
    	if (MazeCheckIsAccess(cur) == 1)
    	{
    		MazeGetShortPath(cur, path);
    
    	}
    
    	//RIGHT
    	cur = entry;
    	cur._col += 1;
    	if (MazeCheckIsAccess(cur) == 1)
    	{
    		MazeGetShortPath(cur, path);
    
    	}
    
    	//没有通路
    	StackPop(path);
    	
    }
    
    //带环迷宫
    int MazeCheckShortIsAccess(Pos next,Pos pos){
    	if(pos._row>=0&&pos._row<N
    		&&pos._col>=0&&pos._col<N
    		&&(maze[pos._row][pos._col]==1||
    		maze[pos._row][pos._col]>maze[next._row][next._col]))
    		return 1;
    	else
    		return 0;
    }
    
    void MazeGetShortPathCircle(Pos entry, Stack* path){
    
    	Pos cur , next , prev;
    	cur = next = entry;
    
    	if(StackEmpty(*path)==0){
    		maze[next._row][next._col] = 2;
    	}
    	else{
    		prev=StackTop(path);
    		maze[next._row][next._col]=maze[prev._row][prev._col]+1;
    	}
    	//栈保存坐标
    	StackPush(path, cur);
    
    	printf("[%d,%d]  ",cur._row,cur._col);
    
    	if (cur._col == N - 1)
    	{
    		printf("出口:[%d,%d]
    ", cur._row, cur._col);
    		if (!(StackEmpty(shortPath)) || StackSize(*path) < StackSize(shortPath))
    		{
    			//深浅拷贝问题
    			if (shortPath._arr)
    			{
    				free(shortPath._arr);
    			}
    			//将最短路径保存到shortPath中
    			shortPath._arr =(DataType*)malloc(sizeof(DataType)* path->top);
    			memcpy(shortPath._arr, path->_arr, sizeof(DataType)*path->top);
    			shortPath.top = path->top;
    			shortPath.end = path->end;
    		}
    	}
    
    	//UP
    	cur._row -= 1;
    	if (MazeCheckShortIsAccess(next,cur))
    	{
    		MazeGetShortPathCircle(cur, path);
    	}
    
    	//DOWN
    	cur = entry;
    	cur._row += 1;
    	if (MazeCheckShortIsAccess(next,cur) == 1)
    	{
    		MazeGetShortPathCircle(cur, path);
    	}
    
    	//LEFT
    	cur = entry;
    	cur._col -= 1;
    	if (MazeCheckShortIsAccess(next,cur) == 1)
    	{
    		MazeGetShortPathCircle(cur, path);
    	}
    
    	//RIGHT
    	cur = entry;
    	cur._col += 1;
    	if (MazeCheckShortIsAccess(next,cur) == 1)
    	{
    		MazeGetShortPathCircle(cur, path);
    	}
    	//没有通路
    	StackPop(path);
    }
    
    void TestMaze(){
    	Stack sk;
    	size_t i,n;
    	Pos entry={5,2};
    	StackInit(&sk);
    
    	MazePrint();
    	//printf("是否走出?  %d
    ",MazeGetPathR(entry));
    	//MazeGetShortPath(entry,&sk);
    	//MazePrint();	
    	//n=shortPath.top;
    	//for(i=0;i<n;i++){
    	//	printf("[%d,%d]  ",shortPath._arr[i]._row,shortPath._arr[i]._col);
    	//}
    	MazeGetShortPathCircle(entry,&sk);
    	MazePrint();
    }
    
    

  • 相关阅读:
    Flink命令行提交job (源码分析)
    Flink 中LatencyMarks延迟监控(源码分析)
    Flink中的CEP复杂事件处理 (源码分析)
    Flink中异步AsyncIO的实现 (源码分析)
    Flink中发送端反压以及Credit机制(源码分析)
    Flink中接收端反压以及Credit机制 (源码分析)
    Flink整合oozie shell Action 提交任务 带kerberos认证
    Flink中TaskManager端执行用户逻辑过程(源码分析)
    Flink的Job启动TaskManager端(源码分析)
    Flink中Idle停滞流机制(源码分析)
  • 原文地址:https://www.cnblogs.com/yongtaochang/p/13615362.html
Copyright © 2020-2023  润新知