이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int H, W;
string meadow[4000];
int val[4000][4000];
queue<pair<int, int>> q;
int switches;
char animal;
bool cnt;
void dfs(int row, int col)
{
if (row < 0 || col < 0 || row == H || col == W || meadow[row][col] == '.')
return;
if (val[row][col] != 0 && cnt)
return;
cnt = true;
if (animal != meadow[row][col])
{
q.push({row, col});
val[row][col] = switches + 1;
return;
}
val[row][col] = switches;
dfs(row + 1, col);
dfs(row - 1, col);
dfs(row, col + 1);
dfs(row, col - 1);
}
int main()
{
cin.tie(NULL) -> sync_with_stdio(false);
cin >> H >> W;
for (auto& i : meadow)
cin >> i;
q.push({0, 0});
val[0][0] = 1;
while (!q.empty())
{
auto [row, col] = q.front();
q.pop();
switches = val[row][col];
animal = meadow[row][col];
cnt = false;
dfs(row, col);
}
int ans = 0;
for (int i = 0; i < H; ++i)
for (int j = 0; j < W; ++j)
ans = max(ans, val[i][j]);
cout << ans << endl;
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |