- simple version of labyrinth have no backtracking
public class MiGong {
public static void main(String[] args) {
//创建一个二维数组,模拟迷宫
//map
int[][] map = new int[8][7];
//使用1 表示墙
//上下全部置为1
//将左右设为1
for(int i =0; i<8;i++){
map[i][0]=1;
map[i][6]=1;
}
//将上下置为1
for(int i=0;i<7;i++){
map[0][i]=1;
map[7][i]=1;
}
map[3][1]=1;
map[3][2]=1;
System.out.println("old map");
for (int[] ints : map) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
//using recursion backtracking to find available path for small ball.
boolean b = setWay(map, 1, 1);
System.out.println(b);
//print the new map, the small ball have been discovered and recursion.
System.out.println("----------new map----------");
for (int[] ints : map) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
}
// using recursion backtracking to find the available path
//explanation
//1. map
//2. i,j starting point from map[i,j]
//3. it's exit labyrinth if the ball reach map[6][5]
//4. 约定. 当map[i][j] is 0 means this point haven't discover,
// when it is 1 means this point is wall, when it is 2 means this point is available
// when it is 3 means this point had had discovered and not available
// when you is in labyrinth, you need to choose a strategy(method),
// down-->right-->up-->left, if that point isn't availbale, then backtracking.
/**
*
* @param map
* @param i this's beginning point
* @param j
* @return if it find exit labyrinth,return true,else return false.
*/
public static boolean setWay(int[][] map, int i ,int j){
if(map[6][5] == 2){// Already find the labyrinth exit.-->solution
return true;
}else{
if(map[i][j]==0){// using strategy,if current point hadn't been discovered.
//down-->right-->up-->left
map[i][j]=2;//let's say this point available-----assumption
if(setWay(map,i+1,j)){//went down
return true;
}else if(setWay(map,i,j+1)){//went right
return true;
}else if (setWay(map,i-1,j)){//went up
return true;
}else if (setWay(map,i,j-1)){//went left
return true;
}else{
//when program went to this place means this way isn't available.
map[i][j]=3;
return false;
}
}else{// it may be 1,2,3 ,if map[i][j] != 0,
return false;
}
}
}
}
QUESTION.
how to find the shortest path to escape the labyrinth, please left your solution.
- The Eight Queen
/**
* 1.第一个皇后放在 第一行 第一列
* 2.第二个皇后放在 第二行 第一列,判断是否ok,若不ok,第二列,第三列...
* 3.第三个皇后,第三行 第一列....判断....
*/
public class TheEightQueen {
//The number of queens.
int max = 8;
//This array for the queens' location ; e.g arr={0,4,7,5,2,6,1,3}
int[] array = new int[max];
static int count =0;
public static void main(String[] args) {
TheEightQueen theEightQueen = new TheEightQueen();
theEightQueen.check(0);
System.out.println("There have "+count+" ways to solve");
}
//This method for putting The n queen
private void check(int n) {
if (n == max) { // when n == 8 , The queens had been seted
print();
count++;
return;
}
//place queens in sequence and judge whther it's conflict
for(int i=0;i<max;i++){
//put the current queen in current row column 1
array[n]=i;
//judge whther it's conflict
if(judge(n)){ // no conflict
// then put the current+1 queen , the beginning of recursion.
check(n+1);
}
//conflict, do array[n] = i continuously
//put int current row column n+1
}
}
//Testing whther it's conflit,when we put The N queen
/**
* @param n The N queen
* @return
*/
private boolean judge(int n) {
for (int i = 0; i < n; i++) {
//1.array[i] == array[n] means to
// judge that The N queen and The N-1 queen whther it's in same column
//2.Math.abs(n-i) == Math.abs(array[n] - array[i] means to
// judge The n queen and The i queen whther it's in same oblique line.
if (array[i] == array[n] ||
Math.abs(n - i) == Math.abs(array[n] - array[i])) {
return false;
}
}
return true;
}
//this method is to print the queens' location
private void print() {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}