이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stdio.h>
#include<string>
#include<queue>
using namespace std ;
#define MAXN 4007
int n , m ;
string a[ MAXN ] ;
int used[ MAXN ][ MAXN ] ;
int ans = 0 ;
int dx[ 4 ] = { 0 , 0 , 1 , -1 } ;
int dy[ 4 ] = { 1 , -1 , 0 , 0 } ;
void bfs ( ) {
queue < pair < int , int > > q ;
queue < pair < int , int > > aux ;
int i , j ;
used[ n ][ m ] = 1 ;
q.push ( make_pair ( n , m ) ) ;
while ( 1 ) {
ans ++ ;
while ( q.empty ( ) == false ) {
pair < int , int > p = q.front ( ) ;
q.pop ( ) ;
int i ;
for ( i = 0 ; i < 4 ; i ++ ) {
int nx , ny ;
nx = p.first + dx[ i ] ;
ny = p.second + dy[ i ] ;
if ( nx < 1 || n < nx ) { continue ; }
if ( ny < 1 || m < ny ) { continue ; }
if ( a[ nx ][ ny ] == '.' ) { continue ; }
if ( used[ nx ][ ny ] != 0 ) { continue ; }
used[ nx ][ ny ] = used[ p.first ][ p.second ] + 1 ;
if ( a[ nx ][ ny ] == a[ p.first ][ p.second ] ) {
q.push ( make_pair ( nx , ny ) ) ;
}
else {
aux.push ( make_pair ( nx , ny ) ) ;
}
}
}
if ( aux.empty ( ) == true ) { break ; }
while ( aux.empty ( ) == false ) {
q.push ( aux.front ( ) ) ;
aux.pop ( ) ;
}
}
}
void input ( ) {
cin >> n >> m ;
int i ;
for ( i = 1 ; i <= n ; i ++ ) {
cin >> a[ i ] ;
a[ i ] = '#' + a[ i ] ;
}
}
void solve ( ) {
bfs ( ) ;
printf ( "%d\n" , ans ) ;
}
int main ( ) {
ios_base::sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
input ( ) ;
solve ( ) ;
return 0 ;
}
컴파일 시 표준 에러 (stderr) 메시지
tracks.cpp: In function 'void bfs()':
tracks.cpp:21:9: warning: unused variable 'i' [-Wunused-variable]
int i , j ;
^
tracks.cpp:21:13: warning: unused variable 'j' [-Wunused-variable]
int i , j ;
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |