用C语言找迷宫路径

给定一个二维数组,2为墙,0为路。找出所有路径并打印出来。

方法一:

用递归,打印出直观的图形来显示路径。

PS:要将走过的路径做一个标记,换成一个和墙与路不一样的值,当走到死路的时候要在回去时将道路改回原来的0.这样,我们就可以用上下左右四个方向进行递归,每到一次终点就打印一次,一共有几条路径,就打印几个二维数组出来。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void visited(int (*pa)[9], int i, int j)
{
	int n, m;
	pa[i][j] = 1;
	if(i == 7 && j == 7){
		printf("find path: \n");
		for(n=0; n<9; n++){
			for(m=0; m<9; m++){
				if(pa[n][m] == 1)
					printf("* ");
				else if(pa[n][m] == 2)
					printf("2 ");
				else 
					printf("0 ");
			}
			printf("\n");	
		}
	}

	if(pa[i-1][j] == 0)
		visited(pa, i-1, j);
	if(pa[i+1][j] == 0)
		visited(pa, i+1, j);
	if(pa[i][j-1] == 0)
		visited(pa, i, j-1);
	if(pa[i][j+1] == 0)
		visited(pa, i, j+1);

	pa[i][j] = 0;
}
int main()
{
	int a[9][9] = {2, 2, 2, 2, 2, 2, 2, 2, 2,
		       2, 0, 0, 0, 0, 0, 0, 0, 2,
		       2, 2, 2, 0, 2, 2, 2, 0, 2,
		       2, 2, 2, 0, 2, 2, 2, 0, 2,
		       2, 2, 2, 0, 2, 2, 2, 0, 2,
		       2, 0, 0, 0, 0, 0, 0, 0, 2,
		       2, 0, 2, 2, 2, 2, 2, 0, 2,
		       2, 0, 0, 0, 0, 0, 0, 0, 2,
		       2, 2, 2, 2, 2, 2, 2, 2, 2,
		};

	visited(a, 1, 1);

	return 0;
}


方法二:

用迭代求解。输出可以变为路径的坐标(可以定义一个结构体数组来存放路径坐标)。思路差不多。

#include<stdio.h> 
#include<conio.h>   
int migong[10][10]=  
{     
    {2,2,2,2,2,2,2,2,2,2}, 
    {2,0,0,0,0,0,0,2,2,2}, 
    {2,0,2,2,2,2,2,0,0,2}, 
    {2,0,2,0,0,0,0,0,0,2}, 
    {2,0,0,0,2,0,2,2,2,2}, 
    {2,2,2,2,0,0,2,2,2,2}, 
    {2,0,0,0,0,2,2,2,2,2}, 
    {2,0,2,2,0,0,2,2,2,2}, 
    {2,0,0,0,0,0,0,0,0,2}, 
    {2,2,2,2,2,2,2,2,2,2} 
}; 
int num; 
struct 
{ 
int x,y,d; 
}lj[100];//x,y分别为垂直和水平方向 
  
void start() 
{ 
int top=0,x,y,d,find;//d为设置方向,上下左右。find为设置找不找得到路 
lj[top].x=1; 
lj[top].y=1; 
migong[1][1]=-1; 
find=0;d=-1; 
  
while(top>-1){ 
if(lj[top].x==8&&lj[top].y==8)  
{ 
printf("迷宫路径如下:\n"); 
printf("start->"); 
for(x=0;x<=top;x++) 
{ 
printf("(%d,%d)-> ",lj[x].x,lj[x].y);//把找到的路径输出 
num++; 
if(num%8==0) 
printf("\n"); 
}  
printf("->end!\n"); 
} 
  
while(d<4&&find==0){ 
d++; 
switch(d){ 
case 0:x=lj[top].x-1; y=lj[top].y;  break;//方向为上 
case 1:x=lj[top].x;   y=lj[top].y+1;break;//方向为右 
case 2:x=lj[top].x+1; y=lj[top].y;  break;//方向为下 
case 3:x=lj[top].x;   y=lj[top].y-1;}//方向为左 
if(migong[x][y]==0) 
find=1; 
} 
  
if(find==1){     //判断是否找得到 
lj[top].d=d; 
top++; 
lj[top].x=x; 
lj[top].y=y; 
d=-1;find=0;     //重新调整方向 
migong[x][y]=-1;} 
else{ 
migong[lj[top].x][lj[top].y]=0; 
top--;d=lj[top].d; //找不到的话退栈 
} 
} 
} 
  
void main() 
{ 
start(); 
getch(); 
}  



  • 9
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: Hi there, I need help writing a program in C language to find the shortest path in a maze. Can you help me with that? Of course! I'd be happy to help you write a program to find the shortest path in a maze using C language. Can you give me a little more information about the maze and how you want to solve it? For example, what is the format of the maze, and what algorithms are you considering for finding the shortest path? ### 回答2: 在C语言中,可以使用图的最短路径算法来解决迷宫最短路径问题。以下是一种基于深度优先搜索(DFS)的解决方法的示例代码: ```c #include <stdio.h> #include <stdbool.h> #define N 5 // 迷宫的大小 #define WALL 0 #define PATH 1 #define VISITED 2 int maze[N][N] = { {1, 1, 0, 1, 1}, {1, 0, 0, 0, 1}, {1, 1, 1, 0, 1}, {0, 0, 1, 1, 1}, {1, 1, 1, 1, 1} }; int move[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; bool isValidMove(int x, int y) { return (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] != WALL && maze[x][y] != VISITED); } bool findShortestPath(int x, int y) { if (x == N - 1 && y == N - 1) { maze[x][y] = VISITED; return true; } maze[x][y] = VISITED; // 标记当前位置为已访问 for (int i = 0; i < 4; i++) { int newX = x + move[i][0]; int newY = y + move[i][1]; if (isValidMove(newX, newY)) { if (findShortestPath(newX, newY)) { return true; } } } return false; } int main() { if (findShortestPath(0, 0)) { printf("到了迷宫的最短路径!\n"); } else { printf("没有迷宫的最短路径!\n"); } return 0; } ``` 以上代码中,我们使用了一个5x5的迷宫作为示例。其中1代表可以通过的路径,0代表墙壁。算法通过深度优先搜索从起点开始寻路径,如果到终点,则返回true。最后,根据返回的结果输出相应的提示消息。 请注意,以上代码只能迷宫的一条最短路径,如果存在多条最短路径,则只能到其中的一条。如果需要到所有最短路径,则需要使用其他更复杂的算法,如Dijkstra算法或A*算法。 ### 回答3: 要使用C语言编写迷宫最短路径代码,可以采用广度优先搜索算法。以下是一个简单的示例代码: ``` #include <stdio.h> #include <stdlib.h> #define ROWS 10 // 迷宫行数 #define COLS 10 // 迷宫列数 int maze[ROWS][COLS] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, {1, 1, 1, 0, 1, 0, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 0, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 0, 1, 1, 0, 1}, {1, 0, 0, 0, 1, 0, 1, 0, 0, 1}, {1, 0, 1, 0, 0, 0, 0, 1, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; typedef struct { int row; int col; int distance; } Position; Position queue[ROWS * COLS]; int front = -1; int rear = -1; void enqueue(Position pos) { if (rear == ROWS * COLS - 1) { return; } if (front == -1) { front = 0; } rear++; queue[rear] = pos; } Position dequeue() { if (front == -1 || front > rear) { Position empty = {-1, -1, -1}; return empty; } Position pos = queue[front]; front++; return pos; } int isGoal(Position pos) { return (pos.row == ROWS-1 && pos.col == COLS-1); } int isValid(int row, int col) { return (row >= 0 && row < ROWS && col >= 0 && col < COLS && maze[row][col] == 0); } int findShortestPath() { Position start = {0, 0, 0}; enqueue(start); while (1) { Position current = dequeue(); if (current.row == -1 && current.col == -1) { break; // 队列为空,搜索结束 } if (isGoal(current)) { return current.distance; // 到了最短路径 } // 向四个方向扩展 int row = current.row; int col = current.col; int distance = current.distance; maze[row][col] = -1; // 标记为已访问 // 上方 if (isValid(row-1, col)) { Position up = {row-1, col, distance+1}; enqueue(up); } // 下方 if (isValid(row+1, col)) { Position down = {row+1, col, distance+1}; enqueue(down); } // 左方 if (isValid(row, col-1)) { Position left = {row, col-1, distance+1}; enqueue(left); } // 右方 if (isValid(row, col+1)) { Position right = {row, col+1, distance+1}; enqueue(right); } } return -1; // 未路径 } int main() { int shortestPath = findShortestPath(); if (shortestPath != -1) { printf("最短路径长度为: %d\n", shortestPath); } else { printf("无法到最短路径\n"); } return 0; } ``` 上述代码中,使用二维数组表示迷宫,1代表墙壁,0代表通路。使用队列保存待搜索的位置,基于广度优先搜索算法逐步扩展位置,直到到终点或队列为空。最后返回最短路径的长度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值