문제 링크: https://www.acmicpc.net/problem/5958
5958번: Space Exploration
Farmer John's cows have finally blasted off from earth and are now floating around space in their Moocraft. The cows want to reach their fiery kin on Jupiter's moon of Io, but to do this they must first navigate through the dangerous asteroid belt. Bessie
www.acmicpc.net
DFS
component의 갯수를 세면 됨.
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
|
#include <bits/stdc++.h>
using namespace std;
int n, v[1001][1001], dy[4] = {-1, 1, 0, 0}, dx[4] = {0, 0, -1, 1};
char a[1001][1001];
void dfs(int y, int x) {
v[y][x] = 1;
for(int d = 0; d < 4; d++) {
int yy = y + dy[d], xx = x + dx[d];
if(yy >= 0 && xx >= 0 && yy < n && xx < n) {
if(!v[yy][xx] && a[yy][xx] == '*')
dfs(yy, xx);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++)
cin >> a[i][j];
int ans = 0;
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) {
if(!v[i][j] && a[i][j] == '*') {
dfs(i, j);
ans++;
}
}
cout << ans << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'USACO > Bronze' 카테고리의 다른 글
백준 5959 Crop Circles (USACO January 2011 Bronze 4번) (0) | 2020.04.08 |
---|---|
백준 5957 Cleaning the Dishes (USACO January 2011 Bronze 2번) (0) | 2020.04.08 |
백준 5956 Symmetry (USACO January 2011 Bronze 1번) (0) | 2020.04.08 |
백준 5949 Adding Commas (USACO December 2010 Bronze 3번) (0) | 2020.04.06 |
백준 5948 Bad Random Numbers (USACO December 2010 Bronze 2번) (0) | 2020.04.06 |