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

来源:互联网 发布:中世纪2优化9毛子 编辑:程序博客网 时间:2024/05/21 12:58

扫雷游戏,在点击某点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);}}}


原创粉丝点击