File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You canβt perform that action at this time.
0 commit comments