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\)最后出现在不同位置的 ...
随机推荐
- 【Java基础】面向对象中
面向对象中 这一章主要涉及面向对象的三大特征,包括封装.继承.多态.(抽象). 封装 程序设计追求"高内聚,低耦合": 高内聚 :类的内部数据操作细节自己完成,不允许外部干涉: 低 ...
- 【ORACLE错误】SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled
执行set autotrace traceonly的时候,报错 SQL> set autotrace traceonly SP2-0618: Cannot find the Session Id ...
- [GKCTF2020]老八小超市儿
题目来自buu 一.题目初探 首先是一个shopxo搭建的演示站,通过扫描后台得到如下的网页 二.题目解答 首先是找到后台登陆的admin.php,然后通过百度找到shopxo的默认管理员登陆账号和密 ...
- ftp设置二进制上传
一个不重要的数据库,备份是用expdp导出,然后上传到ftp服务器上面.上周这个主机宕机了,要在别的数据库恢复,发现报如下错误: ORA-39001: invalid argument value O ...
- [Cerc2005]Knights of the Round Table
题目描述 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少有3个骑士参加,且相互憎恨的骑士不能坐在圆桌的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是大于1的奇数,以防 ...
- Wi-Fi IoT套件连PCF8563实现电子钟功能
首先跟同样新入手单片机开发的小伙伴分享一点I2C通信的知识.我估计大部分入手开发板的小伙伴都有一定程序开发的能力,但是底层开发可能是新接触,我看有的小伙伴配置开发环境都有障碍,其实并不是多复杂,只是首 ...
- linux设备
设备初始化时同样要执行一个device_register函数,该函数传入一个struct device *类型的指针,因此要定义一个struct device类型的变量作为我们的设备. struct ...
- ctfshow_djb杯
桐桑又开始摸鱼了 ctfshow的比赛整的一手好活.djb杯. web1_veryphp 打开就是源码: 1 <?php 2 error_reporting(0); 3 highlight_fi ...
- JavaScript中函数的调用!
JavaScript中函数的调用! 1 普通函数 // 1 普通函数 function fn() { console.log(123); } // 函数名 + 一个小括号! 或者 函数名.call() ...
- 415 Unsupported Media Type
415 Unsupported Media Type - HTTP | MDN https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415