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;
}
相关推荐
MSTcheng.36 分钟前
【数据结构】顺序表和链表详解(下)
数据结构·链表
Q8137574601 小时前
中阳视角下的资产配置趋势分析与算法支持
算法
yvestine1 小时前
自然语言处理——文本表示
人工智能·python·算法·自然语言处理·文本表示
慢半拍iii1 小时前
数据结构——F/图
c语言·开发语言·数据结构·c++
GalaxyPokemon1 小时前
LeetCode - 148. 排序链表
linux·算法·leetcode
iceslime2 小时前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析
aichitang20243 小时前
矩阵详解:从基础概念到实际应用
线性代数·算法·矩阵
OpenCSG3 小时前
电子行业AI赋能软件开发经典案例——某金融软件公司
人工智能·算法·金融·开源
witton4 小时前
美化显示LLDB调试的数据结构
数据结构·python·lldb·美化·debugger·mupdf·pretty printer
chao_7894 小时前
链表题解——环形链表 II【LeetCode】
数据结构·leetcode·链表