main" java.lang.StackOverflowError 栈溢出 求解决 遍历格子

空方块 2013-05-01 10:52:51
[code=java]import java.util.*;
public class ground
{
private short a = 0;
private short b = 0;
private short cwide = 5; //地图的宽
private short clong = 5; //地图的长
private int popSize= 10; //种群数量

//每一个格子对应的各个方向状态
class point
{
boolean x[]= new boolean [4];//右,上,左,下
int array[][] =new int [cwide][clong];
}
public void init(int i)
{
for (int k=0;k<cwide ;k++ )
for (int j=0;j<clong ; j++)
{
cells[i].cell[k][j] = new point();
for (int m=0;m<4 ;m++ )
{
cells[i].cell[k][j].x[m] =false;
}
}
}
private class genotype {
point cell[][] = new point [cwide][clong]; //单个基因序列2维数组
long fitness; //该基因的适应度
}
private genotype[] cells = new genotype[popSize];
//构造函数,初始化
public ground(){
for (int i=0;i<popSize ;i++ ){
cells[i] = new genotype();
cells[i].fitness = 0;
init(i);
ergodic(i,a,b);
}
}
public void zeroSet(int i){
for (int k=0;k<cwide ;k++ ){
for (int j=0;j<clong ; j++){
for (int m=0;m<4 ;m++ ){
cells[i].cell[k][j].x[m] =false;
}
cells[i].cell[k][j].array [k][j]=0;
}
}
ergodic(i,a,b);
}
public void ergodic(int i,int k,int j){
for (int number = 0;number<cwide*clong ;number++ ){
if ((k==0&&j==0)||(k==cwide-1&&j==0)||(k==cwide-1&&j==clong-1)||(k==0&&j==clong-1)){
//左上角条件判断
if(k==0&&j==0){
if (sum(i,k,j+1)*sum(i,k+1,j)!=0){
zeroSet(i);
}else if (sum(i,k,j+1)!=0&&sum(i,k+1,j)==0){
cells[i].cell[k][j].x[3] = true;
cells[i].cell[k][j].array [k][j]= number;
k++;
}else if (sum(i,k+1,j)!=0&&sum(i,k,j+1)==0){
cells[i].cell[k][j].x[0] = true;
cells[i].cell[k][j].array [k][j]= number;
j++;
}else if (sum(i,k,j+1)*sum(i,k+1,j)==0){
int p = 2;
int temp = (int)(Math.random()*p);
switch (temp){
case 0:temp = 3 ;break;
case 1:temp = 0 ;break;
}
cells[i].cell[k][j].x[temp] = true;
cells[i].cell[k][j].array[k][j]= number;
switch (temp){
case 0:j++;break;
case 1:k--;break;
case 2:j--;break;
case 3:k++;break;
default :break;
}}
}else
//左下角条件判断
if(k==cwide-1&&j==0){
if (sum(i,k-1,j)*sum(i,k,j+1)!=0)
{
zeroSet(i);
}else if (sum(i,k-1,j)!=0&&sum(i,k,j+1)==0){
cells[i].cell[k][j].x[0] = true;
cells[i].cell[k][j].array [k][j]= number;
j++;
}else if (sum(i,k,j+1)!=0&&sum(i,k-1,j)==0){
cells[i].cell[k][j].x[1] = true;
cells[i].cell[k][j].array [k][j]= number;
k--;
}else
if (sum(i,k-1,j)*sum(i,k,j+1)==0){
int p = 2;
int temp = (int)(Math.random()*p);
switch (temp){
case 0:temp = 1 ;break;
case 1:temp = 0 ;break;
}
cells[i].cell[k][j].x[temp] = true;
cells[i].cell[k][j].array[k][j]= number;
switch (temp){
case 0:j++;break;
case 1:k--;break;
case 2:j--;break;
case 3:k++;break;
default :break;
}}
}else
//右下角条件判断
if(k==cwide-1&&j==clong-1){
if (sum(i,k-1,j)*sum(i,k,j-1)!=0){
zeroSet(i);
}else
if (sum(i,k-1,j)!=0&&sum(i,k,j-1)==0){
cells[i].cell[k][j].x[2] = true;
cells[i].cell[k][j].array [k][j]= number;
j--;
}else
if (sum(i,k,j-1)!=0&&sum(i,k-1,j)==0){
cells[i].cell[k][j].x[1] = true;
cells[i].cell[k][j].array [k][j]= number;
k--;
}else if (sum(i,k-1,j)*sum(i,k,j-1)==0){
int p = 2;
int temp = (int)(Math.random()*p);
switch (temp){
case 0:temp = 1 ;break;
case 1:temp = 2 ;break;
}
cells[i].cell[k][j].x[temp] = true;
cells[i].cell[k][j].array[k][j]= number;
switch (temp){
case 0:j++;break;
case 1:k--;break;
case 2:j--;break;
case 3:k++;break;
default :break;
}}
}else
//右上角条件判断
if(k==0&&j==clong-1){
if (sum(i,k,j-1)*sum(i,k+1,j)!=0){
zeroSet(i);
}else if (sum(i,k,j-1)!=0&&sum(i,k+1,j)==0){
cells[i].cell[k][j].x[3] = true;
cells[i].cell[k][j].array [k][j]= number;
k++;
}else if (sum(i,k+1,j)!=0&&sum(i,k,j-1)==0){
cells[i].cell[k][j].x[2] = true;
cells[i].cell[k][j].array [k][j]= number;
j--;
}else if (sum(i,k,j-1)*sum(i,k+1,j)==0){
int p = 2;
int temp = (int)(Math.random()*p);
switch (temp){
case 0:temp = 3 ;break;
case 1:temp = 2 ;break;
}
cells[i].cell[k][j].x[temp] = true;
cells[i].cell[k][j].array[k][j]= number;
switch (temp){
case 0:j++;break;
case 1:k--;break;
case 2:j--;break;
case 3:k++;break;
default :break;
}}}
}else
if (k>0&&j>0&&k<cwide-1&&j<clong-1)//中间
{
if (sum(i,k-1,j)*sum(i,k,j-1)*sum(i,k,j+1)*sum(i,k+1,j)!=0){
zeroSet(i);
}
else {
int num=0;
int n[] =new int [4];
for (int g = 0; g<4; g++){
n[g] = 8;
}
if (sum(i,k-1,j)==0) {
n[num]= 1;
num++;
}
if (sum(i,k,j-1)==0){
n[num] = 2;
num++;
}
if (sum(i,k,j+1)==0){
n[num] = 0;
num++;
}
if (sum(i,k+1,j)==0){
n[num] = 3;
num++;
}
int temp = (int)(Math.random() * num);
cells[i].cell[k][j].x[ n[temp]] =true;
cells[i].cell[k][j].array[k][j]= number;
switch (n[temp]){
case 0:j++;break;
case 1:k--;break;
case 2:j--;break;
case 3:k++;break;
default :break;
}
}
}else
if(k>0&&k<cwide-1&&j==0)//左{
if (sum(i,k-1,j)*sum(i,k,j+1)*sum(i,k+1,j)!=0){
zeroSet(i);
}else{
int num=0;
int n[] =new int [3];
for (int g = 0; g<3; g++)
{
n[g] = 8;
}
if (sum(i,k-1,j)==0) {
n[num]= 1;
num++;
}
if (sum(i,k,j+1)==0){
n[num] = 0;
num++;}
if (sum(i,k+1,j)==0){
n[num] = 3;
num++;}
int temp = (int)(Math.random() * num);
cells[i].cell[k][j].x[ n[temp]] =true;
cells[i].cell[k][j].array[k][j]= number;
switch (n[temp]){
case 0:j++;break;
case 1:k--;break;
case 3:k++;break;
default :break;}}
}else if (k>0&&k<cwide-1&&j==clong-1)//右{
if (sum(i,k-1,j)*sum(i,k,j-1)*sum(i,k+1,j)!=0)
zeroSet(i);
else{
int num=0;
int n[] =new int [3];
for (int g = 0; g<3; g++){
n[g] = 8;}
if (sum(i,k-1,j)==0) {
n[num]= 1;
num++;}
if (sum(i,k,j-1)==0){
n[num] = 2;
num++;}
if (sum(i,k+1,j)==0){
n[num] = 3;
num++;}
int temp = (int)(Math.random() * num);
cells[i].cell[k][j].x[ n[temp]] =true;
cells[i].cell[k][j].array[k][j]= number;
switch (n[temp]){
case 1:k--;break;
case 2:j--;break;
case 3:k++;break;
default :break;
}}
}else if (j>0&&j<clong-1&&k==0)//上{
if (sum(i,k,j+1)*sum(i,k,j-1)*sum(i,k+1,j)!=0)
zeroSet(i);
else {
int num=0;
int n[] =new int [3];
for (int g = 0; g<3; g++){
n[g] = 8;}
if (sum(i,k,j-1)==0){
n[num] = 2;
num++;}
if (sum(i,k,j+1)==0){
n[num] = 0;
num++;}
if (sum(i,k+1,j)==0){
n[num] = 3;
num++;}
int temp = (int)(Math.random() * num);
cells[i].cell[k][j].x[ n[temp]] =true;
cells[i].cell[k][j].array[k][j]= number;
switch (n[temp]){
case 0:j++;break;
case 2:j--;break;
case 3:k++;break;
default :break;
}}
}else if (j>0&&j<clong-1&&k==cwide-1)//下{
if (sum(i,k-1,j)*sum(i,k,j-1)*sum(i,k,j+1)!=0)
zeroSet(i);
else {
int num=0;
int n[] =new int [3];
for (int g = 0; g<3; g++){
n[g] = 8;}
if (sum(i,k-1,j)==0){
n[num]= 1;
num++;}
if (sum(i,k,j-1)==0){
n[num] = 2;
num++;}
if (sum(i,k,j+1)==0){
n[num] = 0;
num++;}
int temp = (int)(Math.random() * num);

cells[i].cell[k][j].x[ n[temp]] =true;
cells[i].cell[k][j].array[k][j]= number;
switch (n[temp])
{
case 0:j++;break;
case 1:k--;break;
case 2:j--;break;
default :break;
}}}}}
//计算一个点的方向量和
public short sum(int i,int k,int j){
short p=1;
boolean temp = false;
for (int m=0;m<4 ; m++)
temp = cells[i].cell[k][j].x[m]||temp;
if (!temp) p=0;return p;}
//计算每个种群每个基因个体的适应度
public void CalAll() {CalFitness();}
//计算所有路线的适应度
public void CalFitness()
{
int p= 0;
for (int i=0;i<popSize ;i++ )
{
for (int k=1;k<cwide-1 ;k++ )
{
for (int j=1;j<clong-1 ;j++ )
{
if(cells[i].cell[k][j+1].x[1]||cells[i].cell[k][j+1].x[3]&&cells[i].cell[k][j].x[0]){p++;}
if(cells[i].cell[k][j-1].x[3]||cells[i].cell[k][j-1].x[1]&&cells[i].cell[k][j].x[2]){p++;}
if(cells[i].cell[k][j-1].x[2]||cells[i].cell[k][j-1].x[0]&&cells[i].cell[k][j].x[1]){p++;}
if(cells[i].cell[k][j-1].x[2]||cells[i].cell[k][j-1].x[0]&&cells[i].cell[k][j].x[3]){p++;}

cells[i].fitness =p;
}}}}
//打印任意代最优的路径序列
public void printBestRoute() {
CalAll();
long temp = cells[0].fitness;
int index = 0;
for (int i=1;i<popSize;i++) {
if (cells[i].fitness<temp) {
temp = cells[i].fitness;
index = i;
}}
System.out.println("最佳路径的序列:");
for (int k=0;k<cwide ;k++ )
{
for (int j=0;j<clong ;j++ )
{
System.out.print(" /t"+cells[index].cell[k][j].array [k][j]+" /t");

}
System.out.println(" ");
}}
public void run()
{
printBestRoute();
}
/**
* @param a 开始时间
* @param b 结束时间
*/
public void CalTime(Calendar a, Calendar b) {
long x = b.getTimeInMillis()-a.getTimeInMillis();
long y = x/1000;
x = x-1000*y;
System.out.println("算法执行时间:"+y+"."+x+"秒");
}
public static void main(String[] args) {
Calendar a = Calendar.getInstance(); //开始时间
ground gd = new ground();
gd.run();
Calendar b = Calendar.getInstance(); //结束时间
gd.CalTime(a,b);
} }
...全文
130 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fei1710 2013-05-01
  • 打赏
  • 举报
回复
看下是不是无限循环导致栈内存耗尽, 或者增加jvm启动参数提高栈内存大小
LCore 2013-05-01
  • 打赏
  • 举报
回复
<span style="color: #FF0000;">ergodic(i, a, b);</span>
LCore 2013-05-01
  • 打赏
  • 举报
回复
引用 1 楼 fei1710 的回复:
看下是不是无限循环导致栈内存耗尽, 或者增加jvm启动参数提高栈内存大小
看了一下代码,问题出在这里
public void zeroSet(int i) {
		for (int k = 0; k < cwide; k++) {
			for (int j = 0; j < clong; j++) {
				for (int m = 0; m < 4; m++) {
					cells[i].cell[k][j].x[m] = false;
				}
				cells[i].cell[k][j].array[k][j] = 0;
			}
		}
		ergodic(i, a, b);
	}
这里出现了递归,但是这是楼主希望的吗?

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧