结果要显示 走过的坐标,一下是自己写的3个classMeiro.javapublic class Meiro {
Zahyo enter;
Zahyo goal;
int map[][];
Meiro(int map[][],Zahyo enter,Zahyo goal) {
this.map = map;
this.enter = enter;
this.goal = goal;
}
public void Display() {
for (int i = 0;i < map.length; i++ ) {
for (int j = 0;j < map[i].length; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
public int GetValue(int x,int y) {
return map[x][y];
}
public void PutValue(int x,int y) {
map[x][y] = 1;
}
public int walk() {
ZahyoStack datastack = new ZahyoStack();
datastack.push(enter);
while (datastack.dataCheck() >= 1) {
Zahyo data = datastack.pop();
if (goal.equals(data.x,data.y)) {
return 1;
} if (GetValue(data.x,data.y) != 0) {
} else {
PutValue(data.x,data.y);
data.write();
data.x = data.x - 1;
data.y = data.y;
datastack.push(data);
data.x = data.x + 1;
data.y = data.y - 1;
datastack.push(data);
data.x = data.x + 1;
data.y = data.y + 1;
datastack.push(data);
data.x = data.x - 1;
data.y = data.y + 1;
datastack.push(data);
}
}
return 1;
}
public static void main(String[] args) {
Zahyo enter = new Zahyo(1,1);
Zahyo goal = new Zahyo(11,11);
int map[][] = new int [][] {
{2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,0,2,2,2,2,2,2,2},
{2,0,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,2,2,2,2,2,2,2,2},
{2,0,2,2,2,2,0,0,2,2,2,2},
{2,0,0,0,0,0,0,0,0,0,2,2},
{2,0,2,2,2,2,2,2,2,0,0,2},
{2,0,2,2,2,2,2,2,2,2,0,2},
{2,0,0,0,0,2,2,2,2,2,0,2},
{2,2,0,2,2,2,2,0,0,0,0,2},
{2,2,0,2,2,2,2,2,0,2,2,2},
{2,0,0,2,2,2,2,2,0,0,0,2},
{2,2,2,2,2,2,2,2,2,2,2,2}};
Meiro a = new Meiro(map,enter,goal);
a.Display();
a.walk();
}
}Zahyo.javapublic class Zahyo {
public int x,y;
Zahyo(int x1,int y1) {
this.x = x1;
this.y = y1;
}
public void write() {
System.out.println("(" + x + "," + y + ")");
}
public void set(int x1,int y1) {
this.x = x1;
this.y = y1;
}
public boolean equals(int x1,int y1) {
return (this.x == x1) && (this.y == y1);
}
}ZahyoStack.java
public class ZahyoStack {
private Zahyo[] stack;
private int top = 0;
public ZahyoStack() {this(100); }
public ZahyoStack ( int max) { stack = new Zahyo[max]; }
public void push (Zahyo item) {
if (top == stack.length) {
System.out.println("Stack is full");
}
stack[top] = item;
top = top + 1;
}
public Zahyo pop() {
if (top == 0) {
System.out.println("Stack is emply");
}
top = top - 1;
return stack[top];
}
public void display() {
System.out.println("top = " + top);
for (int i = 0; i < top ; i++) {
stack[i].write();
}
}
public int dataCheck() {
return top;
}
}
实行结果是2 2 2 2 2 2 2 2 2 2 2 2
2 0 0 0 0 2 2 2 2 2 2 2
2 0 2 2 2 2 2 2 2 2 2 2
2 0 0 0 2 2 2 2 2 2 2 2
2 0 2 2 2 2 0 0 2 2 2 2
2 0 0 0 0 0 0 0 0 0 2 2
2 0 2 2 2 2 2 2 2 0 0 2
2 0 2 2 2 2 2 2 2 2 0 2
2 0 0 0 0 2 2 2 2 2 0 2
2 2 0 2 2 2 2 0 0 0 0 2
2 2 0 2 2 2 2 2 0 2 2 2
2 0 0 2 2 2 2 2 0 0 0 2
2 2 2 2 2 2 2 2 2 2 2 2
(1,1)
(1,2)
(1,3)
(1,4)自己也知道,是走到了死胡同了,可是怎么改wali()的地方才可以呢?0表示可能走的,2表示墙壁,1表示 走过的
Zahyo enter;
Zahyo goal;
int map[][];
Meiro(int map[][],Zahyo enter,Zahyo goal) {
this.map = map;
this.enter = enter;
this.goal = goal;
}
public void Display() {
for (int i = 0;i < map.length; i++ ) {
for (int j = 0;j < map[i].length; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
public int GetValue(int x,int y) {
return map[x][y];
}
public void PutValue(int x,int y) {
map[x][y] = 1;
}
public int walk() {
ZahyoStack datastack = new ZahyoStack();
datastack.push(enter);
while (datastack.dataCheck() >= 1) {
Zahyo data = datastack.pop();
if (goal.equals(data.x,data.y)) {
return 1;
} if (GetValue(data.x,data.y) != 0) {
} else {
PutValue(data.x,data.y);
data.write();
data.x = data.x - 1;
data.y = data.y;
datastack.push(data);
data.x = data.x + 1;
data.y = data.y - 1;
datastack.push(data);
data.x = data.x + 1;
data.y = data.y + 1;
datastack.push(data);
data.x = data.x - 1;
data.y = data.y + 1;
datastack.push(data);
}
}
return 1;
}
public static void main(String[] args) {
Zahyo enter = new Zahyo(1,1);
Zahyo goal = new Zahyo(11,11);
int map[][] = new int [][] {
{2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,0,2,2,2,2,2,2,2},
{2,0,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,2,2,2,2,2,2,2,2},
{2,0,2,2,2,2,0,0,2,2,2,2},
{2,0,0,0,0,0,0,0,0,0,2,2},
{2,0,2,2,2,2,2,2,2,0,0,2},
{2,0,2,2,2,2,2,2,2,2,0,2},
{2,0,0,0,0,2,2,2,2,2,0,2},
{2,2,0,2,2,2,2,0,0,0,0,2},
{2,2,0,2,2,2,2,2,0,2,2,2},
{2,0,0,2,2,2,2,2,0,0,0,2},
{2,2,2,2,2,2,2,2,2,2,2,2}};
Meiro a = new Meiro(map,enter,goal);
a.Display();
a.walk();
}
}Zahyo.javapublic class Zahyo {
public int x,y;
Zahyo(int x1,int y1) {
this.x = x1;
this.y = y1;
}
public void write() {
System.out.println("(" + x + "," + y + ")");
}
public void set(int x1,int y1) {
this.x = x1;
this.y = y1;
}
public boolean equals(int x1,int y1) {
return (this.x == x1) && (this.y == y1);
}
}ZahyoStack.java
public class ZahyoStack {
private Zahyo[] stack;
private int top = 0;
public ZahyoStack() {this(100); }
public ZahyoStack ( int max) { stack = new Zahyo[max]; }
public void push (Zahyo item) {
if (top == stack.length) {
System.out.println("Stack is full");
}
stack[top] = item;
top = top + 1;
}
public Zahyo pop() {
if (top == 0) {
System.out.println("Stack is emply");
}
top = top - 1;
return stack[top];
}
public void display() {
System.out.println("top = " + top);
for (int i = 0; i < top ; i++) {
stack[i].write();
}
}
public int dataCheck() {
return top;
}
}
实行结果是2 2 2 2 2 2 2 2 2 2 2 2
2 0 0 0 0 2 2 2 2 2 2 2
2 0 2 2 2 2 2 2 2 2 2 2
2 0 0 0 2 2 2 2 2 2 2 2
2 0 2 2 2 2 0 0 2 2 2 2
2 0 0 0 0 0 0 0 0 0 2 2
2 0 2 2 2 2 2 2 2 0 0 2
2 0 2 2 2 2 2 2 2 2 0 2
2 0 0 0 0 2 2 2 2 2 0 2
2 2 0 2 2 2 2 0 0 0 0 2
2 2 0 2 2 2 2 2 0 2 2 2
2 0 0 2 2 2 2 2 0 0 0 2
2 2 2 2 2 2 2 2 2 2 2 2
(1,1)
(1,2)
(1,3)
(1,4)自己也知道,是走到了死胡同了,可是怎么改wali()的地方才可以呢?0表示可能走的,2表示墙壁,1表示 走过的
public void push (Zahyo item) {
if (top == stack.length) {
System.out.println("Stack is full");
}
Zahyo tmp = new Zahyo(item.x,item.y);
stack[top] = tmp;
top = top + 1;
}