# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
334280 | CaroLinda | Zoo (COCI19_zoo) | C++14 | 121 ms | 7316 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>
const int MAX = 1010 ;
using namespace std ;
int n , m ;
int dx[4] = {1,-1,0,0} ;
int dy[4] = {0,0,1,-1} ;
int dist[MAX][MAX] ;
char grid[MAX][MAX] ;
bool vis[MAX][MAX] ;
bool valid(int i, int j) { return 1 <= i && 1 <= j && i <= n && j <= m ; }
int main()
{
scanf("%d %d", &n , &m ) ;
for(int i = 1 ; i <= n ; i++ )
for(int j = 1 ; j <= m ; j++ ) scanf(" %c", &grid[i][j] ) , dist[i][j] = n+m+1 ;
deque< pair<int,int> > fila ;
fila.push_back( make_pair(1,1) ) ;
dist[1][1] = 1 ;
int ans = 1 ;
while(!fila.empty() )
{
int i = fila[0].first ;
int j = fila[0].second ;
fila.pop_front() ;
if( vis[i][j] ) continue ;
ans = max(ans, dist[i][j] ) ;
vis[i][j] = true ;
for(int g = 0 , ni , nj ; g < 4 ; g++ )
{
ni = i + dx[g] ;
nj = j + dy[g] ;
if(!valid(ni,nj) || grid[ni][nj] == '*' ) continue ;
if( grid[ni][nj] == grid[i][j] )
{
if( dist[ni][nj] <= dist[i][j] ) continue ;
dist[ni][nj] = dist[i][j] ;
fila.push_front( make_pair(ni,nj) ) ;
}
else
{
if( dist[ni][nj] <= dist[i][j] + 1 ) continue ;
dist[ni][nj] = dist[i][j] + 1 ;
fila.push_back( make_pair(ni,nj) ) ;
}
}
}
printf("%d\n", ans ) ;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |