[ C语言 ] 迷宫 迷宫生成器 [ 递归与搜索 ]

时间:2022-09-08 19:48:29

【原创】转载请注明出处 【浙江大学 程序设计专题】

【地图求解器】

本题目要求输入一个迷宫地图,输出从起点到终点的路线。

基本思路是从起点(Sx,Sy)每次枚举该格子上下左右四个方向,直到走到终点(Tx,Ty)。
方法一:如果使用递归方法,则可以使用深度优先搜索算法,但此方法不能保证答案步数最优。
方法二: 如果要求答案步数最少,则使用广度优先搜索算法,但此方法通常不使用递归函数实现。

DFS版代码

 #include <stdio.h>
#include <string.h> /*Header which contains the output function*/
#include "prnt_route.h" int n,m;
int visited[][];
struct PII from[][];
/*Save the route.*/ const int dx[]={-,,,},dy[]={,,,-}; char Map[][]; int Dfs(const int Sx,const int Sy, const int Tx,const int Ty)
{
if(Sx==Tx && Sy==Ty) return ;
int i;
for(i=;i<;++i)/*Search four directions*/
{
int tx=Sx+dx[i],ty=Sy+dy[i];/*tx,ty refers to the next grid.*/
if(tx>= && ty>= && tx<n && ty<m &&
!visited[tx][ty] && Map[tx][ty]!='#')
/*check if (tx,ty) is reachable*/
{
visited[tx][ty]=;
if(Dfs(tx,ty,Tx,Ty))
{
/*Route is found.*/
from[tx][ty]=make_pair(Sx,Sy);
return ;
}
}
}
return ;
} int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int i,j,Sx,Sy,Tx,Ty;
scanf("%d%d",&n,&m);
Sx=Sy=,Tx=n-,Ty=m-;
for(i=;i<n;++i) scanf("%s",Map[i]); /*Find the starting and ending points.*/
for(i=;i<n;++i)
for(j=;j<m;++j)
if(Map[i][j]=='S') Sx=i,Sy=j;
else if(Map[i][j]=='T') Tx=i,Ty=j; /*Dfs will return 1 if a solution is found, 0 otherwise.*/
if(Dfs(Sx,Sy,Tx,Ty)) Print_Ans(Sx,Sy,Tx,Ty);
else printf("No Solution.\n");
return ;
}

BFS版代码

 #include <stdio.h>
#include <stdlib.h>
#include <string.h> /*Header contains the outout function*/
#include "prnt_route.h" int n,m;
int visited[][];
struct PII from[][];
/*Save the route*/ char Map[][]; int Bfs(const int Sx,const int Sy, const int Tx,const int Ty)
{
struct PII Q[(n+)*(m+)];/*Queue for Bfs*/
int front=,back=;/*head and tail pointer of the queue*/
memset(visited,,sizeof(visited));
memset(from,,sizeof(from));
Q[back++]=make_pair(Sx,Sy);/*push the starting point*/
visited[Sx][Sy]=;
while(front!=back && !visited[Tx][Ty])
{
struct PII t=Q[front++];/*Pop out*/
/*Search four directions*/
if(t.x> && !visited[t.x-][t.y] && Map[t.x-][t.y]!='#')/*up*/
{
Q[back++]=make_pair(t.x-,t.y);/*push*/
visited[t.x-][t.y]=;
from[t.x-][t.y]=make_pair(t.x,t.y);
}
if(t.x<n- && !visited[t.x+][t.y] && Map[t.x+][t.y]!='#')/*down*/
{
Q[back++]=make_pair(t.x+,t.y);/*push*/
visited[t.x+][t.y]=;
from[t.x+][t.y]=make_pair(t.x,t.y);
}
if(t.y> && !visited[t.x][t.y-] && Map[t.x][t.y-]!='#')/*left*/
{
Q[back++]=make_pair(t.x,t.y-);/*push*/
visited[t.x][t.y-]=;
from[t.x][t.y-]=make_pair(t.x,t.y);
}
if(t.y<m- && !visited[t.x][t.y+] && Map[t.x][t.y+]!='#')/*right*/
{
Q[back++]=make_pair(t.x,t.y+);/*push*/
visited[t.x][t.y+]=;
from[t.x][t.y+]=make_pair(t.x,t.y);
}
}
return visited[Tx][Ty];
} int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int i,j,Sx,Sy,Tx,Ty;
scanf("%d%d",&n,&m);
Sx=Sy=,Tx=n-,Ty=m-;
for(i=;i<n;++i) scanf("%s",Map[i]); /*Find the starting and ending points*/
for(i=;i<n;++i)
for(j=;j<m;++j)
if(Map[i][j]=='S') Sx=i,Sy=j;
else if(Map[i][j]=='T') Tx=i,Ty=j; /*Bfs will return 1 if route is found.*/
if(Bfs(Sx,Sy,Tx,Ty)) Print_Ans(Sx,Sy,Tx,Ty);
else printf("No Solution.\n");
return ;
}

print_route.h(两篇代码公用)

 /*Output function*/

 #include <stdio.h>

 struct PII { int x,y; };/*coordinate container*/
struct PII make_pair(const int x,const int y)
{ struct PII t; t.x=x,t.y=y; return t; } extern int n,m;
extern int visited[][];
extern struct PII from[][]; extern char Map[][]; void Print_Ans(const int Sx,const int Sy,const int Tx,const int Ty)
{
int x=Tx,y=Ty,dir=,td,i,j,tempx,tempy;
/*'dir' is the current direction, while 'td' is the last direcion.*/
while(x!=Sx || y!=Sy)
{
/*judge the direction*/
if(from[x][y].y==y && from[x][y].x==x+) Map[x][y]='^',td=;
if(from[x][y].y==y && from[x][y].x==x-) Map[x][y]='V',td=;
if(from[x][y].x==x && from[x][y].y==y+) Map[x][y]='<',td=;
if(from[x][y].x==x && from[x][y].y==y-) Map[x][y]='>',td=;
/*decide which conner character should be output.*/
if(dir== && td==) Map[x][y]='}';
if(dir== && td==) Map[x][y]='}';
if(dir== && td==) Map[x][y]='{';
if(dir== && td==) Map[x][y]='{';
if(dir== && td==) Map[x][y]=']';
if(dir== && td==) Map[x][y]=']';
if(dir== && td==) Map[x][y]='[';
if(dir== && td==) Map[x][y]='[';
tempx=x,tempy=y; dir=td;
x=from[tempx][tempy].x;
y=from[tempx][tempy].y;
}
Map[Sx][Sy]='S';
Map[Tx][Ty]='T'; for(i=;i<n;++i)
{
for(j=;j<m;++j)
{
/*output with special chars.*/
if(Map[i][j]=='#') printf("█");
else if(Map[i][j]=='.') printf(" ");
else if(Map[i][j]=='^') printf("↑");
else if(Map[i][j]=='V') printf("↓");
else if(Map[i][j]=='<') printf("←");
else if(Map[i][j]=='>') printf("→");
else if(Map[i][j]=='+') printf("╋");
else if(Map[i][j]=='[') printf("┏");
else if(Map[i][j]==']') printf("┓");
else if(Map[i][j]=='{') printf("┗");
else if(Map[i][j]=='}') printf("┛");
else printf("☆");
}
printf("\n");
}
return ;
}

标准ascii字符版print_route.h

 #include <stdio.h>

 struct PII { int x,y; };
struct PII make_pair(const int x,const int y)
{ struct PII t; t.x=x,t.y=y; return t; } extern int n,m;
extern int visited[][];
extern struct PII from[][]; extern char Map[][]; void Print_Ans(const int Sx,const int Sy,const int Tx,const int Ty)
{
int x=Tx,y=Ty,dir=,td,i,tempx,tempy;
while(x!=Sx || y!=Sy)
{
if(from[x][y].y==y && from[x][y].x==x+) Map[x][y]='|',td=;
if(from[x][y].y==y && from[x][y].x==x-) Map[x][y]='|',td=;
if(from[x][y].x==x && from[x][y].y==y+) Map[x][y]='-',td=;
if(from[x][y].x==x && from[x][y].y==y-) Map[x][y]='-',td=;
if(dir== && td==) Map[x][y]='+';
if(dir== && td==) Map[x][y]='+';
if(dir== && td==) Map[x][y]='+';
if(dir== && td==) Map[x][y]='+';
if(dir== && td==) Map[x][y]='+';
if(dir== && td==) Map[x][y]='+';
if(dir== && td==) Map[x][y]='+';
if(dir== && td==) Map[x][y]='+';
tempx=x,tempy=y; dir=td;
x=from[tempx][tempy].x;
y=from[tempx][tempy].y;
}
Map[Sx][Sy]='S';
Map[Tx][Ty]='T'; for(i=;i<n;++i)printf("%s\n",Map[i]);
return ;
}

【地图生成器】

方法,使用DFS,每次随机一个方向进行扩展。但这样可能导致一条路径过长,岔路过短。

所以加入三个参数:

  1.搜索过程中有一定几率保持上一次的方向,称为RATE_KEEP_DIR

  2.搜索过程中有一定几率停止当前道路的搜索,直接return,这个参数称为RETURN_RATE

  3.每条链有一个最大长度,称为TWIST_RATE

通过修改这三个参数,可以改变地图的性态。

生成器代码使用C++,编译需要加-std=c++11参数,代码如下

 //Compile with -std=c++11
#include <bits/stdc++.h> using namespace std; const int RATE_KEEP_DIR=;
const int TWIST_RATE=;
const int RETURN_RATE=; int n,m,Sx,Sy,Tx,Ty,Max;
char Map[][];
typedef pair<int,int> PII;
const int dx[]={-,,,},dy[]={,,,-};
const int Dx[]={-,,,},Dy[]={,,,-}; mt19937 RND(time()); //Check if (x,y) has a neighbour can go.
bool Check(const int x,const int y)
{
for(int i=;i<;++i)
{
int nx=x+dx[i],ny=y+dy[i],Nx=x+Dx[i],Ny=y+Dy[i];
if(Nx>= && Nx<n && Ny>= && Ny<m && (Map[Nx][Ny]=='#' && Map[nx][ny]=='#'))return true;
} return false;
} void Dfs(const int x,const int y,const int depth,int Lim,const int last_dir)
{
if(depth>Max)Tx=x,Ty=y,Max=depth;//find a longest route
if(depth>Lim) return ;
Map[x][y]='.';
while(Check(x,y))
{
int t=RND()%;//random direction
if(RND()%<RATE_KEEP_DIR) t=last_dir;//chance of keeping direction.
int nx=x+dx[t],ny=y+dy[t],Nx=x+Dx[t],Ny=y+Dy[t];
if(nx< || nx>n- || ny< || ny>m- || Map[nx][ny]!='#')continue;
if(Nx< || Nx>n- || Ny< || Ny>m- || Map[Nx][Ny]!='#')continue;
if(Nx== || Nx==n- || Ny== || Ny==m-) { Map[nx][ny]='.'; continue; }
Map[nx][ny]='.'; Map[Nx][Ny]='.';
Dfs(Nx,Ny,depth+,Lim,t); //chance of returning directly, without expanding, for more branch roads
if((int)(RND()%)<(min(n,m)<?:RETURN_RATE)) return ; Lim=depth+max(min(n,m)/TWIST_RATE,);
}
return ;
} int main()
{
freopen("in.txt","w",stdout);
scanf("%d%d",&n,&m);
printf("%d %d\n",n,m);
for(int i=;i<n;++i) for(int j=;j<m;++j) Map[i][j]='#';
Sx=Sy=;
// the length limit of each branch road.
Dfs(Sx,Sy,,max(min(n,m)/TWIST_RATE,),);
//set starting and ending points.
Map[Sx][Sy]='S';
Map[Tx][Ty]='T';
for(int i=;i<n;++i) printf("%s\n",Map[i]);
return ;
}

以下C++代码可以将生成器生成的地图转化为全角符号,便于查看

 #include <bits/stdc++.h>

 int n,m;
char Map[][]; using namespace std; int main()
{
freopen("in.txt","r",stdin);
freopen("view.txt","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<n;++i)
scanf("%s",Map[i]);
for(int i=;i<n;++i)
{
for(int j=;j<m;++j)
{
if(Map[i][j]=='#') printf("█");
else if(Map[i][j]=='.') printf(" ");
else printf("☆");
}
printf("\n");
}
return ;
}

[ C语言 ] 迷宫 迷宫生成器 [ 递归与搜索 ]的更多相关文章

  1. AI-随机迷宫&amp&semi;迷宫求解

    本文记录了,人工智能中简单的搜索策略中的路径搜索策略中的A*算法,来实现迷宫寻路的问题.(这只是一次本人的课外作业) 完整的程序源码已经发送到我的Git.这里只记录了我的思路和感想以及收获. 产生随机 ...

  2. python 递归深度优先搜索与广度优先搜索算法模拟实现

    一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件2.找出这一次和上一次关系3.假设当前 ...

  3. fastposter发布1&period;4&period;2 跨语言的海报生成器

    fastposter发布1.4.2 跨语言的海报生成器 fastposter发布1.4.2 跨语言的海报生成器,一分钟完成海报开发 future: 完善docker镜像 引入异步asyncio 升级p ...

  4. &lbrack;迷宫中的算法实践&rsqb;迷宫生成算法&mdash&semi;&mdash&semi;递归分割算法

    Recursive division method        Mazes can be created with recursive division, an algorithm which wo ...

  5. ytu 1980&colon;小鼠迷宫问题(DFS 深度优先搜索)

     小鼠迷宫问题 Time Limit: 2 Sec  Memory Limit: 64 MB Submit: 1  Solved: 1 [Submit][Status][Web Board] Desc ...

  6. &lbrack;Swust OJ 409&rsqb;--小鼠迷宫问题&lpar;BFS&plus;记忆化搜索&rpar;

    题目链接:http://acm.swust.edu.cn/problem/409/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  7. poj3984《迷宫问题》暑假集训-搜索进阶

    K - 迷宫问题 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  8. BZOJ2246 &lbrack;SDOI2011&rsqb;迷宫探险 【记忆化搜索dp &plus; 概率】

    题目 输入格式 输出格式 仅包含一个数字,表示在执行最优策略时,人物活着走出迷宫的概率.四舍五入保留3位小数. 输入样例 4 3 3 2 .$. A#B A#C @@@ 143 37 335 85 9 ...

  9. POJ 3984:迷宫问题 bfs&plus;递归输出路径

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11844   Accepted: 7094 Description ...

随机推荐

  1. C&plus;&plus;虚函数、赋值兼容原则

    #include <iostream.h> class A { public: void f1() { cout << "a" << endl; ...

  2. Usaco 2010 Dec Gold Exercise&lpar;奶牛健美操&rpar;

    /*codevs 3279 二分+dfs贪心检验 堆版本 re一个 爆栈了*/ #include<cstdio> #include<queue> #include<cst ...

  3. Android 使用DexClassLoader要执行其他apk方法

    Android在apk文件dex文件是java编译出来.class次打包,当然在打包之前会利用自己的协议做一些数据处理,比如优化函数表和变量表.在java程序中是使用classloader来载入这些编 ...

  4. redis之sentinel概述

    一.配置sentinel 修改的是这条: 对应: 上面那条配置需要注意:<master-name>:监控主节点的名称 <ip>:监控主节点的ip   <redis-por ...

  5. Automated generation of test oracles using a model-driven approach

    一.基本信息 标题:Automated generation of test oracles using a model-driven approach 时间:2013 出版源:Information ...

  6. IHttpHandler处理请求api

    使用IHttpHandler处理请求,实现webapi功能. 研究asp.net管道处理事件后,可用此法实现webapi功能. 测试环境 VS2017 WIN10 IIS10 集成模式 关键接口类两个 ...

  7. spark 将dataframe数据写入Hive分区表

    从spark1.2 到spark1.3,spark SQL中的SchemaRDD变为了DataFrame,DataFrame相对于SchemaRDD有了较大改变,同时提供了更多好用且方便的API.Da ...

  8. websocket 缺点

    当时用 python 做的服务器,后来回去想再工作项目上用,但新的技术升级,随之而来还是要解决很多非技术问题, 服务器带宽,并发服务器性能方方面面考虑之后还是没有用上,十分可惜, 一个新的技术推动,尤 ...

  9. CSS文档流、块级元素、内联元素

    CSS文档流与块级元素(block).内联元素(inline),之前翻阅不少书籍,看过不少文章, 看到所多的是零碎的CSS布局基本知识,比较表面.看过O'Reilly的<CSS权威指南>, ...

  10. Javascript各种事件汇总

    https://www.cnblogs.com/diligenceday/p/4190173.html#undefined https://www.cnblogs.com/starof/p/40663 ...