- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要你的帮助。我不知道为什么当维度为 10 且脏槽为 11 时我的代码会失败,请帮忙。
package Strategies;
import java.util.LinkedList;
import java.util.Queue;
import Cleaning.State;
public class BFS {
private int dirty;
private Queue<State> fifo = new LinkedList<State>();
private String path = "";
private int num_of_expanded_states;
private boolean failure = false;
public BFS(State state, int dirty) {
// TODO Auto-generated constructor stub
this.dirty = dirty;
this.num_of_expanded_states = 0;
this.fifo.add(state);
}
public void startCleaning() {
// TODO Auto-generated method stub
successorFunction(fifo.element());
if(failure){
System.out.println("There is no solution under the constraint of maximum number of dirty slots");
return;
}
}
public void successorFunction(State state){
int x,y,i;
State temp = new State(state);
// state.printRoom();
if(goalTest(state)){
printPath(state);
return;
}
x = state.x;
y = state.y;
// checking valid moves
if(x+3 <= state.room.length-1){
if(y+1 <= state.room.length-1){
if(state.room[x+1][y]==1 && state.room[x+2][y]==1 && state.room[x+3][y]==1 && state.room[x+3][y+1]==1){
for(i=1;i<=3;i++){
temp.room[x+i][y]=0;
}
temp.room[x+i-1][y+1]=0;
temp.x = x+i-1;
temp.y = y+1;
temp.father = new State(state);
temp.action = "3";
fifo.add(temp);
}
}
temp = new State(state);
if(y-1 >= 0){
if(state.room[x+1][y]==1 && state.room[x+2][y]==1 && state.room[x+3][y]==1 && state.room[x+3][y-1]==1){
for(i=1;i<=3;i++){
temp.room[x+i][y]=0;
}
temp.room[x+i-1][y-1]=0;
temp.x = x+i-1;
temp.y = y-1;
temp.father = new State(state);
temp.action = "1";
fifo.add(temp);
}
}
}
temp = new State(state);
if(x-3 >= 0){
if(y+1 <= state.room.length-1){
if(state.room[x-1][y]==1 && state.room[x-2][y]==1 && state.room[x-3][y]==1 && state.room[x-3][y+1]==1){
for(i=1;i<=3;i++){
temp.room[x-i][y]=0;
}
temp.room[x-i+1][y+1]=0;
temp.x = x-i+1;
temp.y = y+1;
temp.father = new State(state);
temp.action = "5";
fifo.add(temp);
}
}
temp = new State(state);
if(y-1 >= 0){
if(state.room[x-1][y]==1 && state.room[x-2][y]==1 && state.room[x-3][y]==1 && state.room[x-3][y-1]==1){
for(i=1;i<=3;i++){
temp.room[x-i][y]=0;
}
temp.room[x-i+1][y-1]=0;
temp.x = x-i+1;
temp.y = y-1;
temp.father = new State(state);
temp.action = "7";
fifo.add(temp);
}
}
}
temp = new State(state);
if(y+3 <= state.room.length-1){
if(x+1 <= state.room.length-1){
if(state.room[x][y+1]==1 && state.room[x][y+2]==1 && state.room[x][y+3]==1 && state.room[x+1][y+3]==1){
for(i=1;i<=3;i++){
temp.room[x][y+i]=0;
}
temp.room[x+1][y+i-1]=0;
temp.x = x+1;
temp.y = y+i-1;
temp.father = new State(state);
temp.action = "2";
fifo.add(temp);
}
}
temp = new State(state);
if(x-1 >= 0){
if(state.room[x][y+1]==1 && state.room[x][y+2]==1 && state.room[x][y+3]==1 && state.room[x-1][y+3]==1){
for(i=1;i<=3;i++){
temp.room[x][y+i-1]=0;
}
temp.room[x-1][y+i-1]=0;
temp.x = x-1;
temp.y = y+i-1;
temp.father = new State(state);
temp.action = "4";
fifo.add(temp);
}
}
}
temp = new State(state);
if(y-3 >= 0){
if(x+1 <= state.room.length-1){
if(state.room[x][y-1]==1 && state.room[x][y-2]==1 && state.room[x][y-3]==1 && state.room[x+1][y-3]==1){
for(i=1;i<=3;i++){
temp.room[x][y-i]=0;
}
temp.room[x+1][y-i+1]=0;
temp.x = x+1;
temp.y = y-i+1;
temp.father = new State(state);
temp.action = "0";
fifo.add(temp);
}
}
temp = new State(state);
if(x-1 >= 0){
if(state.room[x][y-1]==1 && state.room[x][y-2]==1 && state.room[x][y-3]==1 && state.room[x-1][y-3]==1){
for(i=1;i<=3;i++){
temp.room[x][y-i]=0;
}
temp.room[x-1][y-i+1]=0;
temp.x = x-1;
temp.y = y-i+1;
temp.father = new State(state);
temp.action = "6";
fifo.add(temp);
}
}
}
num_of_expanded_states = num_of_expanded_states+1;
// return
fifo.remove();
if(fifo.isEmpty()){
failure = true;
return;
}
else{
successorFunction(fifo.element());
}
}
public boolean goalTest(State state){
int counter = 0;
for (int i=0;i<state.room.length;i++){
for (int j=0;j<state.room.length;j++){
if(state.room[i][j] == 1){
counter++;
}
}
}
if(counter <= dirty){
return true;
}
else{
return false;
}
}
public int pathCost(String path){
if(path.equals(null)){
return 0;
}
path.split("(?!^)");
return path.length();
}
public void printPath(State goal_state){
System.out.println(calculatePath(goal_state));
System.out.println("The number of expanded nodes is: "+num_of_expanded_states);
}
public String calculatePath(State state){
if(state.father==null){
return path;
}
return calculatePath(state.father).concat(state.action);
}
}
这是状态代码:
package Cleaning;
public class State {
public long[][] room;
public State father;
public String action;
public int x;
public int y;
public State(State another){
this.room = new long[another.room.length][another.room.length];
this.father = another.father;
this.x = another.x;
this.y = another.y;
this.action = another.action;
for(int i=0; i<this.room.length; i++)
for(int j=0; j<this.room.length; j++)
this.room[i][j] = another.room[i][j];
}
public State() {
// TODO Auto-generated constructor stub
}
public void initializeState(int dimention){
int i,j;
this.room = new long[dimention][dimention];
this.action = "";
this.father = new State();
father = null;
this.x = dimention - 1;
this.y = 0;
for(i=0;i<dimention;i++){
for(j=0;j<dimention;j++){
if(i == (dimention-1) && j==0){
room[i][j] = 0;
}
else{
room[i][j] = 1;
}
}
}
}
public void printRoom(){
for(int i=0;i<room.length;i++){
for(int j=0;j<room.length;j++){
System.out.print(room[i][j]+" ");
}
System.out.println();
}
}
}
我尝试了一切,但无法解决此异常,请帮忙。并感谢您的帮助
日志:
Exception in thread "main" java.lang.StackOverflowError at
java.util.LinkedList.removeFirst(Unknown Source)
at java.util.LinkedList.remove(Unknown Source) at Strategies.BFS.successorFunction(BFS.java:195)
at Strategies.BFS.successorFunction(BFS.java:201)
at Strategies.BFS.successorFunction(BFS.java:201)
at Strategies.BFS.successorFunction(BFS.java:201)
at Strategies.BFS.successorFunction(BFS.java:201)
at Strategies.BFS.successorFunction(BFS.java:201)
at Strategies.BFS.successorFunction(BFS.java:201)
there is a lot lines like the above one so i can't include all of them
最佳答案
您可以解决 stackover-flow 问题,但将 successFunction 从递归更改为迭代,例如
private void successsFunction(声明另一个){
...
else {
successFunction(fifo.element());
}
}
至
private void successFunction() {
while(!fifo.isEmpty()) {
another = fifo.element();
...
}
}
这将使您的问题从堆栈溢出变成运行缓慢和/或内存不足。
我相信您真正的问题是,由于您标记房间已被访问的方式以及每次复制房间状态以创建新房间的方式,您经过相同房间的次数远远多于您需要的次数状态
来自您的代码
public State(State another){
this.room = new long[another.room.length][another.room.length];
....
for(int i=0; i<this.room.length; i++)
for(int j=0; j<this.room.length; j++)
this.room[i][j] = another.room[i][j];
}
...
private successFunction() {
...
for(i=1;i<=3;i++){
temp.room[x][y+i]=0;
}
这意味着您仅标记该房间以及该房间的所有子级访问过的这些房间。如果我们有一个简单的房子,房间 1 连接到房间 10 和 11,房间 10 连接到房间 11、100 和 101。房间 11 连接到房间 10、110 和 111。
如果你打印出每个 successFunction 开始时你所在的房间,以及 fifo 队列上的房间,你就会明白我的意思。
我建议对此问题的答案是仅拥有一组房间,并让每个州引用该集合。
所以
public State(State another){
this.room = another.room;
....
//for(int i=0; i<this.room.length; i++)
// for(int j=0; j<this.room.length; j++)
// this.room[i][j] = another.room[i][j];
}
关于java - 线程中的异常 "main"java.lang.StackOverflowError 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27052898/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!