Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths(贪心)
题目链接: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(贪心)的更多相关文章
- Educational Codeforces Round 89 (Rated for Div. 2) C Palindromic Paths
题目链接:Palindromic Paths 题意: 给你一个n行m列的矩阵,这个矩阵被0或者1所填充,你需要从点(1,1)走到点(n,m).这个时候会有很多路径,每一条路径对应一个01串,你可以改变 ...
- 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, ...
- 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$,依次给出 ...
- Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords(贪心/数学)
题目链接:https://codeforces.com/contest/1366/problem/A 题意 有两个数 $a$ 和 $b$,每次可以选择从一个数中取 $2$,另一个数中取 $1$,问最多 ...
- Educational Codeforces Round 89 (Rated for Div. 2) A Shovels and Swords B、Shuffle
题目链接:A.Shovels and Swords 题意: 你需要一个木棍和两个钻石可以造出来一把剑 你需要两个木棍和一个钻石可以造出来一把铁锹 你现在有a个木棍,b个钻石,问你最多可以造出来几件东西 ...
- Educational Codeforces Round 89 (Rated for Div. 2)D. Two Divisors 线性筛质因子
题目链接:D:Two Divisors 题意: 给你n个数,对于每一个数vi,你需要找出来它的两个因子d1,d2.这两个因子要保证gcd(d1+d2,vi)==1.输出的时候输出两行,第一行输出每一个 ...
- Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords (贪心)
题意:你有\(a\)个树枝和\(b\)个钻石,\(2\)个树枝和\(1\)个钻石能造一个铁铲,\(1\)个树枝和\(2\)个钻石能造一把剑,问最多能造多少铲子和剑. 题解:如果\(a\le b\),若 ...
- 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\). 题解:对于 ...
- Educational Codeforces Round 89 (Rated for Div. 2) B. Shuffle (数学,区间)
题意:有长为\(n\)的排列,其中\(x\)位置上的数为\(1\),其余位置全为\(0\),询问\(m\)次,每次询问一个区间,在这个区间内可以交换任意两个位置上的数,问\(1\)最后出现在不同位置的 ...
随机推荐
- 利用DES,C#加密,Java解密代码
//C#加密 /// <summary> /// 进行DES加密. /// </summary> /// <param name="pToEncrypt&quo ...
- vue 深度作用选择器
使用 scoped 后,父组件的样式将不会渗透到子组件中 如果想在使用scoped,不污染全局的情况下,依然可以修改子组件样式,可以使用深度作用选择器 .tree{ width: 100%; floa ...
- MySQL多版本并发控制——MVCC机制分析
MVCC,即多版本并发控制(Multi-Version Concurrency Control)指的是,通过版本链维护一个数据的多个版本,使得读写操作没有冲突,可保证不同事务读写.写读操作并发执行,提 ...
- MongoDB Sharding(二) -- 搭建分片集群
在上一篇文章中,我们基本了解了分片的概念,本文将着手实践,进行分片集群的搭建 首先我们再来了解一下分片集群的架构,分片集群由三部分构成: mongos:查询路由,在客户端程序和分片之间提供接口.本次实 ...
- K8s遇到问题解决思路
问题排查一 从describe去查找相应的deploy/pod/rs/svc [root@k8s-master ~]# kubectl describe po/nginx-f95d765f9-8b6b ...
- AQS之ReentrantReadWriteLock写锁
用法 1.1 定义一个安全的list集合 public class LockDemo { ArrayList<Integer> arrayList = new ArrayList<& ...
- 入门OJ:Coin
题目描述 你有n个硬币,第i硬币面值为ai,现在总队长想知道如果丢掉了某个硬币,剩下的硬币能组成多少种价值?(0价值不算) 输入格式 第一行一个整数n 第二行n个整数.,a1,a2-an. 1< ...
- Linux的环境变量配置在/etc/profile或/etc/profile.d/*.sh文件中的区别是什么?
@ 目录 login shell non-login shell 它们的区别 Linux的环境变量可在多个文件中配置,如/etc/profile,/etc/profile.d/*.sh,~/.bash ...
- linux设备
设备初始化时同样要执行一个device_register函数,该函数传入一个struct device *类型的指针,因此要定义一个struct device类型的变量作为我们的设备. struct ...
- 使用XML作为配置表,WinForm程序读取配置表来动态显示控件
一.首先创建一个XML文件定义以下格式(uName:显示的中文字,uKey:代表控件的Name属性,ukeyValue:代表是否显示) 二.项目中定义一个通用类,来存放读取的值 这三个字段对应XML文 ...