-
Notifications
You must be signed in to change notification settings - Fork 0
/
1030 Matrix Cells in Distance Order.c
106 lines (84 loc) · 2.29 KB
/
1030 Matrix Cells in Distance Order.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
struct Node
{
int x_cor;
int y_cor;
struct Node * next;
};
struct Node * hashtable[201] = {NULL};
void insert_hashtable(int x, int y, int diff)
{
struct Node * new_node = (struct Node *)malloc(sizeof(struct Node));
new_node->x_cor = x;
new_node->y_cor = y;
new_node->next = NULL;
struct Node * trav_ptr = hashtable[diff];
if(trav_ptr == NULL) //Entry is empty
{
hashtable[diff] = new_node;
}
else
{
while(trav_ptr->next != NULL)
{
trav_ptr = trav_ptr->next;
}
trav_ptr->next = new_node;
}
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** allCellsDistOrder(int rows, int cols, int rCenter, int cCenter, int* returnSize, int** returnColumnSizes)
{
int i=0;
for(i=0; i<201; i++)
{
hashtable[i] = NULL;
}
int ** ret_arr = (int **)malloc(sizeof(int *) * (rows * cols));
int ret_arr_index = 0;
int * ret_col = (int *)malloc(sizeof(int) * (rows * cols));
int ret_col_index = 0;
int j=0;
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
int x_diff = (rCenter - i);
if(x_diff < 0)
{
x_diff *= -1;
}
int y_diff = (cCenter - j);
if(y_diff < 0)
{
y_diff *= -1;
}
int diff = x_diff + y_diff;
insert_hashtable(i, j, diff);
}
}
int hash_index = 0;
struct Node * trav_ptr = hashtable[hash_index++];
for(i=0; i<(rows*cols); i++)
{
int * arr = (int *)malloc(sizeof(int) * 2);
if(trav_ptr!=NULL)
{
arr[0] = trav_ptr->x_cor;
arr[1] = trav_ptr->y_cor;
trav_ptr = trav_ptr->next;
ret_arr[ret_arr_index++] = arr;
ret_col[ret_col_index++] = 2;
}
if(trav_ptr == NULL)
{
trav_ptr = hashtable[hash_index++];
}
}
*returnSize = ret_arr_index;
*returnColumnSizes = ret_col;
return ret_arr;
}