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