#pragma GCC optimize ("O3") #pragma GCC target ("avx") #include "bits/stdc++.h" // define macro "/D__MAI" using namespace std; typedef long long int ll; #define debug(v) {printf("L%d %s > ",__LINE__,#v);cout<<(v)< ",__LINE__,#v);for(auto e:(v)){cout< ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[y][x]<<" ";}cout< ostream& operator <<(ostream &o, const pair p) { o << "(" << p.first << ":" << p.second << ")"; return o; } template T& maxset(T& to, const T& val) { return to = max(to, val); } template T& minset(T& to, const T& val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); inline ll rand_range(ll l, ll h) { return uniform_int_distribution(l, h)(randdev); } #if defined(_WIN32) || defined(_WIN64) #define getchar_unlocked _getchar_nolock #define putchar_unlocked _putchar_nolock #elif defined(__GNUC__) #else #define getchar_unlocked getchar #define putchar_unlocked putchar #endif namespace { #define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E) class MaiScanner { public: template void input_integer(T& var) { var = 0; T sign = 1; int cc = getchar_unlocked(); for (; cc<'0' || '9'>(int& var) { input_integer(var); return *this; } inline MaiScanner& operator>>(long long& var) { input_integer(var); return *this; } inline MaiScanner& operator>>(string& var) { int cc = getchar_unlocked(); for (; !isvisiblechar(cc); cc = getchar_unlocked()); for (; isvisiblechar(cc); cc = getchar_unlocked()) var.push_back(cc); return *this; } template void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { public: template void output_integer(T var) { if (var == 0) { putchar_unlocked('0'); return; } if (var < 0) putchar_unlocked('-'), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putchar_unlocked(stack[--stack_p]); } inline MaiPrinter& operator<<(char c) { putchar_unlocked(c); return *this; } inline MaiPrinter& operator<<(int var) { output_integer(var); return *this; } inline MaiPrinter& operator<<(long long var) { output_integer(var); return *this; } inline MaiPrinter& operator<<(char* str_p) { while (*str_p) putchar_unlocked(*(str_p++)); return *this; } inline MaiPrinter& operator<<(const string& str) { const char* p = str.c_str(); const char* l = p + str.size(); while (p < l) putchar_unlocked(*p++); return *this; } template void join(IT begin, IT end, char sep = '\n') { for (auto it = begin; it != end; ++it) *this << *it << sep; } }; } MaiScanner scanner; MaiPrinter printer; // ソートしてi番目の値は 元々idx[i]番目の値 // 元々i番目の値は ソートするとidxr[i]番目の値 template void sort_index(const ITER begin, const ITER end, vector &idx, vector &idxr) { size_t n = end - begin; idx.resize(n); idxr.resize(n); for (int i = 0; i < n; ++i) idx[i] = i; sort(idx.begin(), idx.end(), [&begin](int l, int r) {return begin[l] < begin[r]; }); for (int i = 0; i < n; ++i) idxr[idx[i]] = i; } struct Stick { int len; // 実効長さ int idx1, idx2; // 元の棒のインデックス int len1, len2; // 元の棒の長さ Stick(int l1, int i1) :len(l1), idx1(i1), len1(l1) { } Stick(int l, int i1, int i2, int l1, int l2) :len(l), idx1(i1), idx2(i2), len1(l1), len2(l2) { if (len1 < len2) swap(len1, len2), swap(idx1, idx2); } inline bool single() const { return len == len1; } }; int n, kei; vector len[55]; int aa[55][55]; vector sticks; int answers[2020][4]; int colsum[55][55], rowsum[55][55]; int main() { scanner >> n >> kei; repeat(i, kei) { int l; scanner >> l; len[l].push_back(i); } repeat(y, n) { string str; scanner >> str; repeat(x, n) { aa[y][x] = str[x] == '1'; } } // step 1 for (int l = 50; 4 <= l; --l) { while (!len[l].empty()) { int i = len[l].back(); // 累積和 repeat(y, n) { repeat(x, n) { rowsum[y][x + 1] = rowsum[y][x] + aa[y][x]; } } repeat(x, n) { repeat(y, n) { colsum[x][y + 1] = colsum[x][y] + aa[y][x]; } } // 貪欲 array best_r = { 0,0,0,0 }; repeat(y, n) { repeat(x, n) { if (x + l <= n) { maxset(best_r, array{rowsum[y][x + l] - rowsum[y][x], y, x, 0}); } if (y + l <= n) { maxset(best_r, array{colsum[x][y + l] - colsum[x][y], y, x, 1}); } } } if (best_r[0]*100/l < 75) { break; } if (best_r[3] == 0) { answers[i][0] = best_r[1] + 1; answers[i][1] = best_r[2] + 1; answers[i][2] = best_r[1] + 1; answers[i][3] = best_r[2] + l; repeat(t, l) aa[best_r[1]][best_r[2] + t] ^= 1; } else if (best_r[3] == 1) { answers[i][0] = best_r[1] + 1; answers[i][1] = best_r[2] + 1; answers[i][2] = best_r[1] + l; answers[i][3] = best_r[2] + 1; repeat(t, l) aa[best_r[1] + t][best_r[2]] ^= 1; } len[l].pop_back(); } } // step2 sticks.reserve(kei); for (int l = 50; 9 <= l; --l) { while (!len[l].empty()) { int t = l - 1; while (len[t].empty()) --t; sticks.emplace_back(l - t, len[l].back(), len[t].back(), l, t); len[l].pop_back(); len[t].pop_back(); } } for (int l = 1; l < 50; ++l) { while (!len[l].empty()) { sticks.emplace_back(l, len[l].back()); len[l].pop_back(); } } sort(ALL(sticks), [](const Stick& l, const Stick& r) {return l.len > r.len; }); // step3 for (const Stick& stick : sticks) { // 累積和 repeat(y, n) { repeat(x, n) { rowsum[y][x + 1] = rowsum[y][x] + aa[y][x]; } } repeat(x, n) { repeat(y, n) { colsum[x][y + 1] = colsum[x][y] + aa[y][x]; } } // 貪欲 int l = stick.len; array best_r = { 0,0,0,0 }; repeat(y, n) { repeat(x, n) { if (x + l <= n) { maxset(best_r, array{rowsum[y][x + l] - rowsum[y][x], y, x, 0}); } if (y + l <= n) { maxset(best_r, array{colsum[x][y + l] - colsum[x][y], y, x, 1}); } assert(best_r[0] < 1e3); } } if (best_r[3] == 0) { if (stick.single()) { int i = stick.idx1; answers[i][0] = best_r[1] + 1; answers[i][1] = best_r[2] + 1; answers[i][2] = best_r[1] + 1; answers[i][3] = best_r[2] + l; } else { if (best_r[2] - stick.len2 < 0) { answers[stick.idx1][0] = best_r[1] + 1; answers[stick.idx1][1] = best_r[2] + 1; answers[stick.idx1][2] = best_r[1] + 1; answers[stick.idx1][3] = best_r[2] + stick.len1; answers[stick.idx2][0] = best_r[1] + 1; answers[stick.idx2][1] = best_r[2] + l + 1; answers[stick.idx2][2] = best_r[1] + 1; answers[stick.idx2][3] = best_r[2] + stick.len1; } else { answers[stick.idx1][0] = best_r[1] + 1; answers[stick.idx1][1] = best_r[2] - stick.len2 + 1; answers[stick.idx1][2] = best_r[1] + 1; answers[stick.idx1][3] = best_r[2] + l; answers[stick.idx2][0] = best_r[1] + 1; answers[stick.idx2][1] = best_r[2] - stick.len2 + 1; answers[stick.idx2][2] = best_r[1] + 1; answers[stick.idx2][3] = best_r[2]; } } repeat(t, l) aa[best_r[1]][best_r[2] + t] ^= 1; } else if (best_r[3] == 1) { if (stick.single()) { int i = stick.idx1; answers[i][0] = best_r[1] + 1; answers[i][1] = best_r[2] + 1; answers[i][2] = best_r[1] + l; answers[i][3] = best_r[2] + 1; } else { if (best_r[1] - stick.len2 < 0) { answers[stick.idx1][0] = best_r[1] + 1; answers[stick.idx1][1] = best_r[2] + 1; answers[stick.idx1][2] = best_r[1] + stick.len1; answers[stick.idx1][3] = best_r[2] + 1; answers[stick.idx2][0] = best_r[1] + l + 1; answers[stick.idx2][1] = best_r[2] + 1; answers[stick.idx2][2] = best_r[1] + stick.len1; answers[stick.idx2][3] = best_r[2] + 1; } else { answers[stick.idx1][0] = best_r[1] - stick.len2 + 1; answers[stick.idx1][1] = best_r[2] + 1; answers[stick.idx1][2] = best_r[1] + l; answers[stick.idx1][3] = best_r[2] + 1; answers[stick.idx2][0] = best_r[1] - stick.len2 + 1; answers[stick.idx2][1] = best_r[2] + 1; answers[stick.idx2][2] = best_r[1]; answers[stick.idx2][3] = best_r[2] + 1; } } repeat(t, l) aa[best_r[1] + t][best_r[2]] ^= 1; } } repeat(i, kei) { printer << answers[i][0] << ' ' << answers[i][1] << ' ' << answers[i][2] << ' ' << answers[i][3] << '\n'; } // debugaa(aa, n, n); return 0; }