Submission #334280

#TimeUsernameProblemLanguageResultExecution timeMemory
334280CaroLindaZoo (COCI19_zoo)C++14
110 / 110
121 ms7316 KiB
#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)

zoo.cpp: In function 'int main()':
zoo.cpp:18:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   18 |  scanf("%d %d", &n , &m ) ;
      |  ~~~~~^~~~~~~~~~~~~~~~~~~
zoo.cpp:20:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   20 |   for(int j = 1 ; j <= m ; j++ ) scanf(" %c", &grid[i][j] ) , dist[i][j] = n+m+1 ;
      |                                  ~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...