网站首页 > 技术文章 正文
1
单项选择题
2
判断题
3
编程实战
荒地开垦
题目分析
- 统计原本可以开垦的荒地数量
- 枚举清除每一块#后可以增加的荒地最大数量
- 结果为上述两部分之和。
参考程序
#include<bits/stdc++.h>
usingnamespacestd;
constint N = 1010;
int n, m;
char g[N][N];
// 上右左下
int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};
boolcheck(int x, int y, int d){
for(int i = 0; i < 4; i++){
if(i == 3 - d) continue;
int nx = x + dx[i], ny = y + dy[i];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && g[nx][ny] == '#')
returnfalse;
}
returntrue;
}
intmain(){
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> g[i][j];
// 统计原本可以开垦的荒地数量
int cnt = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(g[i][j] == '#') continue; // 剔除自己是杂物情况
bool is_ok = true;
for(int k = 0; k < 4; k++){
int nx = i + dx[k], ny = j + dy[k];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && g[nx][ny] == '#'){
is_ok = false;
break;
}
}
cnt += is_ok;
}
}
// cout << cnt << endl;
// cout << check(1, 2, 0);
// 枚举清除每一块#后可以增加的荒地最大数量
int maxv = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(g[i][j] == '#'){
int t = 0;
bool is_ok = true; // 中间点是否可以统计进去
for(int k = 0; k < 4; k++){
int nx = i + dx[k], ny = j + dy[k];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && g[nx][ny] == '#'){
is_ok = false;
break;
}
}
t += is_ok;
// cout << "1:" << t << endl;
// 上
if(i - 1 >= 1 && g[i - 1][j] == '.' && check(i - 1, j, 0))
t++;
// cout << "2:" << t << endl;
// 下
if(i + 1 <= n && g[i + 1][j] == '.' && check(i + 1, j, 3))
t++;
// cout << "3:" << t << endl;
// 左
if(j - 1 >= 1 && g[i][j - 1] == '.' && check(i, j - 1, 2))
t++;
// cout << "4:" << t << endl;
// 右
if(j + 1 <= m && g[i][j + 1] == '.' && check(i, j + 1, 1))
t++;
// cout << "5:" << t << endl;
// printf("(%d,%d) %d\n", i, j, t);
maxv = max(maxv, t);
}
}
}
// cout << cnt << maxv << endl;
cout << cnt + maxv;
return0;
}
二阶矩阵
题目分析
简单题,枚举法。
参考程序
#include<bits/stdc++.h>
usingnamespacestd;
constint N = 510;
int n, m;
int a[N][N];
intmain(){
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> a[i][j];
int cnt = 0;
for(int i = 1; i < n; i++){
for(int j = 1; j < m; j++){
int t1 = a[i][j], t2 = a[i + 1][j + 1], t3 = a[i][j + 1], t4 = a[i + 1][j];
if(t1 * t2 == t3 * t4)
cnt++;
}
}
cout << cnt;
return0;
}
猜你喜欢
- 2025-06-08 洛谷刷题C++语言 | P1036 选数(洛谷p5719答案c语言)
- 2025-06-08 用C实现协程库(c++20协程库)
- 2025-06-08 树莓派Pico快速上手教程之MicroPython和C使用说明
- 2025-06-08 洛谷刷题C++语言 | P1618 三连击(升级版)
- 2025-06-08 c++中的对齐问题(c++怎么左对齐)
- 2025-06-08 洛谷刷题C++语言 | P1135 奇怪的电梯
- 2025-06-08 小猴编程C++ | 特殊的三位数(小猴编程学而思编程)
- 2025-06-08 洛谷刷题C++语言 | P1424 小鱼的航程(改进版)
- 2025-06-08 小猴编程C++ | 数字替换(小猴编程官网)
- 2025-06-08 C++ 20 module小试(c++实验二)
- 08-06中等生如何学好初二数学函数篇
- 08-06C#构造函数
- 08-06初中数学:一次函数学习要点和方法
- 08-06仓颉编程语言基础-数据类型—结构类型
- 08-06C++实现委托机制
- 08-06初中VS高中三角函数:从"固定镜头"到"360°全景",数学视野升级
- 08-06一文讲透PLC中Static和Temp变量的区别
- 08-06类三剑客:一招修改所有对象!类方法与静态方法的核心区别!
- 1526℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 666℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 533℃MySQL service启动脚本浅析(r12笔记第59天)
- 497℃启用MySQL查询缓存(mysql8.0查询缓存)
- 496℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 483℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 465℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 463℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- windowsscripthost (69)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (70)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)