前几天在抖音看到个题,当时没做出来,今天突然想起就用程序做了一个。居然是无解,也不敢完全保证程序没问题,有发现的告诉我,我会更正。
题目是图中0的格子走一遍,从左上开始,任意点结束,不能重复不能走斜线,图中1的位置不能走。
0 | 1 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 |
基本上可以理解为迷宫,下面是我的代码:
package test;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static int[][] room = new int[8][8];
// 行动路径
public static List<Spot> path = new ArrayList<Spot>();
public static void main(String[] args) {
// 初始化棋盘
init();
run();
System.out.println("GAME OVER!");
}
private static void init() {
// 空间初始化
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
room[i][j] = 0;
}
}
// 不可通行坐标(1,2)
room[0][1] = 1;
// 初始两步固定
room[0][0] = 1;
room[1][0] = 1;
// 行动路径初始化
int[] f = {0,0};
int[] s = {1,0};
path.add(new Spot(0, null, f, s));
path.add(new Spot(1, f, s, null));
}
private static boolean run() {
int lastSpotNum = path.size();
Spot lastSpot = path.get(lastSpotNum-1);
int x = lastSpot.getSpot()[0];
int y = lastSpot.getSpot()[1];
// System.out.println(x+","+y);
if(check()) {
return true;
}else {
//上
if(0 < x) {
x-=1;
if(0 == room[x][y]) {
//移动
room[x][y] = 1;
lastSpot.setSon(new int[] {x,y});
path.add(new Spot(lastSpotNum, lastSpot.spot, new int[] {x,y}, null));
if(run()) {
return true;
}else {
// 减坐标
room[x][y] = 0;
lastSpot.setSon(null);
path.remove(lastSpotNum);
x++;
// System.out.println(x+","+y);
}
}else {
x++;
}
}
//下
if(x < 4) {
x+=1;
if(0 == room[x][y]) {
//移动
room[x][y] = 1;
lastSpot.setSon(new int[] {x,y});
path.add(new Spot(lastSpotNum, lastSpot.spot, new int[] {x,y}, null));
if(run()) {
return true;
}else {
// 减坐标
room[x][y] = 0;
lastSpot.setSon(null);
path.remove(lastSpotNum);
x--;
// System.out.println(x+","+y);
}
}else {
x--;
}
}
//左
if(y > 0) {
y-=1;
if(0 == room[x][y]) {
//移动
room[x][y] = 1;
lastSpot.setSon(new int[] {x,y});
path.add(new Spot(lastSpotNum, lastSpot.spot, new int[] {x,y}, null));
if(run()) {
return true;
}else {
// 减坐标
room[x][y] = 0;
lastSpot.setSon(null);
path.remove(lastSpotNum);
y++;
// System.out.println(x+","+y);
}
}else {
y++;
}
}
//右
if(y < 4) {
y+=1;
if(0 == room[x][y]) {
//移动
room[x][y] = 1;
lastSpot.setSon(new int[] {x,y});
path.add(new Spot(lastSpotNum, lastSpot.spot, new int[] {x,y}, null));
if(run()) {
return true;
}else {
// 减坐标
room[x][y] = 0;
lastSpot.setSon(null);
path.remove(lastSpotNum);
y--;
// System.out.println(x+","+y);
}
}else {
y--;
}
}
return false;
}
}
private int[] moveLeft(int[] spot) {
return spot;
}
private static boolean check() {
// 遍历棋盘是否全走完
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
if(0 == room[i][j]) {
return false;
}
}
}
for(int i = 0; i < path.size(); i++) {
System.out.println(path.get(i).getSpot());
}
System.out.println(path.toString());
return true;
}
}
package test;
public class Spot {
public int num;
public int[] father = new int[2];
public int[] spot = new int[2];
public int[] son = new int[2];
Spot(int n, int[] f, int[] s, int[] ss) {
this.num = n;
this.father = f;
this.spot = s;
this.son = ss;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int[] getFather() {
return father;
}
public void setFather(int[] father) {
this.father = father;
}
public int[] getSpot() {
return spot;
}
public void setSpot(int[] spot) {
this.spot = spot;
}
public int[] getSon() {
return son;
}
public void setSon(int[] son) {
this.son = son;
}
}
代码比较简单,开始还以为递归会很费时间和资源,结果发现也挺快。