-
Notifications
You must be signed in to change notification settings - Fork 2
/
zad9_ms.cpp
140 lines (118 loc) · 3.09 KB
/
zad9_ms.cpp
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
// original: @asajab
// i've studied his code to understand the idea
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
struct p
{
int x, y, level;
};
typedef struct p *Point;
Point create_point(int x, int y, int level)
{
Point newPoint = (Point)malloc(sizeof(p));
newPoint->x = x;
newPoint->y = y;
newPoint->level = level;
return newPoint;
}
vector<vector<bool>> load_map(int x, int y)
{
vector<vector<bool>> ocean;
for (int ys = 0; ys < y; ys++)
{
vector<bool> currentRow;
for (int xs = 0; xs < x; xs++)
{
int i;
cin >> i;
currentRow.push_back((bool)i);
}
ocean.push_back(currentRow);
}
return ocean;
}
// push to deque new point, should levelup?
void enqueue(deque<Point> &toBeVisited, int x, int y, int l, bool levelUp)
{
if (levelUp)
{
toBeVisited.push_back(create_point(x, y, l + 1));
}
else
{
toBeVisited.push_front(create_point(x, y, l));
}
}
bool are_proper_coordinates(int xs, int ys, int x, int y)
{
return xs >= 0 && xs < x && ys >= 0 && ys < y;
}
const int dx[] = {0, 0, 1, -1};
const int dy[] = {-1, 1, 0, 0};
int wyspa(const vector<vector<bool>> &mapa)
{
int y = (int)mapa.size();
if (y <= 0)
return 0;
int x = (int)mapa[0].size();
int maxIsland = -1;
deque<Point> toBeVisited;
vector<vector<bool>> wasVisited(y, vector<bool>(x, false));
for (int i = 0; i < x; i++)
{
// push top line
wasVisited[0][i] = true;
enqueue(toBeVisited, i, 0, 0, mapa[0][i]);
// push bottom line
wasVisited[y - 1][i] = true;
enqueue(toBeVisited, i, y - 1, 0, mapa[y - 1][i]);
}
for (int i = 0; i < y; i++)
{
// push left line
wasVisited[i][0] = true;
enqueue(toBeVisited, 0, i, 0, mapa[i][0]);
// push right line
wasVisited[i][x - 1] = true;
enqueue(toBeVisited, x - 1, i, 0, mapa[i][x - 1]);
}
while (!toBeVisited.empty())
{
Point current = toBeVisited.front();
toBeVisited.pop_front();
if (mapa[current->y][current->x] && current->level > maxIsland)
{
maxIsland = current->level;
}
for (int i = 0; i < 4; ++i)
{
int ys = current->y + dy[i], xs = current->x + dx[i];
if (!are_proper_coordinates(xs, ys, x, y) || wasVisited[ys][xs])
continue;
wasVisited[ys][xs] = true;
// land -> water should not levelup
bool levelUp = mapa[ys][xs] != mapa[current->y][current->x];
if (levelUp && !mapa[ys][xs])
current->level--;
enqueue(toBeVisited, xs, ys, current->level, levelUp);
}
free(current);
}
// cleanup
for(vector<bool> row : wasVisited){
row.clear();
}
wasVisited.clear();
return maxIsland;
}
int main()
{
int x, y;
cin >> y >> x;
vector<vector<bool>> ocean = load_map(x, y);
int maxLevel = wyspa(ocean);
cout << maxLevel << endl;
ocean.clear();
}