关于扫雷的自动补空实现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);
相关推荐
ytttr87333 分钟前
MATLAB基于LDA的人脸识别算法实现(ORL数据库)
数据库·算法·matlab
charlie11451419140 分钟前
现代嵌入式C++教程:C++98——从C向C++的演化(2)
c语言·开发语言·c++·学习·嵌入式·教程·现代c++
雨季余静1 小时前
c语言 gb2312转utf-8,带码表,直接使用。
c语言·c语言utf8·c语言gb2312·c语言gbk·c语言gb18030·gb2312转utf8·gbk转utf8
2401_890443021 小时前
Linux 基础IO
linux·c语言
jianfeng_zhu2 小时前
整数数组匹配
数据结构·c++·算法
smj2302_796826522 小时前
解决leetcode第3782题交替删除操作后最后剩下的整数
python·算法·leetcode
LYFlied3 小时前
【每日算法】LeetCode 136. 只出现一次的数字
前端·算法·leetcode·面试·职场和发展
唯唯qwe-4 小时前
Day23:动态规划 | 爬楼梯,不同路径,拆分
算法·leetcode·动态规划
做科研的周师兄4 小时前
中国土壤有机质数据集
人工智能·算法·机器学习·分类·数据挖掘
来深圳4 小时前
leetcode 739. 每日温度
java·算法·leetcode