-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathamoebas.cpp
112 lines (104 loc) · 1.8 KB
/
amoebas.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
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_set>
#include <ctype.h>
#include <queue>
#include <map>
#include <numeric>
#include <set>
typedef long long ll;
using namespace std;
void fillAmoeba(vector<string> & dish, vector<vector<bool>> & visited,
ll x, ll y, ll & rows, ll & cols);
int main()
{
ll i, j, k;
ll rows, cols;
ll num = 0;
cin >> rows >> cols;
vector<string> dish(rows);
vector<vector<bool>> visited (rows);
for (i = 0; i < rows; i++)
{
cin >> dish[i];
for (j = 0; j < cols; j++)
{
visited[i].push_back(false);
}
}
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (dish[i][j] == '#' && !visited[i][j])
{
fillAmoeba(dish, visited, i, j, rows, cols);
num++;
}
}
}
cout << num << "\n";
return 0;
}
void fillAmoeba(vector<string> & dish, vector<vector<bool>> & visited,
ll x, ll y, ll & rows, ll & cols)
{
bool found = false;
visited[x][y] = true;
ll tempX, tempY;
while (!found)
{
found = true;
for (int i = 0; i < 8; i++)
{
switch (i)
{
case 0:
tempX = x;
tempY = y + 1;
break;
case 1:
tempX = x + 1;
tempY = y + 1;
break;
case 2:
tempX = x + 1;
tempY = y;
break;
case 3:
tempX = x + 1;
tempY = y - 1;
break;
case 4:
tempX = x;
tempY = y - 1;
break;
case 5:
tempX = x - 1;
tempY = y - 1;
break;
case 6:
tempX = x - 1;
tempY = y;
break;
case 7:
tempX = x - 1;
tempY = y + 1;
break;
};
if (tempX >= 0 && tempX < rows && tempY >= 0 &&
tempY < cols && dish[tempX][tempY] == '#' && !visited[tempX][tempY])
{
x = tempX;
y = tempY;
visited[x][y] = true;
found = false;
}
}
}
}