代码
package com.atguitu.recursion;
public class MiGongSearchShortestPath {
public static void main(String[] args) {
int[][] map = new int[8][7];
setMap(map);
System.out.println("初始地图的情况");
showMap(map);
int[] arr = new int[24];
String[] array = { "1234", "1243", "1423", "4123", "1324", "1342", "1432", "4132", "3124", "3142", "3412",
"4312", "2134", "2143", "2413", "4213", "2314", "2341", "2431", "4231", "3214", "3241", "3421",
"4321" };
int[] count = new int[24];
for (int i = 0; i < 24; i++) {
arr[i] = i;
chooseMethod(arr[i], map);
count[i] = countUsefulNode(map);
setMap(map);
}
int method = getMinNodes(count);
chooseMethod(method, map);
System.out.println("上(1), 下(2), 左(3), 右(4)");
System.out.println("最短路径使用的策略是:");
System.out.println(method + ":" + array[method]);
System.out.println("属于最短路径的策略有:");
for (int i = 0; i < count.length; i++) {
if (count[i] == count[method]) {
System.out.println(i + ":" + array[i]);
}
}
System.out.println("最短路径的地图情况:");
showMap(map);
for (int i = 0; i < count.length; i++) {
System.out.println(i + ":" + array[i] + ":" + count[i]);
}
}
public static void showMap(int[][] map) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
public static int getMinNodes(int[] count) {
int min = 99;
int method = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] < min) {
min = count[i];
method = i;
}
}
return method;
}
public static int countUsefulNode(int[][] map) {
int count = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
if (map[i][j] == 2) {
count++;
} else {
continue;
}
}
}
return count;
}
public static void setMap(int[][] map) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
map[i][j] = 0;
}
}
for (int i = 0; i < 7; i++) {
map[0][i] = 1;
map[7][i] = 1;
}
for (int i = 0; i < 8; i++) {
map[i][0] = 1;
map[i][6] = 1;
}
map[3][1] = 1;
map[3][2] = 1;
}
public static void chooseMethod(int i, int map[][]) {
switch (i) {
case 0:
_1234(map, 1, 1);
break;
case 1:
_1243(map, 1, 1);
break;
case 2:
_1423(map, 1, 1);
break;
case 3:
_4123(map, 1, 1);
break;
case 4:
_1324(map, 1, 1);
break;
case 5:
_1342(map, 1, 1);
break;
case 6:
_1432(map, 1, 1);
break;
case 7:
_4132(map, 1, 1);
break;
case 8:
_3124(map, 1, 1);
break;
case 9:
_3142(map, 1, 1);
break;
case 10:
_3412(map, 1, 1);
break;
case 11:
_4312(map, 1, 1);
break;
case 12:
_2134(map, 1, 1);
break;
case 13:
_2143(map, 1, 1);
break;
case 14:
_2413(map, 1, 1);
break;
case 15:
_4213(map, 1, 1);
break;
case 16:
_2314(map, 1, 1);
break;
case 17:
_2341(map, 1, 1);
break;
case 18:
_2431(map, 1, 1);
break;
case 19:
_4231(map, 1, 1);
break;
case 20:
_3214(map, 1, 1);
break;
case 21:
_3241(map, 1, 1);
break;
case 22:
_3421(map, 1, 1);
break;
case 23:
_4321(map, 1, 1);
break;
default:
break;
}
}
public static int up(int i) {
return i - 1;
}
public static int down(int i) {
return i + 1;
}
public static int left(int j) {
return j - 1;
}
public static int right(int j) {
return j + 1;
}
public static boolean _1234(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_1234(map, up(i), j)) {
return true;
} else if (_1234(map, down(i), j)) {
return true;
} else if (_1234(map, i, left(j))) {
return true;
} else if (_1234(map, i, right(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _1243(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_1243(map, up(i), j)) {
return true;
} else if (_1243(map, down(i), j)) {
return true;
} else if (_1243(map, i, right(j))) {
return true;
} else if (_1243(map, i, left(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _1423(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_1423(map, up(i), j)) {
return true;
} else if (_1423(map, i, right(j))) {
return true;
} else if (_1423(map, down(i), j)) {
return true;
} else if (_1423(map, i, left(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _4123(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_4123(map, i, right(j))) {
return true;
} else if (_4123(map, up(i), j)) {
return true;
} else if (_4123(map, down(i), j)) {
return true;
} else if (_4123(map, i, left(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _1324(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_1324(map, up(i), j)) {
return true;
} else if (_1324(map, i, left(j))) {
return true;
} else if (_1324(map, down(i), j)) {
return true;
} else if (_1324(map, i, right(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _1342(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_1342(map, up(i), j)) {
return true;
} else if (_1342(map, i, left(j))) {
return true;
} else if (_1342(map, i, right(j))) {
return true;
} else if (_1342(map, down(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _1432(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_1432(map, up(i), j)) {
return true;
} else if (_1432(map, i, right(j))) {
return true;
} else if (_1432(map, i, left(j))) {
return true;
} else if (_1432(map, down(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _4132(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_4132(map, i, right(j))) {
return true;
} else if (_4132(map, up(i), j)) {
return true;
} else if (_4132(map, i, left(j))) {
return true;
} else if (_4132(map, down(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _3124(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_3124(map, i, left(j))) {
return true;
} else if (_3124(map, up(i), j)) {
return true;
} else if (_3124(map, down(i), j)) {
return true;
} else if (_3124(map, i, right(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _3142(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_3142(map, i, left(j))) {
return true;
} else if (_3142(map, up(i), j)) {
return true;
} else if (_3142(map, i, right(j))) {
return true;
} else if (_3142(map, down(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _3412(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_3412(map, i, left(j))) {
return true;
} else if (_3412(map, i, right(j))) {
return true;
} else if (_3412(map, up(i), j)) {
return true;
} else if (_3412(map, down(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _4312(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_4312(map, i, right(j))) {
return true;
} else if (_4312(map, i, left(j))) {
return true;
} else if (_4312(map, up(i), j)) {
return true;
} else if (_4312(map, down(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _2134(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_2134(map, down(i), j)) {
return true;
} else if (_2134(map, up(i), j)) {
return true;
} else if (_2134(map, i, left(j))) {
return true;
} else if (_2134(map, i, right(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _2143(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_2143(map, down(i), j)) {
return true;
} else if (_2143(map, up(i), j)) {
return true;
} else if (_2143(map, i, right(j))) {
return true;
} else if (_2143(map, i, left(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _2413(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_1234(map, down(i), j)) {
return true;
} else if (_1234(map, i, right(j))) {
return true;
} else if (_1234(map, up(i), j)) {
return true;
} else if (_1234(map, i, left(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _4213(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_4213(map, i, right(j))) {
return true;
} else if (_4213(map, down(i), j)) {
return true;
} else if (_4213(map, up(i), j)) {
return true;
} else if (_4213(map, i, left(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _2314(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_2314(map, down(i), j)) {
return true;
} else if (_2314(map, i, left(j))) {
return true;
} else if (_2314(map, up(i), j)) {
return true;
} else if (_2314(map, i, right(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _2341(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_2341(map, down(i), j)) {
return true;
} else if (_2341(map, i, left(j))) {
return true;
} else if (_2341(map, i, right(j))) {
return true;
} else if (_2341(map, up(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _2431(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_2431(map, down(i), j)) {
return true;
} else if (_2431(map, i, right(j))) {
return true;
} else if (_2431(map, i, left(j))) {
return true;
} else if (_2431(map, up(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _4231(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_4231(map, i, right(j))) {
return true;
} else if (_4231(map, down(i), j)) {
return true;
} else if (_4231(map, i, left(j))) {
return true;
} else if (_4231(map, up(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _3214(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_3214(map, i, left(j))) {
return true;
} else if (_3214(map, down(i), j)) {
return true;
} else if (_3214(map, up(i), j)) {
return true;
} else if (_3214(map, i, right(j))) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _3241(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_3241(map, i, left(j))) {
return true;
} else if (_3241(map, down(i), j)) {
return true;
} else if (_3241(map, i, right(j))) {
return true;
} else if (_3241(map, up(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _3421(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_3421(map, i, left(j))) {
return true;
} else if (_3421(map, i, right(j))) {
return true;
} else if (_3421(map, down(i), j)) {
return true;
} else if (_3421(map, up(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
public static boolean _4321(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (_4321(map, i, right(j))) {
return true;
} else if (_4321(map, i, left(j))) {
return true;
} else if (_4321(map, down(i), j)) {
return true;
} else if (_4321(map, up(i), j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
}
结果截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/55297624bcd655ca962689b5524fbc8e.png#pic_center)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/05a93bba3156e7335a08fec0e10116a1.png#pic_center)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/67cf4512a4a3cacc5fd7d393f2bd78f4.png#pic_center)