Skip to content

Commit e16d06d

Browse files
authored
Merge pull request #177 from AlgorithmWithGod/khj20006
[20250225] BOJ / G3 / 미로 탈출하기 / 권혁준
2 parents e116abd + bddf843 commit e16d06d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
```cpp
2+
3+
#include <iostream>
4+
#include <vector>
5+
#include <algorithm>
6+
using namespace std;
7+
using ll = long long;
8+
9+
int N, M;
10+
char A[500][500]{};
11+
int vis[502][502]{};
12+
13+
int dfs(int x, int y) {
14+
if (x < 0 || x >= N || y < 0 || y >= M) return 1;
15+
int xx, yy;
16+
if (A[x][y] == 'U') xx = x - 1, yy = y;
17+
if (A[x][y] == 'D') xx = x + 1, yy = y;
18+
if (A[x][y] == 'L') xx = x, yy = y - 1;
19+
if (A[x][y] == 'R') xx = x, yy = y + 1;
20+
21+
if (xx < 0 || xx >= N || yy < 0 || yy >= M) return vis[x][y] = 1;
22+
if (!vis[xx][yy]) {
23+
vis[xx][yy] = -1;
24+
return vis[x][y] = dfs(xx, yy);
25+
}
26+
return vis[x][y] = vis[xx][yy];
27+
28+
}
29+
30+
int main()
31+
{
32+
cin.tie(0)->sync_with_stdio(0);
33+
34+
cin >> N >> M;
35+
for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) cin >> A[i][j];
36+
37+
for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) if (!vis[i][j]) {
38+
vis[i][j] = -1;
39+
vis[i][j] = dfs(i, j);
40+
}
41+
42+
int ans = 0;
43+
for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) ans += vis[i][j] == 1;
44+
cout << ans;
45+
46+
}
47+
48+
```

0 commit comments

Comments
 (0)