문제 링크
1. 아이디어
캠퍼스에서의 도연이의 위치에서부터 BFS나 DFS를 통해 빈 공간이나 사람이 있는 위치로 탐색을 이어나가며 사람이면 개수를 세면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|
| BFS | $O(N \times M)$ | $O(N \times M)$ |
| DFS | $O(N \times M)$ | $O(N \times M)$ |
($N$ = 입력값 n, $M$ = 입력값 m)
3. 코드
풀이 1: BFS [Java][C++]
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
| import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static int n, m;
static char[][] grid;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
grid = new char[n][m];
int sr = -1;
int sc = -1;
for (int i = 0; i < n; i++) {
grid[i] = br.readLine().toCharArray();
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'I') {
sr = i;
sc = j;
}
}
}
int cnt = bfs(sr, sc);
if (cnt > 0) {
System.out.println(cnt);
} else {
System.out.println("TT");
}
}
static int bfs(int sr, int sc) {
Queue<int[]> q = new ArrayDeque<>();
q.offer(new int[]{sr, sc});
boolean[][] vis = new boolean[n][m];
vis[sr][sc] = true;
int cnt = 0;
while (!q.isEmpty()) {
int[] cur = q.poll();
if (grid[cur[0]][cur[1]] == 'P') cnt++;
for (int d = 0; d < 4; d++) {
int nr = cur[0] + dr[d];
int nc = cur[1] + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
if (grid[nr][nc] == 'X' || vis[nr][nc]) continue;
q.offer(new int[]{nr, nc});
vis[nr][nc] = true;
}
}
return cnt;
}
}
|
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
| #include <bits/stdc++.h>
using namespace std;
const int MAX = 600 + 1;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int n, m;
char grid[MAX][MAX];
bool vis[MAX][MAX];
int bfs(int sr, int sc) {
queue<pair<int, int>> q;
q.push({sr, sc});
vis[sr][sc] = true;
int cnt = 0;
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
if (grid[r][c] == 'P') cnt++;
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
if (grid[nr][nc] == 'X' || vis[nr][nc]) continue;
q.push({nr, nc});
vis[nr][nc] = true;
}
}
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
int sr, sc;
for (int i = 0; i < n; i++) {
cin >> grid[i];
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'I') {
sr = i;
sc = j;
}
}
}
int cnt = bfs(sr, sc);
if (cnt > 0) {
cout << cnt;
} else {
cout << "TT";
}
}
|
풀이 2: DFS [Java][C++]
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
| import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
static int n, m;
static char[][] grid;
static boolean[][] vis;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
grid = new char[n][m];
int sr = -1;
int sc = -1;
for (int i = 0; i < n; i++) {
grid[i] = br.readLine().toCharArray();
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'I') {
sr = i;
sc = j;
}
}
}
vis = new boolean[n][m];
int cnt = dfs(sr, sc);
if (cnt > 0) {
System.out.println(cnt);
} else {
System.out.println("TT");
}
}
static int dfs(int r, int c) {
vis[r][c] = true;
int cnt = 0;
if (grid[r][c] == 'P') cnt++;
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
if (grid[nr][nc] == 'X' || vis[nr][nc]) continue;
cnt += dfs(nr, nc);
}
return cnt;
}
}
|
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
| #include <bits/stdc++.h>
using namespace std;
const int MAX = 600 + 1;
int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
int n, m;
char grid[MAX][MAX];
bool vis[MAX][MAX];
int dfs(int r, int c) {
vis[r][c] = true;
int cnt = grid[r][c] == 'P';
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nr >= n || nc < 0 || nc >= m) continue;
if (grid[nr][nc] == 'X' || vis[nr][nc]) continue;
cnt += dfs(nr, nc);
}
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
int sr, sc;
for (int i = 0; i < n; i++) {
cin >> grid[i];
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'I') {
sr = i;
sc = j;
}
}
}
int cnt = dfs(sr, sc);
if (cnt > 0) {
cout << cnt;
} else {
cout << "TT";
}
}
|