#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
typedef long long ll;
int H, W;
vector<string> grid;
vector<vector<ll>> visited; // bitset ?
//priority_queue<vector<ll>> q;
vector<vector<pair<ll, ll>>> pq_vec;
void dfs(int hat, int wat, ll num)
{
//cerr << hat << " " << wat << " " <<num << "\n";
//if (visited[hat][wat]>0) return;
//if (grid[hat][wat] == '.') return;
//visited[hat][wat] = num;
char ch = grid[hat][wat];
if (hat + 1 < H) {
if (grid[hat + 1][wat] != '.' && visited[hat + 1][wat] < 0)
{
if (grid[hat + 1][wat] == ch) {
pq_vec[num].push_back({hat + 1, wat});
visited[hat][wat] = num;
}
else {
pq_vec[num + 1].push_back({hat + 1, wat});
visited[hat][wat] = num + 1;
}
}
}
if (wat + 1 < W) {
if (grid[hat][wat + 1] != '.' && visited[hat][wat + 1] < 0)
{
if (grid[hat][wat + 1] == ch) {
pq_vec[num].push_back({hat, wat + 1});
visited[hat][wat + 1] == num;
}
else {
pq_vec[num + 1].push_back({hat, wat + 1});
visited[hat][wat + 1] == num + 1;
}
}
}
if (hat - 1 >= 0) {
if (grid[hat - 1][wat] != '.' && visited[hat - 1][wat] < 0)
{
if (grid[hat - 1][wat] == ch) {
pq_vec[num].push_back({hat - 1, wat});
visited[hat - 1][wat] == num;
}
else {
pq_vec[num + 1].push_back({hat - 1, wat});
visited[hat - 1][wat] == num + 1;
}
}
}
if (wat - 1 >= 0) {
if (grid[hat][wat - 1] != '.' && visited[hat][wat - 1] < 0)
{
if (grid[hat][wat - 1] == ch) {
pq_vec[num].push_back({hat, wat - 1});
visited[hat][wat - 1] == num;
}
else {
pq_vec[num + 1].push_back({hat, wat - 1});
visited[hat][wat - 1] == num + 1;
}
}
}
}
int32_t main()
{
cin>>H>>W;
grid.assign(H, "");
rep(i,0,H){
cin>>grid[i];
}
visited.assign(H, vector<ll>(W, -1));
//q.push({-1,0,0});
ll hw10 = H*W;
hw10 += 10;
pq_vec.assign(hw10, {});
pq_vec[1].push_back({0,0});
visited[0][0] = true;
// v
ll i = 1;
while(i < ll(pq_vec.size()))
{
ll j = 0;
while (j < ll(pq_vec[i].size()))
{
pair<ll,ll> x = pq_vec[i][j];
dfs(x.first, x.second, i);
j++;
}
i++;
}
// ^
ll ans = 1;
rep(i,0,H){
rep(j,0,W){
ans = max(ans, visited[i][j]);
}
}
cout << ans;
}
// pq too slow
// H*W , int * int no good, make ll = int * int
// MLE ?
Compilation message
tracks.cpp: In function 'void dfs(int, int, ll)':
tracks.cpp:41:39: warning: value computed is not used [-Wunused-value]
41 | visited[hat][wat + 1] == num;
tracks.cpp:45:39: warning: value computed is not used [-Wunused-value]
45 | visited[hat][wat + 1] == num + 1;
tracks.cpp:54:39: warning: value computed is not used [-Wunused-value]
54 | visited[hat - 1][wat] == num;
tracks.cpp:58:39: warning: value computed is not used [-Wunused-value]
58 | visited[hat - 1][wat] == num + 1;
tracks.cpp:67:39: warning: value computed is not used [-Wunused-value]
67 | visited[hat][wat - 1] == num;
tracks.cpp:71:39: warning: value computed is not used [-Wunused-value]
71 | visited[hat][wat - 1] == num + 1;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1173 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Runtime error |
1012 ms |
1048576 KB |
Execution killed with signal 9 |
3 |
Runtime error |
941 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Runtime error |
1208 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Runtime error |
1159 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Runtime error |
944 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Runtime error |
989 ms |
1048576 KB |
Execution killed with signal 9 |
8 |
Runtime error |
980 ms |
1048576 KB |
Execution killed with signal 9 |
9 |
Runtime error |
949 ms |
1048576 KB |
Execution killed with signal 9 |
10 |
Runtime error |
1191 ms |
1048576 KB |
Execution killed with signal 9 |
11 |
Runtime error |
1058 ms |
1048576 KB |
Execution killed with signal 9 |
12 |
Runtime error |
1032 ms |
1048576 KB |
Execution killed with signal 9 |
13 |
Runtime error |
1188 ms |
1048576 KB |
Execution killed with signal 9 |
14 |
Runtime error |
1192 ms |
1048576 KB |
Execution killed with signal 9 |
15 |
Runtime error |
951 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Runtime error |
1147 ms |
1048576 KB |
Execution killed with signal 9 |
17 |
Runtime error |
1200 ms |
1048576 KB |
Execution killed with signal 9 |
18 |
Runtime error |
1186 ms |
1048576 KB |
Execution killed with signal 9 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
919 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Runtime error |
954 ms |
1048576 KB |
Execution killed with signal 9 |
3 |
Runtime error |
850 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Runtime error |
975 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Runtime error |
988 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Runtime error |
885 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Runtime error |
914 ms |
1048576 KB |
Execution killed with signal 9 |
8 |
Runtime error |
935 ms |
1048576 KB |
Execution killed with signal 9 |
9 |
Runtime error |
930 ms |
1048576 KB |
Execution killed with signal 9 |
10 |
Runtime error |
922 ms |
1048576 KB |
Execution killed with signal 9 |
11 |
Runtime error |
1066 ms |
1048576 KB |
Execution killed with signal 9 |
12 |
Runtime error |
912 ms |
1048576 KB |
Execution killed with signal 9 |
13 |
Runtime error |
948 ms |
1048576 KB |
Execution killed with signal 9 |
14 |
Runtime error |
960 ms |
1048576 KB |
Execution killed with signal 9 |
15 |
Runtime error |
940 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Runtime error |
940 ms |
1048576 KB |
Execution killed with signal 9 |
17 |
Runtime error |
948 ms |
1048576 KB |
Execution killed with signal 9 |
18 |
Runtime error |
936 ms |
1048576 KB |
Execution killed with signal 9 |
19 |
Runtime error |
992 ms |
1048576 KB |
Execution killed with signal 9 |
20 |
Runtime error |
945 ms |
1048576 KB |
Execution killed with signal 9 |
21 |
Runtime error |
1022 ms |
1048576 KB |
Execution killed with signal 9 |
22 |
Runtime error |
1004 ms |
1048576 KB |
Execution killed with signal 9 |
23 |
Runtime error |
1029 ms |
1048576 KB |
Execution killed with signal 9 |
24 |
Runtime error |
991 ms |
1048576 KB |
Execution killed with signal 9 |
25 |
Runtime error |
838 ms |
1048576 KB |
Execution killed with signal 9 |
26 |
Runtime error |
1004 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
849 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
894 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
941 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
1053 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Runtime error |
1137 ms |
1048576 KB |
Execution killed with signal 9 |
32 |
Runtime error |
1057 ms |
1048576 KB |
Execution killed with signal 9 |