• 关于迷宫


    在迷宫中找一条通路,如果不通,原路返回到岔口,继续上下左右寻找,每到一处都要做上标记:

    如下算法:

    #include <iostream>
    #include <stack>
    #include <assert.h>
    using namespace std;
    
    //从TXT文件中获取迷宫
    void GetMazeMap(int* a, int row, int col)
    {
    	FILE* fOut = fopen("MazeMap.txt", "r");
    	assert(fOut);
    	for (int i = 0 ; i < row; ++i)
    	{
    		for (int j = 0; j < col;)
    		{
    			//a[i][j] = fgetc(fOut) - '0';
    			char ch = fgetc(fOut);
    			if (ch == '0' || ch == '1')
    			{
    				a[i*col+j] = ch - '0';
    				++j;
    			}
    		}
    	}
    	fclose(fOut);
    }
    
    //打印迷宫
    void PrintMaze(int* a, int row, int col)
    {
    	for (int i = 0 ; i < row; ++i)
    	{
    		for (int j = 0; j < col; ++j)
    		{
    			cout<<a[i*col+j]<<" ";
    		}
    		cout<<endl;
    	}
    	cout<<endl;
    }
    
    //迷宫入口位置
    struct Pos
    {
    	int row;
    	int col;
    };
    
    //检查入口是否存在
    bool CheckIsAccess(int* a, int rowSize, int colSize, Pos cur)
    {
    	if (cur.row < rowSize && cur.col < colSize
    		&& a[cur.row*colSize + cur.col] == 0)
    	{
    		return true;
    	}
    
    	return false;
    }
    
    //得到迷宫的路线
    stack<Pos> GetMazePath(int* a, int rowSize, int colSize, Pos entry)
    {
    	stack<Pos> paths;
    	Pos cur = entry;
    	paths.push(entry);
    
    	while (!paths.empty())
    	{
    		Pos cur = paths.top();
    		Pos next = cur;
    
    		if (next.row == rowSize -1)
    		{
    			return paths;
    		}
    
    		// 左
    		next = cur;
    		next.col--;
    		if (CheckIsAccess(a, 10, 10, next))
    		{
    			a[next.row * colSize + next.col] = 2;
    			paths.push(next);
    			continue;
    		}
    
    		// 右
    		next = cur;
    		next.col++;
    		if (CheckIsAccess(a, 10, 10, next))
    		{
    			a[next.row * colSize + next.col] = 2;
    			cur = next;
    			paths.push(next);
    			continue;
    		}
    
    		// 上
    		next.row--;
    		if (CheckIsAccess(a, 10, 10, next))
    		{
    			a[next.row * colSize + next.col] = 2;
    			paths.push(next);
    			continue;
    		}
    
    		// 下
    		next = cur;
    		next.row++;
    		if (CheckIsAccess(a, 10, 10, next))
    		{
    			a[next.row * colSize + next.col] = 2;
    			paths.push(next);
    			continue;
    		}
    
    		Pos top = paths.top();
    		a[top.row* colSize + top.col] = 3;
    		paths.pop();
    	}
    
    	cout<<"没有"<<endl;
    }
    
    void TestMaze()
    {
    	int a[10][10] = {};
    	GetMazeMap((int*)a, 10, 10);
    	PrintMaze((int*)a, 10, 10);
    
    	Pos entry = {2,0};
    	GetMazePath((int*)a,10,10,entry);
    	PrintMaze((int*)a, 10, 10);
    }
    
    int main()
    {
    	TestMaze();
    	return 0;
    }


  • 相关阅读:
    微擎开发笔记
    Array对象的方法实现(1)----Array.prototype.push和Array.prototype.concat(实现常规参数的功能)
    ThinkCMF 5 基础门户CMS框架的模板widget标签实现
    [转]mysql为什么默认有那么多root用户,还有用户名为空的用户?
    PHP 5.4特性 trait
    道破人性
    c#使用easyhook库进行API钩取
    黑马eesy_15 Vue:04.综合案例(前端Vue实现)
    黑马eesy_15 Vue:04.Vue案例(ssm环境搭建)
    黑马eesy_15 Vue:03.生命周期与ajax异步请求
  • 原文地址:https://www.cnblogs.com/melons/p/5791883.html
Copyright © 2020-2023  润新知