8/12 题解

解题思路

贪心,小的搭配大的,和会最小

AC代码

cpp 复制代码
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int l = 1;
    int r = n;
    while(l < r){   
        cout << l << ' ';
        l++;
        cout << r << ' ';
        --r;
        if(l == r){
            cout << l << ' ';
            break;
        }
    }
    return 0;
}

解题思路

模拟

AC代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
int res;

bool safe(vector<vector<char>> &v, int px, int py){
    if(v[px][py] != v[px][py + 1] && v[px][py + 1] != v[px][py + 2] && v[px][py] != v[px + 1][py] && v[px][py + 1] != v[px + 1][py + 1] && v[px][py + 2] != v[px + 1][py + 2] && v[px + 1][py] != v[px + 1][py + 1] && v[px + 1][py + 1] != v[px + 1][py + 2] && v[px + 1][py] != v[px + 2][py] && v[px + 1][py + 1] != v[px + 2][py + 1] && v[px + 1][py + 2] != v[px + 2][py + 2] && v[px + 2][py] != v[px + 2][py + 1] && v[px + 2][py + 1] != v[px + 2][py + 2]){
        return true;
    }
    return false;
}

bool judge(vector<vector<char>> &v, int x, int y){
    if(x + 3 > n || y + 3 > m || x < 0 || y < 0){
        return false;
    }
    if(!safe(v, x, y)){
        return false;
    }
    int mp[3] = {0,0,0};
    for(int i = x; i < x + 3; ++i){
        for(int j = y; j < y + 3; ++j){
            int num = v[i][j] - 'A';
            if(num >= 3){
                return false;
            }
            ++mp[num];            
        }
    }
    for(int i = 0; i < 3; ++i){        
        if(!mp[i]){
            return false;
        }
    }
    return true;
}

int main() {
    cin >> n >> m;
    vector<vector<char>> v(n, vector<char>(m));
    for(int i =0 ; i < n; ++i){
        for(int j = 0; j < m; ++j){
            cin >> v[i][j];
        }
    }
    for(int i =0 ; i < n; ++i){
        for(int j = 0; j < m; ++j){
            if(judge(v, i ,j)){
                ++res;
            }
        }
    }
    cout << res;
    return 0;
}
相关推荐
橙几6 分钟前
击败了90%的解法?Two Sum 从 O(n²) 到 O(n) 的优化之路
算法
叶子爱分享20 分钟前
经典排序算法之归并排序(Merge Sort)
算法·排序算法
珹洺26 分钟前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
呆呆的小鳄鱼1 小时前
leetcode:冗余连接 II[并查集检查环][节点入度]
算法·leetcode·职场和发展
墨染点香1 小时前
LeetCode Hot100【6. Z 字形变换】
java·算法·leetcode
沧澜sincerely1 小时前
排序【各种题型+对应LeetCode习题练习】
算法·leetcode·排序算法
CQ_07121 小时前
自学力扣:最长连续序列
数据结构·算法·leetcode
弥彦_1 小时前
cf1925B&C
数据结构·算法
YuTaoShao2 小时前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
Wendy14419 小时前
【线性回归(最小二乘法MSE)】——机器学习
算法·机器学习·线性回归