#include"bits/stdc++.h" using namespace std; typedef unsigned int uint; typedef long long int ll; typedef unsigned long long int ull; #define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout< ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<>= 1, k++)s = (s << 1) | (u & 1); for (; 0>= 1)cout << (s & 1); } } #define TIME chrono::system_clock::now() #define MILLISEC(t) (chrono::duration_cast(t).count()) namespace { std::chrono::system_clock::time_point t; void tic() { t = TIME; } void toc() { fprintf(stderr, "TIME : %lldms\n", MILLISEC(TIME - t)); } std::chrono::system_clock::time_point tle = TIME; void safe_tle(int msec) { return;assert(MILLISEC(TIME - tle) < msec); } } template ostream& operator <<(ostream &o, const pair p) { o << "(" << p.first << ":" << p.second << ")"; return o; } void safebreak() { static auto t = TIME; assert(MILLISEC(TIME - t) < 5000); } namespace std { template class hash> { public: size_t operator()(const pair& x) const { return hash()(x.first) ^ hash()(x.second); } }; } // todo : typedef int_c int // capacity // TODO:マーカーの実装? class Flow { public: size_t n; struct Arrow { int from, to; int dual; // 相手(逆方向)のArrowID int capleft; int cap; Arrow(int from = 0, int to = 0, int dual = 0, int left = 1, int cap = 1) :from(from), to(to), dual(dual), capleft(left), cap(cap) {} }; vector> vertex_to; vector> vertex_from; // TODO: いらない vector arrow; Flow(int n, int m = 5010) :n(n), vertex_to(n), vertex_from(n) { arrow.reserve(m); } void connect(int from, int to, int w_max) { int id = arrow.size(); vertex_to[from].push_back(id); vertex_from[to].push_back(id); arrow.emplace_back(from, to, id + 1, w_max, w_max); vertex_to[to].push_back(id + 1); // 逆方向のArrowも追加する vertex_from[from].push_back(id + 1); // この辺のidは必ず奇数 arrow.emplace_back(to, from, id, 0, w_max); } size_t degree(int v) { return vertex_to[v].size() + vertex_from[v].size(); } size_t degree_in(int v) { return vertex_from[v].size(); } size_t degree_out(int v) { return vertex_to[v].size(); } }; // DAG int _dinic_tree_dfs(Flow& flow, vector& flag, const vector& dist, int u, int i_sink, int mini) { safe_tle(50); if (i_sink == u) return mini; int sumw = 0; bool term = true; for (int e : flow.vertex_to[u]) { Flow::Arrow& a = flow.arrow[e]; if (a.capleft > 0 && dist[u]>dist[a.to]) { int w; if (mini < 0) w = a.capleft; else w = min(a.capleft, mini); w = _dinic_tree_dfs(flow, flag, dist, a.to, i_sink, w); if (w == -1) continue; a.capleft -= w; flow.arrow[a.dual].capleft += w; //printf("%d->%d (%d) : w=%d mini=%d \n",a.from,a.to,a.left+w,w,mini); sumw += w; mini -= w; term = false; } } return term ? -1 : sumw; } int _dinic_path_dfs(Flow& flow, vector& flag, const vector& dist, int u, int i_sink, int mini) { safe_tle(50); if (mini == 0) return 0; if (i_sink == u) return mini; for (int e : flow.vertex_to[u]) { Flow::Arrow& a = flow.arrow[e]; if (a.capleft > 0 && dist[u]>dist[a.to]) { int w; if (mini < 0) w = a.capleft; else w = min(a.capleft, mini); w = _dinic_path_dfs(flow, flag, dist, a.to, i_sink, w); if (w <= 0) continue; a.capleft -= w; flow.arrow[a.dual].capleft += w; return w; } } return 0; } // 各頂点に流れる流量の演算を消去 // flowは書き換えられる. void dinic(Flow &flow, int i_source, int i_sink) { assert(i_source != i_sink); int distbegin = 0; vector dist(flow.n); queue q; vector flag(flow.n); for (int distbegin = 0; ; distbegin += flow.n) { // for (int i = 0; i < flow.arrow.size(); ++i) { // if (i % 2 == 0) { // Flow::Arrow a = flow.arrow[i]; // printf("%d->%d : %d\n", a.from, a.to, a.capleft); // } // } // for (int i = 0; i < flow.arrow.size(); ++i) { // if (i % 2 == 1) { // Flow::Arrow a = flow.arrow[i]; // printf("%d->%d :R%d\n", a.from, a.to, a.capleft); // } // } q.emplace(i_sink); // bfsはsinkからsourceへの距離を計算. dist[i_sink] = distbegin + 1; while (!q.empty()) { int v = q.front(); q.pop(); for (int ie : flow.vertex_from[v]) { const Flow::Arrow& e = flow.arrow[ie]; if (0 0) ; // if (_dinic_tree_dfs(flow, flag, dist, i_source, i_sink, -1) <= 0) break; } } } // 最大最小流量制限付き class FlowMinMax { public: Flow flow; int ss; // vertex of new source FlowMinMax(int n, int m) :flow(n + 2, m), ss(n) {} FlowMinMax(int n) :flow(n + 2), ss(n) {} void connect(int from, int to, int w_min, int w_max) { // assert(w_min < w_max); /* flow.connect(from, ss+1, w_min); flow.connect(from, to , w_max-w_min); flow.connect(ss , to , w_min); return; */ if (w_max == w_min) { flow.connect(ss, to, w_min); flow.connect(from, ss + 1, w_min); } else if (w_min == 0) { flow.connect(from, to, w_max - w_min); } else { flow.connect(from, ss + 1, w_min); flow.connect(from, to, w_max - w_min); flow.connect(ss, to, w_min); } } private: template // map,int> or unordered_map bool _solve_dinic_edge(MAP_PI& result_edge, int i_source, int i_sink) { dinic(flow, ss, ss + 1); dinic(flow, ss, i_sink); dinic(flow, i_source, ss + 1); dinic(flow, i_source, i_sink); for (int e : flow.vertex_from[ss + 1]) { const Flow::Arrow& a = flow.arrow[e]; //printf("%d->%d (%d)\n",a.from,a.to,a.w_max);cout.flush(); if (0 < a.capleft) return false; } int floow; for (int u = 0; u v if (a.to >= flow.n - 2) { if (0 < a.capleft) return false; continue; } const Flow::Arrow& c = flow.arrow[ea + 2]; // S -> v if (a.to != c.to) { floow = a.cap - a.capleft; } else { if (0 < c.capleft) return false; floow = c.cap + a.cap - c.capleft - a.capleft; } if (0 < floow) result_edge[make_pair(u, a.to)] += floow; } } return true; } // connect操作を行うので,2回以上呼び出すのは禁止 // sumflow = sink,flowの流量が既知 template bool _solve_dinic_edge_known(MAP_PI& result_edge, int i_source, int i_sink, int sumflow) { flow.connect(ss, i_source, sumflow); flow.connect(i_sink, ss + 1, sumflow); dinic(flow, ss, ss + 1); for (int e : flow.vertex_from[ss + 1]) { if (e % 2 == 1) continue; // is dual edge? const Flow::Arrow& a = flow.arrow[e]; //printf("%d: %d->%d (%d)\n",e,a.from,a.to,a.capleft);cout.flush(); if (0 < a.capleft) return false; } int floow; for (int u = 0; u v if (a.to >= flow.n - 2) { if (0 < a.capleft) return false; continue; } const Flow::Arrow& c = flow.arrow[ea + 2]; // S -> v if (a.to != c.to) { floow = a.cap - a.capleft; } else { if (0 < c.capleft) return false; floow = c.cap + a.cap - c.capleft - a.capleft; } if (0 < floow) result_edge[make_pair(u, a.to)] += floow; } } return true; } public: bool solve_dinic_edge(map, int>& result_edge, int i_source, int i_sink, int sumflow = -1) { return sumflow<0 ? _solve_dinic_edge(result_edge, i_source, i_sink) : _solve_dinic_edge_known(result_edge, i_source, i_sink, sumflow); } bool solve_dinic_edge(unordered_map, int>& result_edge, int i_source, int i_sink, int sumflow = -1) { return sumflow<0 ? _solve_dinic_edge(result_edge, i_source, i_sink) : _solve_dinic_edge_known(result_edge, i_source, i_sink, sumflow); } }; /** // dinic sample int main(){ int i,j,k; int x,y,a,b; Flow graph(6); graph.connect(0,1,1); graph.connect(1,4,1); graph.connect(4,5,1); graph.connect(0,3,1); graph.connect(3,4,1); graph.connect(1,2,1); graph.connect(2,5,1); //vector result(6,0); dinic(graph,0,5); //debugv(result); for (int i = 0; i < graph.arrow.size(); ++i) { if (i % 2 == 0) { Flow::Arrow a = graph.arrow[i]; printf("%d->%d : %d\n", a.from, a.to, a.capleft); } } // FlowMinMax graph2(3); // // graph2.connect(0,1,1,2); // graph2.connect(1,2,3,4); // // vector result2(3,0); // cout << (graph2.solve_dinic(result2,0,2) ? "true" : "false") << endl; // // debugv(result2); return 0; } /**/ /**/ int width, height; int m, n; int field[10010]; int commands[30010]; int main() { int i, j, k; int x, y, a, b; tic(); cin >> height >> width >> n; cin.ignore(); int nblocks = 0; // X座標にブロックがいくつ積まれているか、を記録する。 // stringを保持する必要はない。 for (y = 0; y < height; y++) { string s; cin >> s; for (x = 0; x < width; x++) { field[x] += s[x] == '#'; } } for (x = 0; x < width; x++) { nblocks += field[x]; } for (i = 0; i < n; i++) { scanf("%d", commands + i); } // A _ B _ C // | | ----> | | ----> [sink] // [source] -> | | | | // | | | | // |_pack |_field // // A : [1,9] (packは[1,9]個のブロックを持つ) // B : [0,3] (packは3x3の容量を持つ) // C : [#,#] (x列には#個のブロックが積み上がっている) FlowMinMax flow(1 + n + width + 1); const int i_source = 0; const int i_sink = 1; for (i = 0; i < n; i++) { // A edge flow.connect(i_source, 2 + i, 1, 9); int left = commands[i]; for (j = 0; j < 3; j++) { // B edge flow.connect(2 + i, 2 + n + left + j, 0, 3); } } for (x = 0; x < width; x++) { // C edge flow.connect(2 + n + x, i_sink, field[x], field[x]); } //for (Flow::Arrow& ar : flow.flow.arrow){ // if (ar.w_max == 0) continue; // printf("%d -> %d\n",ar.from,ar.to); //} unordered_map, int> nagare; if (!flow.solve_dinic_edge(nagare, i_source, i_sink, nblocks)) { cout << "hage" << endl;exit(0); cerr << "ABORT" << endl; abort(); cout << "warn" << endl; } //debugv(nagare); int hako[3]; for (i = 0; i < n; i++) { for (j = 0; j < 3; j++) { hako[j] = nagare[make_pair(2 + i, 2 + n + commands[i] + j)]; } for (y = 3; 0 < y; y--) { for (x = 0; x < 3; x++) { if (y <= hako[x]) { putchar('#'); } else { putchar('.'); } } putchar('\n'); } } toc(); return 0; } /* 2 4 3 ..#. ..## 0 1 0 */