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;
}
코드를 (의) 여기서 벽에 대해서는 -1, 노드에 대해서는 v> 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 +
+ + + + + + + + +
내가 프로그램을 디버깅하여 문제를 해결하려고 * 내가 위에서 무엇을 표시하는 의미 잠시 동안 작동하지만 그때 나는`Y를 보장 이것이
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
있어 w + x'는't'의 경계에 있습니까? – vicatcu
x는 mp를 초과 할 수 없습니다 (y는 np를 초과 할 수 없습니다). 그래서 y * w + x는 mp * np의 값을 초과 할 수 없습니다. – Yasmine