Skip to content

Commit 80589ef

Browse files
authored
🐜 Study: 무인도 μ—¬ν–‰ (#101)
1 parent aab04e1 commit 80589ef

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <string>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
int dx[4] = {-1, 0, 0, 1};
8+
int dy[4] = {0, -1, 1, 0};
9+
10+
int dfs(int x, int y, vector<string> &m){
11+
if(x < 0 || x >= m.size()) return 0;
12+
if(y < 0 || y >= m[x].size()) return 0;
13+
if(m[x][y] == 'X') return 0;
14+
int sum = m[x][y] - '0';
15+
m[x][y] = 'X';
16+
for(int i = 0; i < 4; i++){
17+
sum += dfs(x + dx[i], y + dy[i], m);
18+
}
19+
return sum;
20+
}
21+
22+
vector<int> solution(vector<string> maps) {
23+
vector<int> answer;
24+
for(int i = 0; i < maps.size(); i++)
25+
for(int j = 0; j < maps[i].size(); j++)
26+
if(maps[i][j] != 'X')
27+
answer.push_back(dfs(i, j, maps));
28+
if(answer.empty()) return {-1};
29+
sort(answer.begin(), answer.end());
30+
return answer;
31+
}

0 commit comments

Comments
Β (0)