#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#include <deque>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
ll linf = 1e15+1;
inline void scoobydoobydoo(){
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
vector<pair<int, int> > neighbours;
const int MAXN = 4e3+1;
char arr[MAXN][MAXN];
int isVisited[MAXN][MAXN];
int main(){
scoobydoobydoo();
int r, C; cin >> r >> C;
for (int i = 0; i < r; i++){
for (int j = 0; j < C; j++){
cin >> arr[i][j];
}
}
deque<vector<int> > dq; //x, y, w
dq.push_back({0, 0, 1});
int lastW;
while (!dq.empty()){
int x = dq.front()[0];
int y = dq.front()[1];
int w = dq.front()[2];
dq.pop_front();
if (isVisited[x][y] != 0 && isVisited[x][y] < w)continue;
lastW = w;
char c = arr[x][y];
if (x > 0 && arr[x-1][y] != '.'){
if (arr[x-1][y] == c && (isVisited[x-1][y] > w || isVisited[x-1][y] == 0)){
isVisited[x-1][y] = w;
dq.push_front({x-1, y, w});
} else if ((isVisited[x-1][y] > w+1 || isVisited[x-1][y] == 0)){
isVisited[x-1][y] = w+1;
dq.push_back({x-1, y, w+1});
}
}
if (x < r-1 && arr[x+1][y] != '.'){
if (arr[x+1][y] == c && (isVisited[x+1][y] > w || isVisited[x+1][y] == 0)){
isVisited[x+1][y] = w;
dq.push_front({x+1, y, w});
} else if ((isVisited[x+1][y] > w+1 || isVisited[x+1][y] == 0)){
isVisited[x+1][y] = w+1;
dq.push_back({x+1, y, w+1});
}
}
if (y > 0 && arr[x][y-1] != '.'){
if (arr[x][y-1] == c && (isVisited[x][y-1] > w || isVisited[x][y-1] == 0)){
isVisited[x][y-1] = w;
dq.push_front({x, y-1, w});
} else if ((isVisited[x][y-1] > w+1 || isVisited[x][y-1] == 0)){
isVisited[x][y-1] = w+1;
dq.push_back({x, y-1, w+1});
}
}
if (y < C-1 && arr[x][y+1] != '.'){
if (arr[x][y+1] == c && (isVisited[x][y+1] > w || isVisited[x][y+1] == 0)){
isVisited[x][y+1] = w;
dq.push_front({x, y+1, w});
} else if ((isVisited[x][y+1] > w+1 || isVisited[x][y+1] == 0)){
isVisited[x][y+1] = w+1;
dq.push_back({x, y+1, w+1});
}
}
}
cout << lastW << endl;
return 0;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:95:13: warning: 'lastW' may be used uninitialized in this function [-Wmaybe-uninitialized]
95 | cout << lastW << endl;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
7772 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2904 KB |
Output is correct |
4 |
Correct |
11 ms |
8412 KB |
Output is correct |
5 |
Correct |
3 ms |
3932 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
3 ms |
3676 KB |
Output is correct |
11 |
Correct |
3 ms |
3772 KB |
Output is correct |
12 |
Correct |
7 ms |
4188 KB |
Output is correct |
13 |
Correct |
3 ms |
3932 KB |
Output is correct |
14 |
Correct |
3 ms |
3932 KB |
Output is correct |
15 |
Correct |
16 ms |
7260 KB |
Output is correct |
16 |
Correct |
18 ms |
7772 KB |
Output is correct |
17 |
Correct |
11 ms |
7260 KB |
Output is correct |
18 |
Correct |
11 ms |
8536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
32348 KB |
Output is correct |
2 |
Correct |
53 ms |
14924 KB |
Output is correct |
3 |
Correct |
310 ms |
58208 KB |
Output is correct |
4 |
Correct |
84 ms |
31932 KB |
Output is correct |
5 |
Correct |
202 ms |
47040 KB |
Output is correct |
6 |
Correct |
1107 ms |
167212 KB |
Output is correct |
7 |
Correct |
11 ms |
32856 KB |
Output is correct |
8 |
Correct |
10 ms |
32348 KB |
Output is correct |
9 |
Correct |
3 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
9 ms |
32752 KB |
Output is correct |
12 |
Correct |
2 ms |
3164 KB |
Output is correct |
13 |
Correct |
53 ms |
14972 KB |
Output is correct |
14 |
Correct |
30 ms |
12184 KB |
Output is correct |
15 |
Correct |
22 ms |
14368 KB |
Output is correct |
16 |
Correct |
26 ms |
7772 KB |
Output is correct |
17 |
Correct |
138 ms |
27340 KB |
Output is correct |
18 |
Correct |
110 ms |
34384 KB |
Output is correct |
19 |
Correct |
85 ms |
32308 KB |
Output is correct |
20 |
Correct |
74 ms |
23632 KB |
Output is correct |
21 |
Correct |
188 ms |
45780 KB |
Output is correct |
22 |
Correct |
202 ms |
47060 KB |
Output is correct |
23 |
Correct |
266 ms |
39088 KB |
Output is correct |
24 |
Correct |
185 ms |
43092 KB |
Output is correct |
25 |
Correct |
453 ms |
78788 KB |
Output is correct |
26 |
Correct |
885 ms |
413780 KB |
Output is correct |
27 |
Correct |
954 ms |
206112 KB |
Output is correct |
28 |
Correct |
1080 ms |
175836 KB |
Output is correct |
29 |
Correct |
1077 ms |
177644 KB |
Output is correct |
30 |
Correct |
1014 ms |
185128 KB |
Output is correct |
31 |
Correct |
753 ms |
73208 KB |
Output is correct |
32 |
Correct |
885 ms |
192204 KB |
Output is correct |