QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#471086 | #6396. Puzzle: Kusabi | UESTC_OldEastWest | WA | 0ms | 3572kb | C++17 | 5.8kb | 2024-07-10 17:52:35 | 2024-07-10 17:52:36 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
const int N = 1e4 + 5;
const ll INF = 1e15;
struct Dinic {
int n, m, s, t, tot;
std::vector <int> cur, dep, from, to, nxt, head, cap;
void AddEdge (int u, int v, int c) {
from[tot] = u, to[tot] = v, nxt[tot] = head[u], head[u] = tot;
cap[tot] = c, ++tot;
}
Dinic (int n, int m, int s, int t, std::vector <std::array <int, 3> > &edge) {
this -> n = n, this -> m = m, this -> s = s, this -> t = t, tot = 0;
from = to = nxt = cap = std::vector <int> (m << 1);
cur = dep = head = std::vector <int> (n + 5, -1);
for (auto [u, v, w] : edge) AddEdge (u, v, w), AddEdge (v, u, 0);
}
bool BFS () {
fill (dep.begin (), dep.end (), -1);
std::queue <int> q;
q.push (s), dep[s] = 1;
while (!q.empty ()) {
int u = q.front ();
q.pop ();
if (u == t) continue;
else {
for (int i = head[u], v, c; ~i; i = nxt[i]) {
v = to[i], c = cap[i];
if (dep[v] != -1 || !c) continue;
dep[v] = dep[u] + 1, q.push (v);
}
}
}
return dep[t] > -1;
}
int DFS (int u, int last) {
if (u == t || !last) return last;
int now = last;
for (int i = cur[u], v, c; ~i; i = nxt[i]) {
cur[u] = nxt[i];
v = to[i], c = cap[i];
if (dep[v] == dep[u] + 1 && c) {
int delta = DFS (v, std::min (now, c));
now -= delta, cap[i] -= delta, cap[i ^ 1] += delta;
if (!now) break;
}
}
return last - now;
}
int MaxFlow () {
int ans = 0;
while (BFS ()) {
for (int i = 0; i <= n + 1; ++i) cur[i] = head[i];
ans += DFS (s, INT_MAX);
}
return ans;
}
};
void charming() {
int n, m; std::cin >> n >> m;
n = n * 2 - 1, m = m * 2 - 1;
std::vector<std::string> mp(n);
for (int i = 0; i < n; ++i) std::cin >> mp[i];
std::vector<std::vector<int> > black(n, std::vector<int> (m));
std::vector<std::array<int, 3> > edges;
int s = n * m, t = n * m + 1, blank_cnt = 0;
auto isValid = [&](int r, int c) -> bool {
if (r >= 0 && r < n && c >= 0 && c < m) return true;
else return false;
};
auto transform = [&](int r, int c) -> int {
return ((r * m + c) >> 1) + ((r * m + c) >> 1 & 1) * N;
};
auto rev_transform = [&](int x) -> std::pair<int, int> {
if (x > N) x -= N;
x <<= 1;
return std::make_pair(x / m, x % m);
};
bool ok = true;
std::vector<std::pair<int, int> > edge;
for (int i = 0; i < m; ++i) {
if (mp[0][i] != '.') edge.emplace_back(std::make_pair(0, i));
}
for (int i = 1; i < n; ++i) {
if (mp[i][m - 1] != '.') edge.emplace_back(std::make_pair(i, m - 1));
}
for (int i = m - 2; i >= 0; --i) {
if (mp[n - 1][i] != '.') edge.emplace_back(std::make_pair(n - 1, i));
}
for (int i = n - 2; i > 0; --i) {
if (mp[i][0] != '.') edge.emplace_back(std::make_pair(i, 0));
}
int edge_cnt = edge.size(), now = 0;
while (now > -edge_cnt) {
int r = edge[(now + edge_cnt) % edge_cnt].first;
int c = edge[(now + edge_cnt) % edge_cnt].second;
if (mp[r][c] == '2' || mp[r][c] == '4') --now;
else break;
}
++now;
for (int i = now; i < now + edge_cnt; ++i) {
int r = edge[(i % edge_cnt + edge_cnt) % edge_cnt].first;
int c = edge[(i % edge_cnt + edge_cnt) % edge_cnt].second;
if (mp[r][c] == '2' || mp[r][c] == '4') {
if (i == now + edge_cnt - 1) {ok = false; break;}
else {
int nr = edge[((i + 1) % edge_cnt + edge_cnt) % edge_cnt].first;
int nc = edge[((i + 1) % edge_cnt + edge_cnt) % edge_cnt].second;
if (mp[nr][nc] == '2' || mp[nr][nc] == '4') {
int mid_r = r + nr >> 1, mid_c = c + nc >> 1;
mp[mid_r][mid_c] = '#';
++i;
}
else {ok = false; break;}
}
}
}
if (!ok) {
std::cout << "NO\n";
return;
}
for (int r = 1; r < n - 1; ++r) {
for (int c = 1; c < m - 1; ++c) {
if (mp[r][c] != '.') {
if (mp[r][c] == '7') {
++blank_cnt;
if (transform(r, c) < N) edges.emplace_back((std::array<int, 3>) {s, transform(r, c), 1});
else edges.emplace_back((std::array<int, 3>) {transform(r, c), t, 1});
for (int dr : {-2, 2}) {
for (int dc : {-2, 2}) {
if (isValid(r + dr, c + dc) && mp[r + dr][c + dc] != '.') {
edges.emplace_back((std::array<int, 3>) {transform(r, c), transform(r + dr, c + dc), 1});
}
}
}
}
}
}
}
for (auto &[x, y, z] : edges) {
if (x == s || y == t) continue;
else if (x > y) std::swap (x, y);
}
sort(edges.begin(), edges.end(), [](std::array<int, 3>&x,
std::array<int, 3> &y) {
if (x[0] != y[0]) return x[0] < y[0];
else return x[1] < y[1];
});
edges.resize(unique(edges.begin(), edges.end()) - edges.begin());
for (auto [u, v, c] : edges) {
std::cout << u << " " << v << " " << c << std::endl;
}
Dinic dinic = Dinic(N << 1, (int)edges.size() + 5, s, t, edges);
if (dinic.MaxFlow() != blank_cnt) std::cout << "NO\n";
else {
std::cout << "YES\n";
for (int i = 0, cnt = edges.size(); i < cnt; ++i) {
int u = edges[i][0], v = edges[i][1];
if (u != s && v != t) {
std::pair<int, int> pos1 = rev_transform(u);
std::pair<int, int> pos2 = rev_transform(v);
int mid_r = pos1.first + pos2.first >> 1;
int mid_c = pos1.second + pos2.second >> 1;
mp[mid_r][mid_c] = '#';
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (mp[i][j] == '.') std::cout << '#';
else if (mp[i][j] == '#') std::cout << '.';
else std::cout << mp[i][j];
}
std::cout << '\n';
}
}
}
signed main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
charming();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3572kb
input:
8 2 1 - 3 1 - 4 2 Tong 5 2 Tong 6 3 Duan 7 3 - 8 7 Chang
output:
NO
result:
wrong answer Jury has answer but participant has not.