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
  • 相关阅读:
    数据库日志文件太大的解决方法及原理
    邮件发送组件
    DataConnectionDialog 旧事重提
    从LINQ实例解析LINQ的另类用法,解决多条件组合问题
    重开BLOG.
    找个搜索结果总数原来可以用到这么多的技术
    Discuz3.2与Java 项目整合单点登陆
    一点感触
    Java 处理word文档后在前端展示
    大数据: 完全分布式Hadoop集群HBase安装
  • 原文地址:https://www.cnblogs.com/ygdblogs/p/4935676.html
Copyright © 2011-2022 走看看