Tempter of the Bone
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
注释:深度优先搜索 dfs package test_jgraph;
import java.util.Iterator;
import junit.framework.TestCase;
public classTestDfs extendsTestCase {
public void testDfs() {
new Maze().doTest();
}
public class Maze {
public void doTest() {
MazeTestCasets1= newMazeTestCase(4, 4, 5);
ts1.setData("S.X...X...XD....");
ts1.initStart();
assertTrue(dfs(ts1.getStart()[0], ts1.getStart()[1], 0, ts1) == false);
ts1.prettyPrint();
MazeTestCasets2= newMazeTestCase(3, 4, 5);
ts2.setData("S.X...X....D");
ts2.initStart();
assertTrue(dfs(ts2.getStart()[0], ts2.getStart()[1], 0, ts2) == true);
ts2.prettyPrint();
}
public boolean dfs(intn, int m, int t, MazeTestCase ts) {
if (ts.isOutOfBoundary(n, m, t)) {
return false;
}
if (ts.isFind(n, m, t)) {
return true;
}
TraverseIterator<int[]> iterator = iterator();
while (iterator.hasNext()) {
int dir[] = iterator.next();
int n1 = n + dir[0];
int m1 = m + dir[1];
if (!ts.isOutOfBoundary(n1, m1, t + 1)) {
Stringc = ts.getValue(n1, m1);
if (ts.isValid(c)) {
ts.setValue(n1, m1, "O" + (t + 1));
if (dfs(n1, m1, t + 1, ts)) {
return true;
}
ts.setValue(n1, m1, ".");
}
}
}
return false;
}
private TraverseIterator<int[]> iterator() {
return new TraverseIterator<int[]>();
}
}
class MazeTestCase {
private int N;
private int M;
private int T;
private String[][] data;
private int[] door;
private int[] start;
public MazeTestCase(int n, int m, int t) {
N = n;
M = m;
T = t;
}
public boolean isValid(String c) {
return !c.equals("X") && !c.startsWith("O");
}
public void initStart() {
setValue(getStart()[0],getStart()[1], "O");
}
public void setData(String str) {
data = new String[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
data[i][j] = String.valueOf(str.charAt(i * M + j));
if (data[i][j].equals("S")) {
start = new int[] { i, j };
}
if (data[i][j].equals("D")) {
door = new int[] { i, j };
}
}
}
}
public void setValue(int n, int m, String c) {
data[n][m] = c;
}
public String getValue(int n, int m) {
return data[n][m];
}
private int[] getDoor() {
return door;
}
private int[] getStart() {
return start;
}
public boolean isOutOfBoundary(int n, int m, int t) {
return n < 0 || n >= N || m < 0 || m >= M || t < 0 || t > T;
}
public boolean isFind(int n, int m, int t) {
return n == getDoor()[0] && m == getDoor()[1] && t == T;
}
public void prettyPrint(){
for( int i= 0 ; i < N; i ++){
for( int j = 0 ; j < M; j ++){
System.out.print("\t" + data[i][j] + "\t");
}
System.out.println();
}
System.out.println();
}
}
class TraverseIterator<T> implements Iterator<T> {
final int[][] dirList = { { 0, 1 }, { 1, 0 },{ 0, -1 }, { -1, 0 } };
int count = 0;
@Override
public boolean hasNext() {
return count < 4;
}
@SuppressWarnings("unchecked")
@Override
public T next() {
return (T) dirList[count++];
}
@Override
public void remove() {
}
}
}
|
Tempter of the Bone
最新推荐文章于 2020-02-22 19:04:33 发布