关于扫雷的自动补空实现C语言

关于扫雷的自动补空实现C语言

相信很多朋友在用C语言实现扫雷功能时,都想实现扫雷里面的的自动补足功能,但总是难以实现,在这里我将分享一种方法------递归思想

先看代码!

位置在game.c

c 复制代码
//判断附近雷区并过滤空白区(实现递归)
void FilterBlankAreas(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y) {
	int MineCount = GetMineCount(mine, x, y);
	
	if (MineCount != 0) {
		show[x][y] = MineCount + '0';
	}
	else if(show[x][y] != ' ' && MineCount == 0 && x >= 1 && x <= row && y >= 1 && y <= col){
		show[x][y] = ' ';
		FilterBlankAreas(mine, show, row, col, x - 1, y - 1);
		FilterBlankAreas(mine, show, row, col, x - 1, y);
		FilterBlankAreas(mine, show, row, col, x - 1, y + 1);
		FilterBlankAreas(mine, show, row, col, x, y - 1);
		FilterBlankAreas(mine, show, row, col, x, y + 1);
		FilterBlankAreas(mine, show, row, col, x + 1, y - 1);
		FilterBlankAreas(mine, show, row, col, x + 1, y);
		FilterBlankAreas(mine, show, row, col, x + 1, y + 1);
	}
	
}


//排查雷+过滤空白区
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	while (1) {
		printf("请输入坐标:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			if (mine[x][y] == '1') {
				printf("很遗憾,你被炸死了!\n");
				break;
			}
			else if (show[x][y] != '*') {
				DisplayBoard(show, ROW, COL);
				printf("这个坐标你已经排查过了,请重输\n");
			}
			else
			{
				FilterBlankAreas(mine, show, row, col, x, y);
				DisplayBoard(show, ROW, COL);
			}
		}
		else {
			printf("输入错误,请重输!\n");
		}
	}
}

过滤空白区解析

c 复制代码
void FilterBlankAreas(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y) {
	int MineCount = GetMineCount(mine, x, y);
	
	if (MineCount != 0) {
		show[x][y] = MineCount + '0';
	}
	else if(show[x][y] != ' ' && MineCount == 0 && x >= 1 && x <= row && y >= 1 && y <= col){
		show[x][y] = ' ';
		FilterBlankAreas(mine, show, row, col, x - 1, y - 1);
		FilterBlankAreas(mine, show, row, col, x - 1, y);
		FilterBlankAreas(mine, show, row, col, x - 1, y + 1);
		FilterBlankAreas(mine, show, row, col, x, y - 1);
		FilterBlankAreas(mine, show, row, col, x, y + 1);
		FilterBlankAreas(mine, show, row, col, x + 1, y - 1);
		FilterBlankAreas(mine, show, row, col, x + 1, y);
		FilterBlankAreas(mine, show, row, col, x + 1, y + 1);
	}
}

这个FilterBlankAreas()函数主要先判断输入坐标附近八个空间地雷的数量,若地雷的数量不为0,那么直接将show棋盘的该坐标设置为地雷数,并没有进入递归。

反之 要是地雷数量为0,那么就进入递归开始排查除了原本x和y的另外8个坐标,注意: 在进入判断递归条件时一定要加入 show[x][y] != ' ',不然该函数会在内部没有地雷的空间进行无限递归从而导致崩溃。同时还要注意递归的范围是否数组越界,否则会导致严重的问题。

小tip:其实递归函数理论上可以不用八个

c 复制代码
FilterBlankAreas(mine, show, row, col, x - 1, y - 1);
FilterBlankAreas(mine, show, row, col, x - 1, y);
FilterBlankAreas(mine, show, row, col, x - 1, y + 1);
FilterBlankAreas(mine, show, row, col, x, y - 1);
FilterBlankAreas(mine, show, row, col, x, y + 1);
FilterBlankAreas(mine, show, row, col, x + 1, y - 1);
FilterBlankAreas(mine, show, row, col, x + 1, y);
FilterBlankAreas(mine, show, row, col, x + 1, y + 1);

用上下左右的坐标再进行递归就可以了,减少了运算量。

c 复制代码
FilterBlankAreas(mine, show, row, col, x - 1, y);
FilterBlankAreas(mine, show, row, col, x, y - 1);
FilterBlankAreas(mine, show, row, col, x, y + 1);
FilterBlankAreas(mine, show, row, col, x + 1, y);
相关推荐
W23035765734 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
.Ashy.4 小时前
2026.4.11 蓝桥杯软件类C/C++ G组山东省赛 小记
c语言·c++·蓝桥杯
2401_892070984 小时前
链栈(链式栈) 超详细实现(C 语言 + 逐行精讲)
c语言·数据结构·链栈
minji...5 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
语戚6 小时前
力扣 968. 监控二叉树 —— 贪心 & 树形 DP 双解法递归 + 非递归全解(Java 实现)
java·算法·leetcode·贪心算法·动态规划·力扣·
skywalker_116 小时前
力扣hot100-7(接雨水),8(无重复字符的最长子串)
算法·leetcode·职场和发展
bIo7lyA8v7 小时前
算法稳定性分析中的输入扰动建模的技术9
算法
CoderCodingNo7 小时前
【GESP】C++三级真题 luogu-B4499, [GESP202603 三级] 二进制回文串
数据结构·c++·算法
sinat_286945197 小时前
AI Coding 时代的 TDD:从理念到工程落地
人工智能·深度学习·算法·tdd