zoukankan      html  css  js  c++  java
  • 迷宫

    要求:

    以队列和栈的基本操作对于随机迷宫找到入口到出口的最短路径的查找并输出路径

    思路:

    BFS的实现

    #include "queue.h"
    #include "stack.h"
    #include <stdlib.h>
    
    #define SIZE 100
    #define SHORT 299
    
    void Initmaze(int, int);/*初始化迷宫*/
    void printmap(int, int);/*输出*/
    int BFS(int, int, int, int);
    void printPath(int, int); 
    
    ElemType pre[SIZE][SIZE];
    int map[SIZE][SIZE];
    ElemType move[8]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
    
    int main()
    {
    	int n, m;
    	while(scanf("%d%d", &n, &m) != EOF && n &&m)
    	{
    		memset(map, 0, sizeof(map));
    		Initmaze(n, m);
    		printmap(n, m);
    		if(BFS(n,m,1,1)){
    		    printf("YES
    ");
    		    printPath(n,m);
    		}
    		else{
    			printf("NO Path
    ");
    		}
    		printmap(n, m);
    	}
    	return 0;
    }
    
    void Initmaze(int n, int m)
    {
    	for(int i = 0; i< n+2; i++)
    	{
    		for(int j = 0; j< m+2; j++)
    		{
    			if(i == 0 || j == 0 || i == n+1 || j == m+1)
    			{
    				map[i][j] = 1;
    			}
    			else if((i == 1 && j == 1) || (i == n && j == m)){
    				map[i][j] = 0;
    			}
    			else{
    				int p = rand()%3;
    				if(p == 2)
    				{
    					map[i][j] = 1;
    				}
    			}
    		}
    	}
    }
    
    void printPath(int row, int column)
    {
    	ElemType temp;         //保存位置 
        Stack s;			//保存路径序列
    	s = CreateStack(100);
    	MakeEmpty(s);
        temp.x = row;
        temp.y = column;
        while(temp.x != 1 || temp.y != 1)
    	{
    		Push(temp, s);
            temp = pre[temp.x][temp.y];
        }
        printf("(1, 1)");
        map[1][1] = SHORT;
        while(!IsEmpty(s)){
            temp = Top(s);
            printf(" (%d, %d) ", temp.x, temp.y);
            map[temp.x][temp.y] = SHORT;
            Pop(s);
        }
        printf("
    ");
    }
    
    int BFS(int row,int column,int x,int y)
    {
    	if(x == row && y == column)
    		return 1;
    	LinkQueue q;
    	InitQueue(&q);
    	ElemType now;
    	now.x = x;
        now.y = y;
        Insert(q,now);
        map[now.x][now.y] = -1;
        while(!isEmpty(q))
        {
        	DeQueue(q, &now);
            for(int i=0; i<8; i++){
                if(now.x + move[i].x == row && now.y + move[i].y == column){
                    map[now.x + move[i].x][now.y + move[i].y] = -1;
                    pre[row][column] = now;
                    return 1;
                }
                if(map[now.x + move[i].x][now.y + move[i].y] == 0){
                    ElemType temp;     //下个位置
                    temp.x = now.x + move[i].x;
                    temp.y = now.y + move[i].y;
                    Insert(q,temp);
                    map[temp.x][temp.y] = -1;
                    pre[temp.x][temp.y] = now;
    
                }
            }
        }
    }
    
    void printmap(int n, int m)
    {
    	for(int i = 0; i< n+2; i++)
    	{
    		for(int j = 0; j< m+2; j++)
    		{
    			if(map[i][j] == 1)
    				printf("#");
    			else if(map[i][j] == SHORT){
    				printf("^");
    			}
    			else{
    				printf(" ");
    			}
    		}
    		printf("
    ");
    	}
    }
    
    转载请注明出处:http://www.cnblogs.com/ygdblogs
  • 相关阅读:
    Python os.fchdir() 方法
    Python os.dup2() 方法
    JavaScript——三
    JavaScript——二
    JavaScript函数console、this关键字
    JavaScript——DOMM
    JavaScript——浏览器检查
    JavaScript——匿名函数和闭包
    JavaScript——原型
    JavaScript——面向对象与原型
  • 原文地址:https://www.cnblogs.com/ygdblogs/p/4935676.html
Copyright © 2011-2022 走看看