// C++11 #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define pv(val) cerr << #val << '=' << (val) << endl #define pvn(name, val) cerr << name << '=' << (val) << endl #define pl cerr << '@' << __LINE__ << endl static constexpr uint64_t u0 = 0ull; static constexpr uint64_t u1 = 1ull; static constexpr uint64_t uF = 0xFFFFFFFFFFFFFFFFull; #ifdef _MSC_VER //dummy int __builtin_popcountll(unsigned long long) { return 0; } int __builtin_clzll(unsigned long long) { return 0; } #endif template ostream& operator<<(ostream& os, vector const& vec) { if (vec.empty()) { os << "{}"; } else { os << '{'; for (size_t i = 0; i < vec.size() - 1; i++) os << vec[i] << ", "; os << vec.back() << '}'; } return os; } template ostream& operator<<(ostream& os, array const& arr) { if (arr.empty()) { os << "{}"; } else { os << '{'; for (size_t i = 0; i < arr.size() - 1; i++) os << arr[i] << ", "; os << arr.back() << '}'; } return os; } class Timer { using clock = chrono::high_resolution_clock; clock::time_point startTime; template auto elapsed() const { return chrono::duration_cast(clock::now() - startTime).count(); } public: Timer() { restart(); } void restart() { startTime = clock::now(); } auto milli() const { return elapsed(); } auto micro() const { return elapsed(); } }; Timer globalTimer; class Xorshift128 { uint32_t x, y, z, w, t; public: using result_type = uint32_t; Xorshift128(uint32_t seed = 12433) : x(123456789), y(362436069), z(521288629), w(seed) {} static constexpr result_type min() { return std::numeric_limits::min(); } static constexpr result_type max() { return std::numeric_limits::max(); } result_type operator()() { t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } }; template > class PriorityQueue { public: using container = vector; using iterator = typename container::iterator; using const_iterator = typename container::const_iterator; private: container queue_; size_t last_; public: PriorityQueue() { last_ = 0; } size_t size() const { return last_; } const_iterator begin() const { return queue_.begin(); } const_iterator end() const { return queue_.begin() + last_; } bool full() const { return size() == capacity(); } void setCapacity(size_t n) { queue_.resize(n); } size_t capacity() const { return queue_.size(); } void push(const T& val) { queue_[last_] = val; last_++; std::push_heap(queue_.begin(), queue_.begin() + last_, Comp()); } void push(T&& val) { queue_[last_] = std::move(val); last_++; std::push_heap(queue_.begin(), queue_.begin() + last_, Comp()); } void clear() { last_ = 0; } T const& top() const { return queue_[0]; } void pop() { std::pop_heap(queue_.begin(), queue_.begin() + last_, Comp()); last_--; } }; array rotate(array const& board) { array result; for (int y = 0; y < 60; y++) { result[y] = 0; for (int x = 0; x < 60; x++) { result[y] |= (board[x] & (u1 << y)) == 0 ? u0 : (u1 << x); } } return result; } struct Line { int dir; //0: yoko, 1: tate int a; int p; }; struct State { int turn = 0; int score; array board; array ans; }; struct Ope { State* parent; int score; Line line; }; bool operator<(State const& a, State const& b) { return a.turn < b.turn; } bool operator<(Ope const& a, Ope const& b) { return a.score > b.score; } static constexpr int N = 500; void solve(array L, array board) { auto const origBoard = board; State s0; s0.board = origBoard; int constexpr beamWidth = 25; PriorityQueue opes0, opes1; opes0.setCapacity(beamWidth); opes1.setCapacity(beamWidth); Ope dummy; dummy.score = -10000; vector states0, states1; states0.reserve(beamWidth); states1.reserve(beamWidth); states0.push_back(s0); for (int i = 0; i < 500; i++) { opes0.clear(); while (opes0.size() < opes0.capacity()) opes0.push(dummy); for (auto& s : states0) { for (int yt = 0; yt < 2; yt++) { for (int a = 0; a < 60; a++) { for (int p = 0; p < (61 - L[i]); p++) { auto const b = ((uF >> (64 - L[i])) << p); int diff = 1000 * (2 * __builtin_popcountll(s.board[a] & b) - L[i]); //diff += 100 * ((s.board[a] & ((b << 1) ^ b)) == 0 ? 1 : 0); //diff += 100 * ((s.board[a] & ((b >> 1) ^ b)) == 0 ? 1 : 0); //int const nWhiteSide = 100 * (s.board[a] & l if (1000 * opes0.top().score < 1000 * s.score + diff) { opes0.pop(); if (diff >= 0) opes0.push({ &s, s.score + diff / 1000, Line{yt, a, p} }); else opes0.push({ &s, s.score + ((diff - 200) / 1000), Line{yt, a, p} }); } } } s.board = rotate(s.board); } } states1.clear(); for (auto ope : opes0) { states1.push_back(*(ope.parent)); auto& ns = states1.back(); ns.ans[i] = ope.line; ns.score = ope.score; if (ope.line.dir == 0) { ns.board[ope.line.a] ^= ((uF >> (64 - L[i])) << ope.line.p); } else { ns.board = rotate(ns.board); ns.board[ope.line.a] ^= ((uF >> (64 - L[i])) << ope.line.p); ns.board = rotate(ns.board); } } swap(states0, states1); } pv(globalTimer.milli()); auto ans = max_element(states0.begin(), states0.end())->ans; auto score = max_element(states0.begin(), states0.end())->score; for (int i = 0; i < N; i++) { if (ans[i].dir == 0) { printf("%d %d %d %d\n", ans[i].a + 1, 61 - (ans[i].p + L[i]), ans[i].a + 1, 61 - (ans[i].p + 1)); } else { printf("%d %d %d %d\n", ans[i].p + 1, 61 - (ans[i].a + 1), ans[i].p + L[i], 61 - (ans[i].a + 1)); } } pvn("Score", score); pvn("Time", globalTimer.milli()); } int main(void) { cin.tie(0); ios::sync_with_stdio(false); int N, K; array L; array board; cin >> N >> K; for (int i = 0; i < 500; i++) { cin >> L[i]; } string line; for (int i = 0; i < 60; i++) { cin >> line; std::bitset<64> bs(line); board[i] = bs.to_ullong(); } solve(L, board); return 0; }