#include #include #include #include #include #include #include #include using namespace std; using ll = long long; #define CIN( LL , A ) LL A; cin >> A #define GETLINE( A ) string A; getline( cin , A ) #define GETLINE_SEPARATE( A , SEPARATOR ) string A; getline( cin , A , SEPARATOR ) #define FOR_LL( VAR , INITIAL , FINAL_PLUS_ONE ) for( ll VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define FOR_ITR( ARRAY , ITR , END ) for( auto ITR = ARRAY .begin() , END = ARRAY .end() ; ITR != END ; ITR ++ ) #define REPEAT( HOW_MANY_TIMES ) FOR_LL( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES ) #define RETURN( ANSWER ) cout << ( ANSWER ) << endl; return 0 #define DOUBLE( PRECISION , ANSWER ) cout << fixed << setprecision( PRECISION ) << ( ANSWER ) << endl; return 0 #define MIN( A , B ) A < B ? A : B; #define MAX( A , B ) A < B ? B : A; template inline T Distance( const T& a , const T& b ){ return a < b ? b - a : a - b; } class data1 { public: ll m_h; ll m_i; inline data1( const ll& h = 0 , const ll& i = 0 ) : m_h( h ) , m_i( i ) {} }; inline bool operator<( const data1& d0 , const data1& d1 ) { return d0.m_h > d1.m_h; } class data2 { public: ll m_down; ll m_j; ll m_k; ll m_i; inline data2( const ll& down = 0 , const ll& j = 0 , const ll& k = 0 , const ll& i = 0 ) : m_down( down ) , m_j( j ) , m_k( k ) , m_i( i ) {} }; inline bool operator<( const data2& d0 , const data2& d1 ) { return d0.m_i < d1.m_i; } int main() { GETLINE( dummy ); // constexpr const ll N = 3; // constexpr const ll K = 3; constexpr const ll N = 60; constexpr const ll K = 500; constexpr const ll N2 = N * N; vector L{}; vector D{}; ll L_copy[ K ] , Li , A[ N2 ], i , j , k , h , N_minus , black_current , black_min , down , j_min , k_min; string a; i = 0; while( i < K ){ cin >> h; L.push_back( data1( h , i ) ); L_copy[i] = h; ++i; } sort( L.begin() , L.end() ); k = 0; while( k < N ){ cin >> a; j = 0; h = k * N; while( j < N ){ A[ h + j ] = stoi( a.substr( j , 1 ) ); ++j; } ++k; } // 1回ずつの最大化よりもラスト数回分纏めた最大化の方が // sort後のL[K-1].m_hなどが大きい時はスコアが上がるが、 // 面倒なので1回ずつの最大化で諦める i = 0; while( i < K ){ Li = L[i].m_h; N_minus = N - Li; black_min = N2 + 1; j = 0; // down while( j < N ){ k = 0; while( k < N_minus ){ black_current = 0; h = 0; while( h < Li ){ black_current -= A[ ( k + h ) * N + j ]; ++h; } if( black_current < black_min ){ black_min = black_current; down = 1; j_min = j; k_min = k; } ++k; } ++j; } k = 0; // right while( k < N ){ j = 0; while( j < N_minus ){ black_current = 0; h = 0; while( h < Li ){ black_current -= A[ k * N + ( j + h ) ]; ++h; } if( black_current < black_min ){ black_min = black_current; down = 0; j_min = j; k_min = k; } ++j; } ++k; } D.push_back( data2( down , j_min , k_min , L[i].m_i ) ); h = 0; if( down == 1 ){ while( h < Li ){ A[ ( k_min + h ) * N + j_min ] = 1 - A[ ( k_min + h ) * N + j_min ]; ++h; } } else { while( h < Li ){ A[ k_min * N + ( j_min + h ) ] = 1 - A[ k_min * N + ( j_min + h ) ]; ++h; } } ++i; } sort( D.begin() , D.end() ); i = 0; while( i < K ){ const data2& d = D[i]; if( d.m_down == 1 ){ cout << d.m_k << " " << d.m_j << " " << d.m_k + L_copy[i] - 1 << " " << d.m_j << endl; } else { cout << d.m_k << " " << d.m_j << " " << d.m_k << " " << d.m_j + L_copy[i] - 1 << endl; } ++i; } return 0; }