有没有人知道在扫雷游戏中,如果点击了一个没有雷的区域,周围没有雷的区域是怎么搜索的,什么时候停止搜索?
没太看懂您说的搜索的含义,我这里列举一个大致demo,关于扫雷中查找雷和找到类后周围展现的过程
首先,定义了横纵坐标后,这里是查找雷的过程
int CheckShow(char show[ROWS][COLS], int row, int col)
{
int win = 0;
int i = 0;
int j = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '#')
win++;
}
}
return win;
}
这是展开的过程
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 8 * '0';
}
void ExcludeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int count = get_mine_count(mine, x, y);
if (count != 0)
{
show[x][y] = count + '0';
}
else
{
show[x][y] = ' ';
if (show[x - 1][y] == '#')
ExcludeMine(mine, show, x - 1, y);
if (show[x - 1][y - 1] == '#')
ExcludeMine(mine, show, x - 1, y - 1);
if (show[x][y - 1] == '#')
ExcludeMine(mine, show, x, y - 1);
if (show[x + 1][y - 1] == '#')
ExcludeMine(mine, show, x + 1, y - 1);
if (show[x + 1][y] == '#')
ExcludeMine(mine, show, x + 1, y);
if (show[x + 1][y + 1] == '#')
ExcludeMine(mine, show, x + 1, y + 1);
if (show[x][y + 1] == '#')
ExcludeMine(mine, show, x, y + 1);
if (show[x - 1][y + 1] == '#')
ExcludeMine(mine, show, x - 1, y + 1);
}
}
1 回答3.3k 阅读✓ 已解决
1 回答2.7k 阅读
2.5k 阅读
1 回答499 阅读✓ 已解决
1 回答1.1k 阅读
1 回答450 阅读✓ 已解决
遍历即可,上下左右咯。不需要什么特殊算法
写好了,欢迎去玩,哈哈哈:http://jsrun.net/sfQKp/edit