#include <bits/stdc++.h>
using namespace std;
#define int long long
int h,w;
int hsh(int i, int j){
// 0 <= i < h
// 0 <= j < w
return (i*w)+j;
}
pair<int,int> unhsh(int x){
return {x/w,x%w};
}
signed main() {
cin >> h >> w;
int grid[h][w];
bool visited[h][w];
for (int i = 0; i<h; i++){
for (int j = 0; j<w; j++){
char a;
cin >> a;
if (a == '.') grid[i][j] = 0;
if (a == 'R') grid[i][j] = 1;
if (a == 'F') grid[i][j] = 2;
visited[i][j] = false;
}
}
vector<int> dx = {0,1,0,-1};
vector<int> dy = {1,0,-1,0};
int mxn = hsh(h-1,w-1);
vector<int> dist(mxn+1, 1e9);
deque<pair<int,int>> q;
q.push_front({0,0});
dist[0] = 1;
int ans = 0;
while (!q.empty()){
pair<int,int> v = q.front();
int vhsh = hsh(v.first,v.second);
q.pop_front();
ans = max(ans,dist[vhsh]);
if (visited[v.first][v.second]) continue;
visited[v.first][v.second] = true;
int r = v.first;
int c = v.second;
for (int k = 0; k<4; k++){
int rr = r + dx[k];
int cc = c + dy[k];
if (rr < 0 || rr >= h || cc < 0 || cc >= w) continue;
if (visited[rr][cc]) continue;
if (grid[rr][cc] == 0) continue;
int khsh = hsh(rr,cc);
if (dist[vhsh] + (grid[r][c] != grid[rr][cc]) < dist[khsh]){
dist[khsh] = dist[vhsh] + (grid[r][c] != grid[rr][cc]);
if (grid[rr][cc] != grid[r][c]){
q.push_back({rr,cc});
}
else{
q.push_front({rr,cc});
}
}
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
4696 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
3424 KB |
Output is correct |
5 |
Correct |
5 ms |
1884 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
448 KB |
Output is correct |
10 |
Correct |
4 ms |
1372 KB |
Output is correct |
11 |
Correct |
3 ms |
1216 KB |
Output is correct |
12 |
Correct |
8 ms |
1884 KB |
Output is correct |
13 |
Correct |
5 ms |
1884 KB |
Output is correct |
14 |
Correct |
5 ms |
1884 KB |
Output is correct |
15 |
Correct |
18 ms |
4700 KB |
Output is correct |
16 |
Correct |
21 ms |
4700 KB |
Output is correct |
17 |
Correct |
16 ms |
4440 KB |
Output is correct |
18 |
Correct |
12 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1116 KB |
Output is correct |
2 |
Correct |
92 ms |
27860 KB |
Output is correct |
3 |
Correct |
780 ms |
281960 KB |
Output is correct |
4 |
Correct |
198 ms |
66216 KB |
Output is correct |
5 |
Correct |
422 ms |
159208 KB |
Output is correct |
6 |
Correct |
1403 ms |
310932 KB |
Output is correct |
7 |
Correct |
3 ms |
1116 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
4 ms |
1372 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
3 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
93 ms |
27856 KB |
Output is correct |
14 |
Correct |
54 ms |
16312 KB |
Output is correct |
15 |
Correct |
55 ms |
18012 KB |
Output is correct |
16 |
Correct |
44 ms |
11604 KB |
Output is correct |
17 |
Correct |
240 ms |
71764 KB |
Output is correct |
18 |
Correct |
231 ms |
70744 KB |
Output is correct |
19 |
Correct |
202 ms |
66388 KB |
Output is correct |
20 |
Correct |
169 ms |
60776 KB |
Output is correct |
21 |
Correct |
441 ms |
164184 KB |
Output is correct |
22 |
Correct |
412 ms |
158748 KB |
Output is correct |
23 |
Correct |
468 ms |
136692 KB |
Output is correct |
24 |
Correct |
430 ms |
160520 KB |
Output is correct |
25 |
Correct |
1083 ms |
282324 KB |
Output is correct |
26 |
Correct |
1285 ms |
316424 KB |
Output is correct |
27 |
Correct |
1238 ms |
333384 KB |
Output is correct |
28 |
Correct |
1319 ms |
311000 KB |
Output is correct |
29 |
Correct |
1336 ms |
305624 KB |
Output is correct |
30 |
Correct |
1286 ms |
313772 KB |
Output is correct |
31 |
Correct |
882 ms |
181844 KB |
Output is correct |
32 |
Correct |
1272 ms |
330840 KB |
Output is correct |