博客
关于我
B. Monopole Magnets(思维+连通块)
阅读量:264 次
发布时间:2019-03-01

本文共 2131 字,大约阅读时间需要 7 分钟。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout << #a << " = " << a << endl;using namespace std;const int maxn = 3000 + 100;typedef long long ll;inline ll read() { ll x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f;}char ma[maxn][maxn];ll fa[maxn*maxn];int dx[4] = {1, 0, -1, 0};int dy[4] = {0, 1, 0, -1};ll n, m;ll get(ll x, ll y) { return (x - 1) * m + y;}ll find(ll x) { if (fa[x] != x) { fa[x] = find(fa[x]); fa[x] = x; }}int main(void) { cin.tie(0); cout.tie(0); std::ios::sync_with_stdio(false); cin >> n >> m; for (ll i = 0; i < maxn*maxn; i++) fa[i] = i; for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= m; j++) { cin >> ma[i][j]; } } bool flag = 1; bool r1 = false; for (ll i = 1; i <= n; i++) { ll num = 0; for (ll j = 1; j <= m; j++) { if (num == 0 && ma[i][j] == '#') num = 1; else if (num == 1 && ma[i][j] == '.') num = 2; else if (num == 2 && ma[i][j] == '#') { flag = 0; break; } } if (num == 0) r1 = true; } for (ll j = 1; j <= m; j++) { ll num = 0; for (ll i = 1; i <= n; i++) { if (num == 0 && ma[i][j] == '#') num = 1; else if (num == 1 && ma[i][j] == '.') num = 2; else if (num == 2 && ma[i][j] == '#') { flag = 0; break; } } if (num == 0) l1 = true; } if (r1 != l1) flag = 0; if (!flag) { cout << "-1" << endl; } else { for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= m; j++) { if (ma[i][j] != '#') continue; for (ll k = 0; k < 4; k++) { ll nx = i + dx[k]; ll ny = j + dy[k]; if (nx < 1 || ny < 1 || nx > n || ny > m) continue; if (ma[nx][ny] == '#' && find(get(nx, ny)) != find(get(i, j))) { fa[find(get(nx, ny))] = find(get(i, j)); } } } } ll ans = 0; for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= m; j++) { if (ma[i][j] == '#' && fa[get(i, j)] == get(i, j)) { ans++; } } } cout << ans << endl; } return 0;}

这段代码实现了一个棋盘问题的解决方案。代码首先读取输入数据,初始化相关变量和数组,然后通过深度优先搜索(DFS)来标记各个连通区域。接着,代码检查棋盘是否存在不连续的黑色块,以及是否存在全空的行或列。最后,根据检查结果,计算棋盘中可以放置的极数。

转载地址:http://vact.baihongyu.com/

你可能感兴趣的文章
Prometheus实战教程:监控Nginx状态
查看>>
prometheus常用exporter下载地址大全
查看>>
Prometheus快速搭建与监控Linux系统实战
查看>>
prometheus报警与恢复告警的格式
查看>>
Pytorch中安装 torch_geometric 详细图文操作(全)
查看>>
prometheus监控docker容器实战
查看>>
Prometheus监控k8s集群使用邮箱和微信告警!
查看>>
Prometheus监控mysq数据库实战
查看>>
prometheus监控nginx实战
查看>>
Prometheus监控redis数据库实战
查看>>
Prometheus监控教程:使用Grafana展示主机基本信息
查看>>
pytorch中如何使用预训练词向量
查看>>
Prometheus监控教程:使用PromQL查询监控数据(上篇)
查看>>
Prometheus监控教程:使用PromQL查询监控数据(下篇)
查看>>
Pytorch中关于forward函数的理解与用法
查看>>
Prometheus监控教程:安装部署
查看>>
Prometheus监控教程:配置介绍
查看>>
Pytorch中tqdm进度条的使用
查看>>
Prometheus(2):SpringBoot 2.X集成Prometheus
查看>>
Promise 原理解析与实现(遵循Promise/A+规范)
查看>>