-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixshapes.cpp
More file actions
executable file
·184 lines (159 loc) · 5 KB
/
matrixshapes.cpp
File metadata and controls
executable file
·184 lines (159 loc) · 5 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
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
/*
* matrixshapes.cpp
*
* Created on: 2013-11-09
* Author: Rong Xiao
*/
/*
* Count the shapes in mxn integer matrix M
* A shape is a 'continuous' region in the matrix consists of the same number
*/
#include <utility>
#include <bitset>
#include <stack>
#include <vector>
#include <cstdlib>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::pair;
using std::bitset;
using std::stack;
using std::vector;
typedef pair<size_t, size_t> Index;
/*
* visit all indices in the shape containing i, add un visited indices adjacent to the shape to startings stack,
* for future exploring
*/
void exploreShape(const Index& i, int** M, size_t m, size_t n, vector<bool>& visited, stack<Index>& startings) {
int val = M[i.first][i.second];
visited[i.first*m+i.second] = true; // mark index i as visited
stack<Index> toExplore;
toExplore.push(i);
while(true) {
if (toExplore.empty()) {
break;
}
Index j = toExplore.top();
toExplore.pop();
size_t x = j.first;
size_t y = j.second;
// check all 8 neighbors of i
if (x+1<m) {
if (!visited[(x+1)*m+y]) {
if (val == M[x+1][y]) {
visited[(x+1)*m+y] = true;
toExplore.push(Index(x+1,y));
} else {
startings.push(Index(x+1,y));
}
}
if (y+1<n && !visited[(x+1)*m+y+1]) {
if (val == M[x+1][y+1]) {
visited[(x+1)*m+y+1] = true;
toExplore.push(Index(x+1,y+1));
} else {
startings.push(Index(x+1,y+1));
}
}
if (y>0 && !visited[(x+1)*m+y-1]) {
if (val == M[x+1][y-1]) {
visited[(x+1)*m+y-1] = true;
toExplore.push(Index(x+1,y-1));
} else {
startings.push(Index(x+1,y-1));
}
}
}
if (x>0) {
if (!visited[(x-1)*m+y]) {
if (val == M[x-1][y]) {
visited[(x-1)*m+y] = true;
toExplore.push(Index(x-1,y));
} else {
startings.push(Index(x-1,y));
}
}
if (y+1<n && !visited[(x-1)*m+y+1]) {
if (val == M[x-1][y+1]) {
visited[(x-1)*m+y+1] = true;
toExplore.push(Index(x-1,y+1));
} else {
startings.push(Index(x-1,y+1));
}
}
if (y>0 && !visited[(x-1)*m+y-1]) {
if (val == M[x-1][y-1]) {
visited[(x-1)*m+y-1] = true;
toExplore.push(Index(x-1,y-1));
} else {
startings.push(Index(x-1,y-1));
}
}
}
if (y+1<n && !visited[x*m+y+1]) {
if (val == M[x][y+1]) {
visited[x*m+y+1] = true;
toExplore.push(Index(x,y+1));
} else {
startings.push(Index(x,y+1));
}
}
if (y>0 && !visited[x*m+y-1]) {
if (val == M[x][y-1]) {
visited[x*m+y-1] = true;
toExplore.push(Index(x,y-1));
} else {
startings.push(Index(x,y-1));
}
}
}
}
/*
* count number of shapes
* Complexity: O(m*n) time and space, each index will be checked at most 9 times
*/
size_t countShapes(int** M, size_t m, size_t n) {
//maintain potential start index stack, with initial index [0][0]
stack<Index> startings;
startings.push(Index(0,0));
// visited marks
vector<bool> visited(m*n, false);
size_t count = 0;
while (true) {
if (startings.empty()) {
break;
} else {
Index i = startings.top();
startings.pop();
if (!visited[i.first*m+i.second]) {
++count;
exploreShape(i, M, m, n, visited, startings); // visit all indices in the shape containing i
}
}
}
return count;
}
int main(int argc, char** argv) {
size_t m = static_cast<size_t>(atoi(argv[1]));
size_t n = static_cast<size_t>(atoi(argv[2]));
// allocate mem for the mxn matrix
int **M = new int*[m];
for (size_t i = 0; i < m; ++i) {
M[i] = new int[n];
}
cout << "Please input your " << m << " by " << n << " matrix row by row" << endl;
for (size_t i = 0; i < m; ++i) {
for (size_t j = 0; j < n; ++j) {
cin >> M[i][j];
}
}
cout << "There are " << countShapes(M, m, n) << " shapes" << endl;
// free matrix mem
for (size_t i = 0; i < m; ++i) {
delete [] M[i];
}
delete [] M;
return 0;
}