迷宫问题

#include <stdio.h>
#include <stdlib.h>
#define n 5
#define m 5

/*地图*/
char place[m][n] = {
'.', '.', '.', '*', '*',
'*', '.', '*', '*', '.',
'.', '.', '.', '.', '.',
'.', '.', '.', '.', '.',
'*', '.', '.', '.', '.'
};

/*定义起始坐标 与 终点坐标*/
int xStart = 0;
int yStart = 0;
int xEnd = 2;
int yEnd = 0;

/*最大拐弯次数*/
int maxCount = 2;

/*定义走的方向*/
int a[5][2] = {0, 0, -1, 0, 0, -1, 0, 1, 1, 0};

int change = 0;
int oldDriection = 0;
int flag;

int ok(int x, int y) {
	if (x < 0 || x >= m || y < 0 || y >=n || place[x][y] == '*')
		return 0;
	return 1;
}
void traceback(int x, int y) {
	if (change > maxCount+1)
		return;
	if (flag)
		return;
	if (x == xEnd && y == yEnd) {
		flag = 1;
		return;
	}

	for (int d = 1; d<=4; d++) {
		x+=a[d][0];
		y+=a[d][1];
		int temp = oldDriection;
		if (ok(x, y)) {
			if (d != oldDriection)
				change++;
			oldDriection = d;
			traceback(x, y);
			oldDriection = temp;
			if (d != oldDriection)
				change--;
		}
		x-=a[d][0];
		y-=a[d][1];
	}
}
int main() {
	traceback(xStart, yStart);
	if (flag)
		printf("yes");
	else 
		printf("no");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值