QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#471087 | #6398. Puzzle: Tapa | UESTC_OldEastWest | WA | 1ms | 4072kb | C++17 | 5.8kb | 2024-07-10 17:53:09 | 2024-07-10 17:53:09 |
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: 100
Accepted
time: 1ms
memory: 4072kb
input:
3 3 2.4.3 ..... 5.8.5 ..... 3.5.3
output:
YES 2.4#3 ##### 5#8#5 ##### 3#5#3
result:
ok Correct.
Test #2:
score: 0
Accepted
time: 0ms
memory: 3880kb
input:
3 3 3.4.3 ..... 5.7.5 ..... 3.5.3
output:
NO
result:
ok Correct.
Test #3:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
2 2 2.2 ... 2.2
output:
YES 2#2 .#. 2#2
result:
ok Correct.
Test #4:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
2 50 2.4.4.4.4.5.5.5.5.5.5.5.5.4.5.5.4.4.5.5.5.5.4.5.5.5.5.5.4.4.5.4.5.5.5.5.5.5.5.5.5.5.5.4.4.5.5.4.5.3 ................................................................................................... 2.5.5.4.4.5.5.5.4.4.5.5.5.4.5.5.5.5.5.5.5.5.4.4.4.5.5.5.5.5.5.4.4.4.5.5.5.5.5.5.5.4.4.5.5.5.5.4...
output:
NO
result:
ok Correct.
Test #5:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
2 50 2.4.4.5.5.5.5.5.5.5.5.5.4.4.5.5.5.5.4.4.5.5.4.4.5.5.5.4.5.4.4.4.5.4.4.5.4.4.5.5.5.5.4.4.5.5.5.5.5.2 ................................................................................................... 3.5.4.5.5.5.5.5.5.5.5.5.5.5.4.5.5.5.5.4.5.5.5.5.4.4.5.4.5.4.5.5.5.5.5.4.4.5.5.5.4.4.5.5.5.5.5.4...
output:
NO
result:
ok Correct.
Test #6:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
50 2 3.2 ... 5.4 ... 5.5 ... 4.4 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ... 5.4 ... 5.4 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ... 5.4 ... 5.4 ... 5.4 ... 5.4 ... 4.4 ... 5.5 ... 5.5 ... 4.4 ... 5.4 ... 5.4 ... 5.5 ... 4.5 ... 4.5 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ......
output:
NO
result:
ok Correct.
Test #7:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
50 2 3.3 ... 5.4 ... 5.4 ... 5.4 ... 5.4 ... 5.5 ... 4.4 ... 4.4 ... 5.5 ... 4.4 ... 5.5 ... 5.5 ... 5.5 ... 5.5 ... 4.5 ... 5.5 ... 5.5 ... 5.4 ... 5.4 ... 5.5 ... 5.4 ... 5.5 ... 5.4 ... 5.4 ... 5.5 ... 5.5 ... 4.5 ... 4.5 ... 4.5 ... 4.5 ... 5.5 ... 5.4 ... 5.4 ... 5.5 ... 5.5 ... 4.4 ... 4.4 ......
output:
NO
result:
ok Correct.
Test #8:
score: -100
Wrong Answer
time: 1ms
memory: 3656kb
input:
3 50 3.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.5.4.4.5.5.5.5.4.4.5.5.5.5.5.5.5.5.4.4.5.5.4.4.5.4.4.5.3 ................................................................................................... 4.8.8.8.8.8.8.8.8.8.8.8.8.8.8.7.7.7.7.7.7.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.8.7.7.8...
output:
14 114 1 16 114 1 16 116 1 18 116 1 18 118 1 20 118 1 44 144 1 46 144 1 114 212 1 114 214 1 116 214 1 116 216 1 118 216 1 118 218 1 144 242 1 144 244 1 495 114 1 495 116 1 495 118 1 495 144 1 10020 10120 1 10022 10120 1 10022 10122 1 10024 10122 1 10024 10124 1 10026 10124 1 10050 10150 1 10052 1015...
result:
wrong answer YES or NO expected in answer, but 14 found.