网站首页 > 技术文章 正文
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-09-03 C++性能优化利器:std::move()_c++应用程序性能优化
- 2025-09-03 C++26值得期待的几个特性_c++ %2
- 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 小鱼的航程(改进版)
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (77)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)