迷宫问题c语言

示例1

输入:

5 5
0 1 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

复制输出:

(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4)

示例2

输入:

5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 1
0 1 1 1 0
0 0 0 0 0

复制输出:

(0,0)
(1,0)
(2,0)
(3,0)
(4,0)
(4,1)
(4,2)
(4,3)
(4,4)

核心算法是用GetMazePath递归来探索路径

#include<stdlib.h>
#include<assert.h>
#include <stdio.h>
typedef struct Position
{
	int row;
	int col;
}PT;
typedef PT ElemType;
typedef struct Stack
{
	int top;//栈顶
	ElemType* a;//后续使用动态开辟空间
	int capacity;//容量
}ST;
void StackInit(ST* ps)//初始化
{
	assert(ps);
	ps->top = 0;
	ps->a = NULL;
	ps->capacity = 0;
}
void StackPush(ST* ps, ElemType x)//增
{
	assert(ps);
	//检查空间,如果满了就二倍增容
	//如果是第一次增容则先开辟四个空间
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : (2 * ps->capacity);
		ElemType* tmp = (ElemType*)realloc(ps->a, sizeof(ST) * newcapacity);
		//如果ps->a为NULL,则此时realloc的作用与malloc一样
		if (tmp == NULL)
		{
			printf("realloc fail!\n");
			exit(0);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;

	}
	ps->a[ps->top] = x;
	ps->top++;
}
bool StackEmpty(ST* ps)//判断是否为空
{
	assert(ps);
	if (ps->top == 0)//为空
	{
		return true;
	}

	else//不为空
	{
		return false;
	}
}
void StackPop(ST* ps)//删
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
	//printf("栈顶数据删除成功\n");
}

ElemType StackTop(ST* ps)//查看栈顶
{
	assert(ps);
	//找栈顶的话得保证指向的空间不为空
	assert(!StackEmpty(ps));
	//此时的top-1就是栈顶数据
	return ps->a[ps->top - 1];
}
void StackDestory(ST* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
	}
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}
bool IsPass(int** maze,PT pos, int n, int m)
{
	if (pos.row >= n || pos.row < 0 || pos.col < 0 || pos.col >= m)
	{
		return false;
	}

	if (maze[pos.row][pos.col] == 0)
	{
		return true;
	}

	else
	{
		return false;
	}
}
ST path;
bool GetMazePath(int** maze, int n, int m, PT cur)
{
	StackPush(&path, cur);
	if (cur.row == n - 1 && cur.col == m - 1)
	{
		return true;
	}
	
	PT next = cur;
	maze[cur.row][cur.col] = 2;
	//上
	next = cur;
	next.row -= 1;
	if (IsPass(maze, next, n, m))
	{
		if (GetMazePath(maze, n, m, next))
		{
			return true;
		}
	}

	//下
	next = cur;
	next.row += 1;
	if (IsPass(maze, next, n, m))
	{
		if (GetMazePath(maze, n, m, next))
		{
			return true;
		}
	}

	//左
	next = cur;
	next.col -= 1;
	if (IsPass(maze, next, n, m))
	{
		if (GetMazePath(maze, n, m, next))
		{
			return true;
		}
	}

	//右
	next = cur;
	next.col += 1;
	if (IsPass(maze, next, n, m))
	{
		if (GetMazePath(maze, n, m, next))
		{
			return true;
		}
	}
	StackPop(&path);
	return false;
}
void PrintPath()
{
	ST _path;
	StackInit(&_path);
	while (!StackEmpty(&path))
	{
		StackPush(&_path, StackTop(&path));
		StackPop(&path);
	}

	while (!StackEmpty(&_path))
	{
		PT top = StackTop(&_path);
		StackPop(&_path);
		cout << "(" << top.row << "," << top.col << ")" << endl;
	}
	StackDestory(&_path);
}
int main()
{
	int row = 0, col = 0;
	StackInit(&path);

	while (cin >> row >> col)
	{
		int** maze = new int* [row];
		int i = 0, j = 0;
		for (i = 0; i < row; i++)
		{
			maze[i] = new int[col];
		}

		for (i = 0; i < row; i++)
		{
			for (j = 0; j < col; j++)
			{
				cin >> maze[i][j];
			}
		}

		PT entery;
		entery.row = 0, entery.col = 0;
		if (GetMazePath(maze, row, col, entery))
		{
			PrintPath();
			cout << "找到通路" << endl;
		}
		else
		{
			cout << "未找到通路" << endl;
		}

		for (i = 0; i < row; i++)
		{
			delete[] maze[i];
		}

		delete[] maze;
		maze = NULL;
	}
	StackDestory(&path);
	return 0;
}

图的查找升级版!等以后有能力再解决

输入 #1

8
2 3 13
2 6  6
3 5  7
4 4 14
5 2 21
5 6  4
6 3 15
7 2 14
0 0  0

 输出 #1

67

typedef int ElemType;
typedef struct Stack
{
	int top;//栈顶
	ElemType* a;//后续使用动态开辟空间
	int capacity;//容量
}ST;
typedef struct position
{
	int row;
	int col;
}PT;
void StackInit(ST* ps)//初始化
{
	assert(ps);
	ps->top = 0;
	ps->a = NULL;
	ps->capacity = 0;
}
void StackPush(ST* ps, ElemType x)//增
{
	assert(ps);
	//检查空间,如果满了就二倍增容
	//如果是第一次增容则先开辟四个空间
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : (2 * ps->capacity);
		ElemType* tmp = (ElemType*)realloc(ps->a, sizeof(ST) * newcapacity);
		//如果ps->a为NULL,则此时realloc的作用与malloc一样
		if (tmp == NULL)
		{
			printf("realloc fail!\n");
			exit(0);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;

	}
	ps->a[ps->top] = x;
	ps->top++;
}
bool StackEmpty(ST* ps)//判断是否为空
{
	assert(ps);
	if (ps->top == 0)//为空
	{
		return true;
	}

	else//不为空
	{
		return false;
	}
}
void StackPop(ST* ps)//删
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
	//printf("栈顶数据删除成功\n");
}
ElemType StackSize(ST* ps)//判断栈的长度
{
	assert(ps);
	return ps->top;
}
ElemType StackTop(ST* ps)//查看栈顶
{
	assert(ps);
	//找栈顶的话得保证指向的空间不为空
	assert(!StackEmpty(ps));
	//此时的top-1就是栈顶数据
	return ps->a[ps->top - 1];
}
void StackDestory(ST* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
	}
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}


//第二个栈
typedef PT ElemType2;
typedef struct Stack2
{
	int top;//栈顶
	ElemType2* a;//后续使用动态开辟空间
	int capacity;//容量
}ST2;
void StackInit(ST2* ps)//初始化
{
	assert(ps);
	ps->top = 0;
	ps->a = NULL;
	ps->capacity = 0;
}
void StackPush(ST2* ps, ElemType2 x)//增
{
	assert(ps);
	//检查空间,如果满了就二倍增容
	//如果是第一次增容则先开辟四个空间
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : (2 * ps->capacity);
		ElemType2* tmp = (ElemType2*)realloc(ps->a, sizeof(ST2) * newcapacity);
		//如果ps->a为NULL,则此时realloc的作用与malloc一样
		if (tmp == NULL)
		{
			printf("realloc fail!\n");
			exit(0);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;

	}
	ps->a[ps->top] = x;
	ps->top++;
}
bool StackEmpty(ST2* ps)//判断是否为空
{
	assert(ps);
	if (ps->top == 0)//为空
	{
		return true;
	}

	else//不为空
	{
		return false;
	}
}
void StackPop(ST2* ps)//删
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
	//printf("栈顶数据删除成功\n");
}

ElemType2 StackTop(ST2* ps)//查看栈顶
{
	assert(ps);
	//找栈顶的话得保证指向的空间不为空
	assert(!StackEmpty(ps));
	//此时的top-1就是栈顶数据
	return ps->a[ps->top - 1];
}
void StackDestory(ST2* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
	}
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

bool IsPass(int maze[11][11], int n, PT next)
{
	if (next.row == n + 1 && next.col == n){
	
		return true;
	}

	if (next.row == n && next.col == n + 1){
		return true;	
	}

	if (next.row == n + 1 && next.col == n + 1) {
		return true;
	}

	if (next.row > n || next.col > n) {
		return false;
	}
	else {
		return true;
	}

}
ST st;
ST2 path;//路线
int data_1 = 0, data_2 = 0;

int StackCalc(ST* ps){
	int sum = 0;
	assert(ps);
	while (!StackEmpty(&st)){
		sum += StackTop(&st);
		StackPop(&st);
	}
	return sum;
}
void StackClean(ST* ps)
{
	assert(ps);
	while (!StackEmpty(ps))
	{
		StackPop(ps);
	}
}
int data_max = 0;
int data_max2 = 0;
//找迷宫通路,并记录最大的和,还有最大和的路线
void GetMazePath(int maze[11][11], int n, PT cur) {
	PT next = cur;
	int temp = 0;


	//StackPush(&path, cur);
	StackPush(&st, maze[cur.row][cur.col]);
	if (cur.row == n + 1 && cur.col == n + 1) {
		temp = StackCalc(&st);
		if (temp > data_max) {
			data_max = temp;
		}
		if (temp > data_max2 && temp < data_max) {
			data_max2 = temp;
		}
		StackClean(&st);
		return;
	}

	//往下走
	next.row += 1;
	if (IsPass(maze, n, next)) {
		GetMazePath(maze, n, next);
	}
	

	
	//往右走
	next = cur;
	next.col += 1;
	if (IsPass(maze, n, next)) {
		GetMazePath(maze, n, next);
		
	}
	
	//StackPop(&path);
}
int main(){
	StackInit(&st);
	StackInit(&path);

	int n = 0;
	cin >> n;
	int maze[11][11] = { 0 };
	int i = 0;
	int row = 1, col = 1, input = 1;
	while (row != 0 && col != 0 && input != 0) {
		cin >> row >> col >> input;
		maze[row][col] = input;
	}
	PT entery = { 1,1 };
	
	//第一次找最大
	GetMazePath(maze, n, entery);
	int sum = 0;
	sum += data_max;

	/*while (!StackEmpty(&path))
	{
		row = StackTop(&path).row;
		col = StackTop(&path).col;
		maze[row][col] = 0;
		StackPop(&path);
	}*/


	//第二次找次大
	GetMazePath(maze, n, entery);
	sum += data_max2;


	cout << sum + maze[1][1] << endl;
	StackDestory(&st);
	StackDestory(&path);

	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值