-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16724.cpp
69 lines (63 loc) · 1.55 KB
/
16724.cpp
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
68
69
#include<iostream>
using namespace std;
int n, m, answer;
char map[1001][1001];
int visited[1001][1001];
pair<int, int>parent[1001][1001];
pair<int, int> find(pair<int, int>a){
if(parent[a.first][a.second] == a)return a;
return parent[a.first][a.second] = find(parent[a.first][a.second]);
}
pair<int, int>next(int a, int b){
if(map[a][b] == 'D'){
return find({a+1, b});
}
else if(map[a][b] == 'L'){
return find({a, b-1});
}
else if(map[a][b] == 'R'){
return find({a, b+1});
}
return find({a-1, b});
}
void dfs(int a, int b){
pair<int, int>nextLo = next(a, b);
parent[a][b] = nextLo;
if(visited[nextLo.first][nextLo.second] == 0){
visited[nextLo.first][nextLo.second] = 1;
dfs(nextLo.first, nextLo.second);
}
else if(visited[nextLo.first][nextLo.second] == 1){
visited[nextLo.first][nextLo.second] = 2;
return;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> map[i][j];
parent[i][j] = {i, j};
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(visited[i][j] == 0){
visited[i][j] = 1;
dfs(i, j);
}
}
}
pair<int, int>tmp = {-1, -1};
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(visited[i][j] == 2){
answer++;
}
}
}
cout << answer;
}