-
Notifications
You must be signed in to change notification settings - Fork 0
/
isla.c
213 lines (173 loc) · 4.18 KB
/
isla.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "isla.h"
struct _isla {
isla *adj[4];
item used[4][2];
pos *position;
unsigned int name;
unsigned int bridges_avb;
unsigned int bridges_still_avb;
unsigned int dfsed; /* Visited flag for big group operations */
};
isla *create_isla(int name, int y, int x, int bridges_avb)
{
isla *new_isla;
int dir = 0;
int i = 0;
/* Allocate isla */
new_isla = (isla *)malloc(sizeof(isla));
if(new_isla == NULL)
memory_error("Unable to create structure isla");
/* Set isla parameters */
new_isla->position = create_pos(x,y);
new_isla->name = name;
new_isla->bridges_avb = bridges_avb;
new_isla->bridges_still_avb = bridges_avb;
new_isla->dfsed = 0;
for(dir = 0; dir < 4; dir++)
{
new_isla->adj[dir] = NULL;
for(i = 0; i < 2; i++ )
{
new_isla->used[dir][i] = NULL;
}
}
return new_isla;
}
void print_isla(item got_item)
{
isla *got_isla = (isla *)got_item;
fprintf(DEBUG_LOC,
KBLU "Isla name:" RESET " %d "
KBLU "Bridges Avb:" RESET " %d "
KBLU "Bridges still Avb:" RESET " %d ",
got_isla->name, got_isla->bridges_avb, got_isla->bridges_still_avb);
return;
}
int get_isla_name(isla *got_isla)
{
return got_isla->name;
}
int get_isla_bridges_avb(isla *got_isla)
{
return got_isla->bridges_avb;
}
item get_isla_used_bridge(isla *got_isla, int dir, int index)
{
return got_isla->used[dir][index];
}
void set_isla_used_bridge(isla *got_isla, int dir, int index, item connected_bridge)
{
got_isla->used[dir][index] = connected_bridge;
return;
}
pos *get_isla_pos(isla *got_isla)
{
return got_isla->position;
}
isla *get_isla_adj(isla *got_isla, int index)
{
return got_isla->adj[index];
}
int get_isla_dfs_status(isla *got_isla)
{
return got_isla->dfsed;
}
void set_isla_dfs_status(isla *got_isla, int value)
{
got_isla->dfsed = value;
return;
}
int get_isla_bridge_s_avb(isla *got_isla)
{
return got_isla->bridges_still_avb;
}
void dec_isla_bridge_s_avb(isla *got_isla)
{
if(get_isla_bridge_s_avb(got_isla) == 0)
{
printf("You are trying to decrease 0.\n");
return;
}
got_isla->bridges_still_avb --;
return;
}
void inc_isla_bridge_s_avb(isla *got_isla)
{
got_isla->bridges_still_avb ++;
if( get_isla_bridges_avb(got_isla) < get_isla_bridge_s_avb(got_isla) )
{
printf("You are trying to increase beyond bridges_avb.\n");
}
return;
}
void set_isla_adj(isla *got_isla, isla *adj_isla, int index)
{
got_isla->adj[index] = adj_isla;
return;
}
int get_adj_number(isla* new_isla)
{
int n_adj = 0;
int i = 0;
isla* _adj = NULL;
for(i = 0; i < 4; i++)
{
_adj = get_isla_adj(new_isla, i);
if(_adj != NULL)
n_adj++;
}
return n_adj;
}
void reset_dfsed_values(list *isla_list)
{
isla *new_isla;
node *new_node;
new_node = get_head(isla_list);
while(new_node != NULL)
{
new_isla = get_node_item(new_node);
set_isla_dfs_status(new_isla, 0);
new_node = get_next_node(new_node);
}
return;
}
int is_isla_greater_avb(item item_a, item item_b)
{
isla *isla_a;
isla *isla_b;
isla_a = (isla *)item_a;
isla_b = (isla *)item_b;
if(isla_a->bridges_still_avb > isla_b->bridges_still_avb)
{
return 1;
}
return 0;
}
isla *get_isla_for_dfs(list *isla_list)
{
isla *got_isla;
node *aux_node;
for(aux_node = get_head(isla_list); aux_node != NULL; aux_node = get_next_node(aux_node))
{
got_isla = (isla *)get_node_item(aux_node);
if((get_isla_dfs_status(got_isla) < 1) && (get_isla_bridge_s_avb(got_isla) != 0))
{
return got_isla;
}
}
return NULL;
}
bool is_all_dfsed(list *isla_list)
{
isla *got_isla;
node *aux_node;
for(aux_node = get_head(isla_list); aux_node != NULL; aux_node = get_next_node(aux_node))
{
got_isla = (isla *)get_node_item(aux_node);
if((get_isla_dfs_status(got_isla) == 0))
{
return FALSE;
}
}
return TRUE;
}