This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
vector<vector<char>> field;
vector<vector<int>> turn;
int h, w, answer = 1;
queue<pair<int, int>> points;
void scan(pair<int, int> p)
{
int i = p.first;
int j = p.second;
if(i != 0)
{
if(!turn[i - 1][j] && field[i - 1][j] != '.')
{
if(field[i - 1][j] == field[i][j])
{
turn[i - 1][j] = turn[i][j];
scan({i - 1, j});
}
else
{
turn[i - 1][j] = turn[i][j] + 1;
points.push({i - 1, j});
}
}
}
if(j != 0)
{
if(!turn[i][j - 1] && field[i][j - 1] != '.')
{
if(field[i][j - 1] == field[i][j])
{
turn[i][j - 1] = turn[i][j];
scan({i, j - 1});
}
else
{
turn[i][j - 1] = turn[i][j] + 1;
points.push({i, j - 1});
}
}
}
if(i != h - 1)
{
if(!turn[i + 1][j] && field[i + 1][j] != '.')
{
if(field[i + 1][j] == field[i][j])
{
turn[i + 1][j] = turn[i][j];
scan({i + 1, j});
}
else
{
turn[i + 1][j] = turn[i][j] + 1;
points.push({i + 1, j});
}
}
}
if(j != w - 1)
{
if(!turn[i][j + 1] && field[i][j + 1] != '.')
{
if(field[i][j + 1] == field[i][j])
{
turn[i][j + 1] = turn[i][j];
scan({i, j + 1});
}
else
{
turn[i][j + 1] = turn[i][j] + 1;
points.push({i, j + 1});
}
}
}
}
int main()
{
scanf("%d %d", &h, &w);
field.assign(h, vector<char>(w));
turn.assign(h, vector<int>(w, 0));
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
scanf(" %c", &field[i][j]);
turn[0][0] = 1;
points.push({0, 0});
while(!points.empty())
{
answer = max(answer, turn[points.front().first][points.front().second]);
scan(points.front());
points.pop();
}
printf("%d\n", answer);
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'int main()':
tracks.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d %d", &h, &w);
| ~~~~~^~~~~~~~~~~~~~~~~
tracks.cpp:93:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | scanf(" %c", &field[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |