9宫格迷宫算法

 

 

9宫格迷宫算法

package mengka._9gongge;

import org.apache.commons.lang.StringUtils;

/**
 * 有一个9宫格的迷宫,求从这里出去的路径<br>
 * direction=1,向上走; <br>
 * direction=2,向右走; <br>
 * direction=3,向下走; <br>
 * direction=4,向左走; <br>
 * 
 * <hr>
 * 9宫格:<br>
 * <img src=
 * "http://img03.taobaocdn.com/imgextra/i1/16991050039708047/T2A_GcXFXaXXXXXXXX_!!150796991-0-shop_backyard.jpg_300x300"
 * /> <br><br>
 * 满足条件的一个迷宫走法是:<br>
 * nodes[0] = 1 <2><br>
 * nodes[1] = 2 <2><br>
 * nodes[2] = 3 <3><br>
 * nodes[3] = 6 <3><br>
 * nodes[4] = 9 <0><br>
 * <br>
 * <img src=
 * "http://img02.taobaocdn.com/imgextra/i4/16991037159872902/T25r9eXJNXXXXXXXXX_!!150796991-2-shop_backyard.png"
 * />
 * 
 * @author mengka.hyy
 * 
 */
public class _9gongGe {

	public static String[][] gongGe = new String[3][3];

	public static Node[] nodes = new Node[9];

	public static int n = 9;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int k = 0;
		boolean flag = false;

		initDate();
		tag2: while (k >= 0 && k < 9) {
			tag1: while (nodes[k].direction < 4) {
				nodes[k].direction += 1;
				nodes[k + 1] = getNodeValue(nodes[k]);
				if (StringUtils.isBlank(nodes[k + 1].value)) {
					continue;
				}
				if (!validDirection(k + 1)) {
					System.out.println("\n前进受阻");
					// break tag1;
					continue;
				}
				if ("9".equals(nodes[k + 1].value)) {
					System.out.println("成功走出迷宫..");
					flag = true;
					break tag2;
				}

				k = k + 1;
			}
			nodes[k + 1] = new Node();
			System.out.println("k = " + (k + 1));
			for (Node tmp : nodes) {
				System.out.print(tmp.value + " ");
			}
			System.out.println();
			k = k - 1;
		}

		if (flag) {
			System.out.println("满足条件的一个迷宫走法是:");
			for (int i = 0; i < nodes.length; i++) {
				System.out.println("nodes[" + i + "] = " + nodes[i].value
						+ " <" + nodes[i].direction + ">");
			}
		}
	}

	/**
	 * direction=1,向上走; <br>
	 * direction=2,向右走; <br>
	 * direction=3,向下走; <br>
	 * direction=4,向左走; <br>
	 * 
	 * @param node
	 * @return
	 */
	public static Node getNodeValue(Node node) {
		int direction = node.direction;
		int x = node.x;
		int y = node.y;
		Node result = new Node();
		switch (direction) {
		case 1:
			if (y + 1 > 2) {
				break;
			}
			result.value = gongGe[x][y + 1];
			result.x = x;
			result.y = y + 1;
			break;
		case 3:
			if (y - 1 < 0) {
				break;
			}
			result.value = gongGe[x][y - 1];
			result.x = x;
			result.y = y - 1;
			break;
		case 4:
			if (x - 1 < 0) {
				break;
			}
			result.value = gongGe[x - 1][y];
			result.x = x - 1;
			result.y = y;
			break;
		case 2:
			if (x + 1 > 2) {
				break;
			}
			result.value = gongGe[x + 1][y];
			result.x = x + 1;
			result.y = y;
			break;
		}
		return result;
	}

	/**
	 * 验证9宫格迷宫所走的路径是否包含已经走过的格子,如果有,则直接回退一个格子
	 * <hr>
	 * 规则:
	 * <ul>
	 * <li>一个格子只能走一次,不能走重复的格子;</li>
	 * <li>附加条件:如果要求计算最短的路径呢??</li>
	 * </ul>
	 * 
	 * @return
	 */
	public static boolean validDirection(int k) {
		String value = nodes[k].value;
		for (int i = 0; i < k; i++) {
			if (value.equals(nodes[i].value)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 初始化数据:
	 * 
	 * 
	 */
	public static void initDate() {
		/**
		 * 初始化9宫格
		 * 
		 * <0,0> = 7 <br>
		 * <0,1> = 4 <br>
		 * <0,2> = 1 <br>
		 * <1,0> = 8 <br>
		 * <1,1> = 5 <br>
		 * <1,2> = 2 <br>
		 * <2,0> = 9 <br>
		 * <2,1> = 6 <br>
		 * <2,2> = 3 <br>
		 */
		System.out.println("请输入9宫格的数据:");
		
		//①:手动输入
		// Scanner in = new Scanner(System.in);
		// for (int i = 0; i < 3; i++) {
		// for (int j = 0; j < 3; j++) {
		// System.out.print("<" + i + "," + j + "> = ");
		// gongGe[i][j] = in.nextLine();
		// }
		// }

		//②
		gongGe[0][0] = "7";
		gongGe[0][1] = "4";
		gongGe[0][2] = "1";
		gongGe[1][0] = "8";
		gongGe[1][1] = "5";
		gongGe[1][2] = "2";
		gongGe[2][0] = "9";
		gongGe[2][1] = "6";
		gongGe[2][2] = "3";

		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				System.out.println("<" + i + "," + j + "> = " + gongGe[i][j]);
			}
		}

		// 初始化result
		for (int i = 0; i < nodes.length; i++) {
			nodes[i] = new Node();
		}

		// 第一个结点为,Node(0,2)=1
		nodes[0] = new Node(0, 2);
		nodes[0].value = gongGe[0][2];
	}

}

 

package mengka._9gongge;

/**
 * 9宫格里面的格子对象
 * 
 * @author mengka.hyy
 *
 */
public class Node {

	/**
	 *  格子的对应的下一个的走向:
	 *    1:上
	 *    2:下
	 *    3:左
	 *    4:右
	 */
	public int direction;
	
	public String value;
	
	public String name;
	
	public int x;
	
	public int y;
	
	public Node(){
		this.value = "";
		this.direction = 0;
	}
	
	public Node(int x,int y){
		this.x = x;
		this.y = y;
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
	
}

 

运行结果:

请输入9宫格的数据:
<0,0> = 7
<0,1> = 4
<0,2> = 1
<1,0> = 8
<1,1> = 5
<1,2> = 2
<2,0> = 9
<2,1> = 6
<2,2> = 3

前进受阻
成功走出迷宫..
满足条件的一个迷宫走法是:
nodes[0] = 1 <2>
nodes[1] = 2 <2>
nodes[2] = 3 <3>
nodes[3] = 6 <3>
nodes[4] = 9 <0>
nodes[5] =  <0>
nodes[6] =  <0>
nodes[7] =  <0>
nodes[8] =  <0>

 
9宫格迷宫算法
 

你可能感兴趣的:(算法)