3
時獲取分割故障我嘗試隨機生成迷宮,但在程序編譯如下C:生成隨機迷宮
這裏的時候,我得到一個分段錯誤的
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 0對於節點
然後,爲了在節點之間建立連接(這是爲了具有迷宮),隨機選擇框該行或列必須是奇數才能選擇代表的牆(上/下或左/右)
所以「d」取圍牆周圍的框之間的最小值(意思是牆上方框的值與框下方框的值之間的最小值牆上,左/右),同樣的事情
功能傳播:它傳播的價值,各個方向(右,下,左,上)
下面是它的一個例子:
+ + + + + + + + +
+ 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
可疑無限遞歸 – vicatcu
什麼保證'Y * w + x'是否在't'的範圍內? – vicatcu
x不能超過mp(y不能超過np)所以我認爲y * w + x不能超過mp * np – Yasmine