# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
675780 | NONTAC | Tracks in the Snow (BOI13_tracks) | C++11 | 39 ms | 716 KiB |
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 <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int n, m;
char grid[4002][4002];
int d[4002][4002];
int ans = 0;
bool same(char c1, char c2)
{
if(c1 != c2) return true;
return false;
}
int bfs(int si, int sj)
{
deque<ii> dq;
d[si][sj] = 1;
dq.push_front(ii(si, sj));
while(!dq.empty()){
int ui = dq.front().first, uj = dq.front().second;
dq.pop_front();
ans = max(ans, d[ui][uj]);
for(int i = 0; i < 4; i++){
int vi = ui + dx[i], vj = uj + dy[i];
int w = same(grid[ui][uj], grid[vi][vj]);
if(vi < 1 || vi > n || vj < 1 || vj > m || grid[vi][vj] == '.') continue;
if(d[vi][vj] == INT_MAX){
d[vi][vj] = d[ui][uj] + w;
if(w == 0){
dq.push_front(ii(vi, vj));
}
else{
dq.push_back(ii(vi, vj));
}
}
}
}
}
int main()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
cin>>n>>m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin>>grid[i][j];
d[i][j] = INT_MAX;
}
}
bfs(1, 1);
cout<<ans;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |