题目链接:https://codeforces.com/contest/1366/problem/C

题意

有一个 $n \times m$ 的 $01$迷宫,要使从 $(1,1)$ 到 $(n,m)$ 的所有路径均为回文串,至少要变换多少字符。

题解一

用 $bfs$ 得到回文串每个位置可能的 $01$ 个数,对称位置变换 $01$ 总个数中的较少值即可。

代码

#include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const int N = 100; int n, m;
int MP[N][N];
bool vis[N][N];
int cnt[N][2]; bool inside(int x, int y) {
return 1 <= x and x <= n and 1 <= y and y <= m;
} struct P{
int x, y, dep;
}; void bfs() {
queue<P> que;
que.push({1, 1, 1});
vis[1][1] = true;
while (!que.empty()) {
int x = que.front().x;
int y = que.front().y;
int dep = que.front().dep;
que.pop();
++cnt[dep][MP[x][y]];
for (int i = 0; i < 4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (inside(nx, ny) and !vis[nx][ny]) {
que.push({nx, ny, dep + 1});
vis[nx][ny] = true;
}
}
}
} void solve() {
memset(vis, 0, sizeof vis);
memset(cnt, 0, sizeof cnt);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> MP[i][j];
bfs();
int tot = n + m - 1;
int ans = 0;
for (int i = 1; i <= tot / 2; i++)
ans += min(cnt[i][0] + cnt[tot - i + 1][0], cnt[i][1] + cnt[tot - i + 1][1]);
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

题解二

可以用下标计算出当前字符在 $bfs$ 中的层数,即回文串中的位置。

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 100; int n, m;
int cnt[N][2]; void solve() {
memset(cnt, 0, sizeof cnt);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int x; cin >> x;
++cnt[i + j - 1][x];
}
int tot = n + m - 1;
int ans = 0;
for (int i = 1; i <= tot / 2; i++)
ans += min(cnt[i][0] + cnt[tot - i + 1][0], cnt[i][1] + cnt[tot - i + 1][1]);
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths(贪心)的更多相关文章

  1. Educational Codeforces Round 89 (Rated for Div. 2) C Palindromic Paths

    题目链接:Palindromic Paths 题意: 给你一个n行m列的矩阵,这个矩阵被0或者1所填充,你需要从点(1,1)走到点(n,m).这个时候会有很多路径,每一条路径对应一个01串,你可以改变 ...

  2. Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths (思维)

    题意:有一个\(n\)x\(m\)的矩阵,从\((1,1)\)出发走到\((n,m)\),问最少修改多少个数,使得所有路径上的数对应相等(e.g:\((1,2)\)和\((n-1,m)\)或\((2, ...

  3. Educational Codeforces Round 89 (Rated for Div. 2) B. Shuffle(数学/双指针)

    题目链接:https://codeforces.com/contest/1366/problem/B 题意 大小为 $n$ 的数组 $a$,除了 $a_x = 1$,其余 $a_i = 0$,依次给出 ...

  4. Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords(贪心/数学)

    题目链接:https://codeforces.com/contest/1366/problem/A 题意 有两个数 $a$ 和 $b$,每次可以选择从一个数中取 $2$,另一个数中取 $1$,问最多 ...

  5. Educational Codeforces Round 89 (Rated for Div. 2) A Shovels and Swords B、Shuffle

    题目链接:A.Shovels and Swords 题意: 你需要一个木棍和两个钻石可以造出来一把剑 你需要两个木棍和一个钻石可以造出来一把铁锹 你现在有a个木棍,b个钻石,问你最多可以造出来几件东西 ...

  6. Educational Codeforces Round 89 (Rated for Div. 2)D. Two Divisors 线性筛质因子

    题目链接:D:Two Divisors 题意: 给你n个数,对于每一个数vi,你需要找出来它的两个因子d1,d2.这两个因子要保证gcd(d1+d2,vi)==1.输出的时候输出两行,第一行输出每一个 ...

  7. Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords (贪心)

    题意:你有\(a\)个树枝和\(b\)个钻石,\(2\)个树枝和\(1\)个钻石能造一个铁铲,\(1\)个树枝和\(2\)个钻石能造一把剑,问最多能造多少铲子和剑. 题解:如果\(a\le b\),若 ...

  8. Educational Codeforces Round 89 (Rated for Div. 2) D. Two Divisors (数学)

    题意:有\(n\)组数,对于每组数,问是否能找到两个因子\(d_{1},d{2}\),使得\(gcd(d_{1}+d_{2},a_{i}=1)\),如果有,输出它们,否则输出\(-1\). 题解:对于 ...

  9. Educational Codeforces Round 89 (Rated for Div. 2) B. Shuffle (数学,区间)

    题意:有长为\(n\)的排列,其中\(x\)位置上的数为\(1\),其余位置全为\(0\),询问\(m\)次,每次询问一个区间,在这个区间内可以交换任意两个位置上的数,问\(1\)最后出现在不同位置的 ...

随机推荐

  1. 翻译 - ASP.NET Core 托管和部署 - 在 Linux 上使用 Nginx 托管 ASP.NET Core 网站

    翻译自 https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-5.0 本文介 ...

  2. 【ORA】ORA-39002,ORA-39070,ORA-29283, ORA-06512,ORA-29283解决办法

    今天使用IMPDP导入的时候报了一个错误 ORA-39002: invalid operation  ORA-39070: Unable to open the log file.  ORA-2928 ...

  3. Upload - Labs (上)

    Pass - 01: 1.尝试上传一个php文件:aaa.php,发现只允许上传某些图片类型,用bp抓包,发现http请求都没通过burp就弹出了不允许上传的提示框,这表明验证点在前端,而不在服务端 ...

  4. ctfhub技能树—RCE—综合过滤练习

    打开靶机 查看页面信息 查看源码可以发现这一次过滤了很多东西,查看当前目录信息 查询到%0a为换行符,可以利用这个url编码进行命令注入,开始尝试 http://challenge-2a4584dab ...

  5. Sentry(v20.12.1) K8S 云原生架构探索,1分钟上手 JavaScript 性能监控

    系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...

  6. jQuery 实现复制功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Maven 依赖机制

    概述 在 Maven 依赖机制的帮助下自动下载所有必需的依赖库,并保持版本升级.让我们看一个案例研究,以了解它是如何工作的.假设你想使用 Log4j 作为项目的日志.这里你要做什么? 传统方式 访问 ...

  8. 广告召回 Query-Ad Matching

    小结: 1.最为基础的召回链路就是要保证召回层的相关性,但是相关性高的广告并不一定具有很高的商业价值,所以开始尝试将一些商业化业务指标作为召回的依据 百度凤巢新一代广告召回系统--"莫比乌斯 ...

  9. How to kill go routine?

    How to kill go routine? https://stackoverflow.com/questions/37997608/kill-a-method-in-an-infinite-lo ...

  10. TCP半连接队列和全连接

    概述   如上图所示, 在TCP三次握手中,服务器维护一个半连接队列(sync queue) 和一个全连接队列(accept queue). 当服务端接收到客户端第一次SYN握手请求时,将创建的req ...