一天一个算法题-简单的-递归

本文介绍了一个简单的扫雷游戏实现方式,使用Java编程语言通过递归方法展开空白区域。该实现利用了二维数组来存储游戏地图,并使用HashMap来记录已触碰的单元格。

扫雷游戏,在点击某点A的时候,并且A是空白区域,那么他会直接打开一片。java的类似如下:

package com.jue.rescursion;

import java.util.HashMap;
import java.util.Map;

public class Box {
	private Map<String,Boolean> myMap= new HashMap<String,Boolean>();

	private int[][] box = { 
			{ 0, 1, 1, 1, 1, 1 },
			{ 0, 0, 0, 1, 1, 1 },
			{ 1, 0, 0, 1, 1, 1 },
			{ 1, 0, 0, 1, 1, 1 },
			{ 1, 0, 0, 0, 1, 1 },
			{ 1, 1, 1, 1, 1, 1 },
			};

	public void touchCell(int row, int colum) {
		if (row < 0 || colum < 0 || row > (box[0].length - 1) || colum > (box.length - 1)) {
			return;
		}
		if (box[row][colum] == 0) {
			String index = row + "+" + colum;
			if (!myMap.containsKey(index)) {
				myMap.put(index, true);
			} else {
				return;
			}
			System.out.println("(" + row + "," + colum + ") fired " + box[row][colum]);
			touchCell(row - 1, colum);
			touchCell(row, colum - 1);
			touchCell(row + 1, colum);
			touchCell(row, colum + 1);

		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值