-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIslands_BFS.cpp
More file actions
150 lines (128 loc) · 2.86 KB
/
Islands_BFS.cpp
File metadata and controls
150 lines (128 loc) · 2.86 KB
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
/*
Objective:
<p>Given a matrix containing 0 and 1. Consider 1 as 'Land' and 0 as 'Water'. Find out the number of 'Islands' in the matrix. That is, set of all adjacent 1 will make up for an island.
<br>
<br>For example:
<br>
<br>[ 0 1 1 0 1 ]
<br>[ 1 1 1 0 0 ]
<br>[ 0 0 0 1 1 ]
<br>[ 1 0 0 1 0 ]
<br>
<br>This problem has 4 islands. ( consider set of 1s, vertically, horizontally and diagonally ).</p>
*/
#include <iostream>
using namespace std;
// To execute C++, please define "int main()"
typedef struct Coord_ts
{
int i;
int j;
} COORD;
typedef struct Node_ts
{
COORD item;
struct Node_ts *next;
struct Node_ts *prev;
} NODE, *NODE_PTR, **NODE_PTR_PTR;
void Enqueue(NODE_PTR_PTR head, COORD item)
{
if(NULL == *head)
{
*head = (NODE_PTR) malloc(sizeof(NODE));
(*head)->item.i = item.i;
(*head)->item.j = item.j;
(*head)->next = NULL;
(*head)->prev = NULL;
}
else
{
NODE_PTR node = (NODE_PTR) malloc(sizeof(NODE));
node->item.i = item.i;
node->item.j = item.j;
(*head)->prev = node;
node->next = *head;
node->prev = NULL;
*head = node;
}
}
COORD Dequeue(NODE_PTR_PTR head)
{
NODE_PTR iter = *head;
COORD item;
while(NULL != iter->next)
{
iter = iter->next;
}
//copy value
item.i = iter->item.i;
item.j = iter->item.j;
//remove the item from the queue
if(NULL == iter->prev)
{
//last node remaining
delete iter;
iter = NULL;
*head = NULL;
}
else
{
iter->prev->next = NULL;
delete iter;
}
return item;
}
#define SEA 0
#define LAND 1
int main() {
int matrix[][5] = {
{SEA, LAND, LAND, SEA, LAND},
{LAND, LAND, LAND, SEA, SEA},
{LAND, LAND, LAND, SEA, SEA},
{SEA, SEA, SEA, LAND, LAND},
{LAND, SEA, SEA, LAND, SEA}
};
COORD item;
NODE_PTR queue = NULL;
int cnt = 0;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
if(LAND == matrix[i][j])
{
cnt++;
cout<<"Found island "<<cnt<<"\n";
matrix[i][j] = SEA;
item.i = i;
item.j = j;
Enqueue(&queue, item);
cout<<"Item ("<<item.i<<","<<item.j<<") added"<<"\n";
while(NULL != queue)//while queue not empty
{
item = Dequeue(&queue);
cout<<"Item ("<<item.i<<","<<item.j<<") removed"<<"\n";
for(int k=item.i-1; k<=item.i+1; k++)
{
for(int l=item.j-1; l<=item.j+1; l++)
{
if(k>=0 && k<5 && l>=0 && l<5)
{
if(LAND==matrix[k][l])
{
matrix[k][l] = SEA;
item.i = k;
item.j = l;
Enqueue(&queue, item);
cout<<"Item ("<<item.i<<","<<item.j<<") added"<<"\n";
}
}
}
}
}
}
}
}
cout<<cnt;
return 0;
}