[問題] Connected Component Label使用BFS
遇到的問題: (題意請描述清楚)
1. free()了front以及rear所指向的位址為何只有front呈現NULL!?
2. 在do while時,malloc()會產生尚未free()的位址!?
希望得到的正確結果:
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0
0 1 1 1 1 1 0 0 0 0
0 1 1 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 2 2 2 2 2 0 0
0 0 0 2 2 2 2 2 0 0
0 0 0 0 0 0 0 0 0 0
程式跑出來的錯誤結果:
Candidate 1
add()
new_data: 3e2430 front: 0 rear: 0
front: 3e2430 rear: 3e2430
do while
get()
*i: 0 *j: 0 *flag: 3e2430 rear: 3e2430
*i: 1 *j: 1 *flag: 0 rear: 3e2430
add()
new_data: 3e2448 front: 0 rear: 3e2430
front: 3e2448 rear: 3e2448
add()
new_data: 3e2460 front: 3e2448 rear: 3e2448
front: 3e2448 rear: 3e2460
add()
new_data: 3e2478 front: 3e2448 rear: 3e2460
front: 3e2448 rear: 3e2478
do while
get()
*i: 1 *j: 1 *flag: 3e2448 rear: 3e2478
*i: 1 *j: 2 *flag: 3e2460 rear: 3e2478
add()
new_data: 3e2460 front: 3e2460 rear: 3e2478
front: 3e2460 rear: 3e2460
add()
new_data: 3e2490 front: 3e2460 rear: 3e2460
front: 3e2460 rear: 3e2490
do while
get()
*i: 1 *j: 2 *flag: 3e2460 rear: 3e2490
*i: 1 *j: 3 *flag: 3e2490 rear: 3e2490
add()
new_data: 3e2490 front: 3e2490 rear: 3e2490
front: 3e2490 rear: 3e2490
add()
new_data: 3e24a8 front: 3e2490 rear: 3e2490
front: 3e2490 rear: 3e24a8
.......
......
.....
....
...
..
.
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0
0 1 1 1 1 1 0 0 0 0
0 2 2 2 2 2 2 2 0 0
0 2 2 2 2 2 2 2 0 0
0 0 0 3 3 3 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 4 4 4 4 4 0 0
0 0 0 4 4 4 4 4 0 0
0 0 0 0 0 0 0 0 0 0
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
DEV-C++ 4.9.9.2
Windows XP
有問題的code: (請善用置底文標色功能)
#include <stdio.h>
#include <stdlib.h>
typedef struct queue{
int offset_i, offset_j;
struct queue *next;
}queue;
queue *front = NULL, *rear = NULL;
void add(int [][10], int, int, int);
void get(int*, int*);
int main()
{
int i, j, x = 0;
int n = 0, m = 0, flag = 0;
int block = 1;
int arry[10][10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 5, 5, 5, 5, 5, 0, 0, 0, 0},
{0, 5, 5, 5, 5, 5, 0, 0, 0, 0},
{0, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 5, 5, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 0, 5, 5, 5, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 0, 5, 5, 5, 5, 5, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
for(i = 0; i <= 9; i++)
for(j = 0 ; j <= 9; j++){
if(5 == arry[i][j]){
printf("\nCandidate %d\n", block);
add(arry, block, i, j);
do{
puts("do while");
get(&n ,&m);
// system("PAUSE");
if(5 == arry[n - 1][m - 1])
add(arry, block, n - 1, m - 1);
if(5 == arry[n - 1][m])
add(arry, block, n - 1, m);
if(5 == arry[n - 1][m + 1])
add(arry, block, n - 1, m + 1);
if(5 == arry[n][m - 1])
add(arry, block, n, m - 1);
if(5 == arry[n][m + 1])
add(arry, block, n, m + 1);
if(5 == arry[n + 1][m - 1])
add(arry, block, n + 1, m - 1);
if(5 == arry[n + 1][m])
add(arry, block, n + 1, m);
if(5 == arry[n + 1][m + 1])
add(arry, block, n + 1, m + 1);
}while(front != NULL);
block++;
}
}
for(i = 0; i <= 9; i++)
for(j = 0 ; j <= 9; j++){
x++;
printf("%d\t", arry[i][j]);
if(10 == x){
puts("");
x = 0;
}
}
system("PAUSE");
return 0;
}
void add(int temp[][10], int block, int i, int j)
{
queue *new_data;
new_data = (queue*)malloc(sizeof(queue));
puts("add()");
printf("new_data: %x\tfront: %x\trear: %x\n",new_data, front, rear);
temp[i][j] = block;
new_data -> offset_i = i;
new_data -> offset_j = j;
if(front == NULL)
front = new_data;
else
rear -> next = new_data;
rear = new_data;
new_data -> next = NULL;
printf("front: %x\trear: %x\n", front, rear);
return;
}
void get(int *i, int *j)
{
queue *free_data;
puts("get()");
printf("*i: %d *j: %d *flag: %x rear: %x\n", *i, *j, front, rear);
if(front == NULL)
return;
else{
*i = front -> offset_i;
*j = front -> offset_j;
front = front -> next;
free_data = front;
free(free_data);
printf("*i: %d *j: %d *flag: %x rear: %x\n", *i, *j, front, rear);
}
return;
}
補充說明:
想了好久,也debug好久還是不知所云.
所以上來請教各位版上的先進,
小弟哪邊寫不好還是觀念不正確或是...
謝謝!!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.64.210.117
※ 編輯: MaconChou 來自: 61.64.210.117 (10/03 00:48)
※ 編輯: MaconChou 來自: 61.64.210.117 (10/03 00:50)
推
10/03 10:33, , 1F
10/03 10:33, 1F
→
10/03 10:33, , 2F
10/03 10:33, 2F
推
10/03 11:43, , 3F
10/03 11:43, 3F
→
10/03 23:37, , 4F
10/03 23:37, 4F
→
10/03 23:40, , 5F
10/03 23:40, 5F
→
10/03 23:40, , 6F
10/03 23:40, 6F
→
10/03 23:42, , 7F
10/03 23:42, 7F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章