C语言随机走迷宫超出极限, 突破B站纪录!(24行加闪屏优化)

这段代码实现了一个C++程序,用于生成随机迷宫并允许玩家通过键盘控制在迷宫中移动。程序使用了二维数组来表示迷宫地图,并实现了基本的移动逻辑,包括上下左右的方向判断和碰撞检测。玩家的目标是在迷宫中找到出路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <Windows.h>
#include <conio.h>
int map[21 * 21] = { 0 }, x = 0, y = 1;
void dig(int v) {
	int a[4] = { 2,-2,2 * 21,-2 * 21 }, i = 0, j;
	for (; j = rand() % 4, i < 4; i++) i - j && (a[i] ^= a[j] ^= a[i] ^= a[j]);
	for (map[v] = 1, i = 0; i < 4; i++) {
		if (v + a[i] < 0 || 21 * 21 < v + a[i] || map[v + a[i]])continue;
		if ((a[i] == 2 || a[i] == -2) && v / 21 - (a[i] + v) / 21)continue;
		map[v + a[i] / 2] = 1, dig(v + a[i]);
	}
}
int main() {
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &(CONSOLE_CURSOR_INFO) { 25, 0 });
	srand((unsigned)malloc(!system("mode con:cols=42 lines=21")));
	dig(21 + 1), map[21] = map[21 * 21 - 21 - 1] = 1;
	for (char c; c = _getch(), x<20;) {
		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), (COORD) { 0, 0 });
		x += (c == 'd' && map[y * 21 - (21 - x) + 21 + 1] == 1 ? 1 : (c == 'a' && map[y * 21 - (21 - x) + 21 - 1] == 1 ? -1 : 0));
		y += (c == 'w' && map[y * 21 - (21 - x) + 21 - 21] == 1 ? -1 : (c == 's' && map[y * 21 - (21 - x) + 21*2] == 1 ? 1 : 0));
		for (int i = 0; i < 21 * 21; i++)
			_cputs((map[i] ? "  " : "[]") == "  " && ((y*21)-(21-x)+21) == i ? "<>" : (map[i] ? "  " : "[]"));
	}
}

(未经过同意,禁止转载)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值