我正在制作这个Android应用程序,需要使用A star算法解决puzzle8。为了实现这一点,我使用了一个比较器,如下所示:
public class PuzzleBoardComparator implements Comparator<PuzzleBoard> {
@Override
public int compare(PuzzleBoard A, PuzzleBoard B) {
Integer priorityA=A.priority();
Integer priorityB=B.priority();
if(priorityA.compareTo(priorityB)<0)
return -1;
if(priorityA.compareTo(priorityB)>0)
return 1;
else
return 0;
}
}
此comaparator使用基于计算曼哈顿距离的优先级(),如下所示:
public int priority() {
int manhattanDistance=0;
for(int x=0;x<NUM_TILES;x++){
for(int y=0;y<NUM_TILES;y++){
if(tiles.get(XYtoIndex(x,y))!=null){
int numTile=tiles.get(XYtoIndex(x,y)).getNumber();
numTile-=1;
int xNumTile=numTile/NUM_TILES;
int yNumTile=numTile%NUM_TILES;
manhattanDistance+=Math.abs(x-xNumTile)+Math.abs(y-yNumTile);
}
}
}
return manhattanDistance+steps;
}
现在,这是解决这个难题的A-star算法的实现:
public void solve() {
//Log.d("DEMO1","LOOP");
PuzzleBoardComparator comparator=new PuzzleBoardComparator();
//Log.d("DEMO2","LOOP");
PriorityQueue<PuzzleBoard> queue=new PriorityQueue<>(10,comparator);
//Log.d("DEMO3","LOOP");
puzzleBoard.steps=0;
puzzleBoard.previousBoard=null;
queue.add(puzzleBoard);
while(!queue.isEmpty()){
//Log.d("DEMO4","LOOP");
PuzzleBoard cBoard=queue.remove();
if(cBoard.resolved()){
ArrayList<PuzzleBoard> solution=new ArrayList<>();
//solution.add(cBoard);
while(cBoard.getParent()!=null){
solution.add(cBoard);
cBoard=cBoard.getParent();
}
solution.add(cBoard);
Collections.reverse(solution);
animation=solution;
break;
}
ArrayList<PuzzleBoard> neigh;
neigh=cBoard.neighbours();
Iterator<PuzzleBoard> it=neigh.iterator();
while(it.hasNext()){
queue.add(it.next());
}
}
}
但是,我的申请根本不起作用。每当我按下按钮来解决难题时,什么都不会发生。经过一段时间后,我在logcat上收到此错误消息:
07-14 15:14:08.491 6172-6172/com.google.engedu.puzzle8 E/dalvikvm-heap: Out of memory on a 64-byte allocation.
07-14 15:14:09.482 6172-6172/com.google.engedu.puzzle8 E/dalvikvm-heap: Out of memory on a 184-byte allocation.
07-14 15:14:09.662 6172-6172/com.google.engedu.puzzle8 E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.google.engedu.puzzle8, PID: 6172
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.OutOfMemoryError: [memory exhausted]
at dalvik.system.NativeStart.main(Native Method)
请帮我解决问题
编辑1:这是存在onclick属性的xml文件的元素。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/solve_button_label"
android:id="@+id/solve_button"
android:onClick="solve"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/shuffle_button"
android:layout_toEndOf="@+id/shuffle_button" />
编辑2:这是项目https://github.com/akhileshydv/Puzzle8.git
答案 0 :(得分:-1)
您需要在manifest.xml中添加此行
<application
android:largeHeap="true">