-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathANTEATER.cpp
More file actions
103 lines (91 loc) · 2.36 KB
/
ANTEATER.cpp
File metadata and controls
103 lines (91 loc) · 2.36 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
using namespace std;
int CountAntsMeet(int R, int C, vector<string> &board)
{
vector < int > pos[R*C];
for(int i = 0; i < R; i++)
{
for(int j = 0; j < C; j++)
{
if(board[i][j] == 'U' || board[i][j] == 'D' || board[i][j] == 'L' || board[i][j] == 'R')
{
pos[i*C + j].push_back(1);
}
}
}
int p, q, temp;
for(int i = 0; i < R; i++)
{
for(int j = 0; j < C; j++)
{
p = i, q = j, temp = 2;
if(board[i][j] == 'U')
{
p = p-1;
while(p >= 0 && p < R && q >= 0 && q < C && board[p][q] != '#')
{
pos[p*C + q].push_back(temp++);
p--;
}
}
else if(board[i][j] == 'R')
{
q = q + 1;
while(p >= 0 && p < R && q >= 0 && q < C && board[p][q] != '#')
{
pos[p*C + q].push_back(temp++);
q++;
}
}
else if(board[i][j] == 'D')
{
p = p+1;
while(p >= 0 && p < R && q >= 0 && q < C && board[p][q] != '#')
{
pos[p*C + q].push_back(temp++);
p++;
}
}
else if(board[i][j] == 'L')
{
q = q-1;
while(p >= 0 && p < R && q >= 0 && q < C && board[p][q] != '#')
{
pos[p*C + q].push_back(temp++);
q--;
}
}
}
}
int res = 0, cnt;
for(int i = 0; i < R*C; i++)
{
sort(pos[i].begin(), pos[i].end());
for(int j = 1; j < pos[i].size(); j++)
{
cnt = 1;
while(j != pos[i].size() && pos[i][j] == pos[i][j-1])
{
cnt++;
j++;
}
res += ((cnt)*(cnt-1))/2;
}
}
return res;
}
int main()
{
int t;
cin >> t;
while(t--)
{
int R, C;
cin >> R >> C;
vector<string> board(R);
for (int i = 0; i < R; i++)
cin >> board[i];
cout << CountAntsMeet(R, C, board) << endl;
}
return 0;
}