#include #define mp make_pair #define x first #define y second using namespace std ; //ifstream in ("FloodIt.in") ; //ofstream out ("FloodIt.out") ; int a [ 15 ][ 15 ] ; int di [ 4 ] = { -1 , 0 , 1 , 0 } ; int dj [ 4 ] = { 0 , 1 , 0 , -1 } ; bool viz [ 15 ][ 15 ] , viz2 [ 15 ][ 15 ] , viz3 [ 15 ][ 15 ] ; string s ; bool ok ( int i , int j ) { return ( i && j && i <=14 && j <= 14 ) ; } void Fill ( int color ) { int color2 = a [ 1 ][ 1 ] , d , newi , newj , i , j ; for ( i = 1 ; i <= 14 ; ++ i ) for ( j = 1 ; j <= 14 ; ++ j ) viz [ i ][ j ] = 0 ; a [ 1 ][ 1 ] = color ; queue < pair < int , int > > q ; q.push( mp ( 1 , 1 ) ) ; while ( !q.empty() ) { pair < int , int > nod = q.front() ; q.pop() ; for ( d = 0 ; d < 4 ; ++ d ) { newi = di [ d ] + nod.x ; newj = dj [ d ] + nod.y ; if ( !ok ( newi , newj ) ) continue ; if ( viz [ newi ][ newj ] ) continue ; if ( a [ newi ][ newj ] == color || a [ newi ][ newj ] == color2 ) { a [ newi ][ newj ] = color ; q.push( mp ( newi , newj ) ) ; viz [ newi ][ newj ] = 1 ; } } } } int check ( ) { int d , newi , newj , i , j , v [ 10 ] = {0} , best = 0 , ret ; for ( i = 1 ; i <= 14 ; ++ i ) for ( j = 1 ; j <= 14 ; ++ j ) viz2 [ i ][ j ] = 0 ; queue < pair < int , int > > q ; q.push( mp ( 1 , 1 ) ) ; while ( !q.empty() ) { pair < int , int > nod = q.front() ; q.pop() ; for ( d = 0 ; d < 4 ; ++ d ) { newi = di [ d ] + nod.x ; newj = dj [ d ] + nod.y ; if ( !ok ( newi , newj ) ) continue ; if ( viz [ newi ][ newj ] && viz2 [ newi ][ newj ] ) continue ; if ( viz [ newi ][ newj ] && !viz2 [ newi ][ newj ] ) { viz2 [ newi ][ newj ] = 1 ; q.push( mp ( newi , newj ) ) ; } if ( !viz [ newi ][ newj ] && !viz2 [ newi ][ newj ] ) { viz2 [ newi ][ newj ] = 1 ; v [ a [ newi ][ newj ] ] ++ ; } } } for ( i = 0 ; i <= 5 ; ++ i ) { if ( v [ i ] > best ) { best = v [ i ] ; ret = i ; } } if ( !best ) return -1 ; return ret ; } int main () { int i , j , col , d , x ; for ( i = 1 ; i <= 14 ; ++ i ) { cin >> s ; for ( j = 0 ; j < 14 ; ++ j ) a [ i ][ j + 1 ] = ( int )( s [ j ] - '0' ) ; } // for ( i = 1 ; i <= 14 ; ++ i , out << '\n' ) // for ( j = 1 ; j <= 14 ; ++ j ) out << a [ i ][ j ] << ' ' ; for ( d = 1 ; d <= 50 ; ++ d ) { x = check() ; if ( x == -1 ) return 0 ; // out << "\n\n\n" ; cout << x << ' ' ; Fill ( x ) ; // for ( i = 1 ; i <= 14 ; ++ i , out << '\n' ) // for ( j = 1 ; j <= 14 ; ++ j ) out << a [ i ][ j ] << ' ' ; } }