【发布时间】:2017-11-25 21:04:34
【问题描述】:
我尝试随机生成一个迷宫,但是在编译下面的程序时出现分段错误
这是代码
void spread(int v, int x, int y, int *t,int w, int *count){
//table of directions(Right,Down,Left,Up)
int d[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int i;
t[y * w + x] = v;
if(v == 0 && (x & 1) && (y & 1))
(*count)++; /*increments at every box which has value as zero with x odd and y also*/
//spread the value to all directions
for(i = 0; i < 4; i++){
if(v < t[(y+d[i][1]) * w + x +d[i][0]]){
spread(v,x + d[i][0],y+d[i][0],t,w,count);
}
}
}
int *init(int m, int n){
//Initializing the maze
int *t = NULL, mp = 2 * m +1, np = 2 * n + 1;
int x,y,k,d;
int count = 1;
t = malloc(mp * np * sizeof *t);
assert(t);
for(y = k = 0; y < np ;++y){
for(x = 0; x < mp; ++x){
if((x & 1) && (y & 1))
t[y * mp + x] = k++;
else
t[y * mp + x] = -1;
}
}
//Make a labyrinth randomly
while(count < (m * n)){
srand(time(NULL));
if(myRand(2)){ // Up/Down separator
do{
x = myRand(m) * 2 + 1;
y = (myRand(n - 1) + 1) * 2;
}while(t[(y - 1) * mp + x] == t[(y + 1) * mp + x]); /*Don't select the ones which are equal*/
d = t[(y - 1) * mp + x] - t[(y + 1) * mp + x];
//d selects the lowest one
if(d > 0){
t[y * mp +x] = t[(y + 1) * mp + x];
spread(t[(y + 1) * mp +x],x,y-1,t,mp,&count);
}
else if(d < 0){
t[y * mp +x] = t[(y - 1) * mp + x];
spread(t[(y - 1) * mp +x],x,y+1,t,mp,&count);
}
}
else{ //Right/Left separator
do{
x = (myRand(m - 1) + 1) * 2;
y = myRand(n) * 2 + 1;
}while(t[y * mp + x - 1] == t[y * mp + x + 1]);
d = t[y * mp + x - 1] - t[y * mp + x + 1];
if(d > 0){
t[y * mp +x] = t[y * mp + x + 1];
spread(t[y * mp + x + 1],x-1,y,t,mp,&count);
}
else if(d < 0){
t[y * mp +x] = t[y * mp +x - 1];
spread(t[y * mp + x - 1],x+1,y,t,mp,&count);
}
}
}
return t;
}
迷宫首先被初始化(这里是 t),其值为:-1 表示墙壁,v > 0 表示节点
然后为了在节点之间建立连接(这是为了有一个迷宫),随机选择一个框,但是行或列必须是奇数才能选择代表分隔符的墙(上/下或右/左)
所以“d”取墙壁周围盒子之间的最小值(表示墙壁上方盒子的值和墙壁下方盒子的值之间的最小值,与右/左相同)
函数spread:将一个值传播到所有方向(右、下、左、上)
这是一个例子:
+ + + + + + + + +
+ 0 + 1 + 2 + 3 +
+ + + + + + + + +
+ 4 + 5 + 6 + 7 +
+ + + + + + + + +
+ 8 + 9 + 10 + 11 +
+ + + + + + + + +
将某些价值观传播给其他人时
+ + + + + + + + +
+ 0 + 1 1 1 + 3 +
+ + + + + + + + +
+ 4 + 5 + 6 + 7 +
+ + + + + + + + +
+ 8 + 9 9 9 + 11 +
+ + + + + + + + +
我试图通过调试程序来解决问题,它工作了一会儿我的意思是显示上面的内容,但后来我得到了这个
Program received signal SIGSEGV, Segmentation fault.
0x00000000004008e4 in spread (
v=<error reading variable: Cannot access memory at address 0x7fffff7fefec>, x=<error reading variable: Cannot access memory at address 0x7fffff7fefe8>,
y=<error reading variable: Cannot access memory at address 0x7fffff7fefe4>, t=<error reading variable: Cannot access memory at address 0x7fffff7fefd8>,
w=<error reading variable: Cannot access memory at address 0x7fffff7fefe0>, count=<error reading variable: Cannot access memory at address 0x7fffff7fefd0>)
at Lab.c:34
#1 0x0000000000400a1a in spread (v=8, x=4, y=6, t=0x603010, w=9,
count=0x7fffffffddd4) at Lab.c:48
#2 0x0000000000400a1a in spread (v=8, x=4, y=6, t=0x603010, w=9,
count=0x7fffffffddd4) at Lab.c:48
#3 0x0000000000400a1a in spread (v=8, x=4, y=6, t=0x603010, w=9,
count=0x7fffffffddd4) at Lab.c:48
#4 0x0000000000400a1a in spread (v=8, x=4, y=6, t=0x603010, w=9,
count=0x7fffffffddd4) at Lab.c:48
【问题讨论】:
-
怀疑无限递归
-
什么保证
y * w + x在t的范围内? -
x 不能超过 mp(y 也不能超过 np)所以我认为 y * w + x 也不能超过 mp * np 的值
-
为什么你认为它超出了t的限制
-
如果我理解得很好,在
spread函数中,如果v != 0,你跳过第一个if语句,进入for循环。然后,如果当前块旁边的任何块(与值v相关联)与小于或等于v的值相关联,则使用新的当前块(与前一个块相邻)输入spread函数. BUT 由于您不评估x、y或*count的值,因此您可能会:或用完数组的限制,或者再次转轮并再次直到出现Stack Overflow。 (我完全不确定我在说什么)。有人能告诉我我是对还是错吗?
标签: c segmentation-fault maze