5168. 冲击哥 (Standard IO)

 

#include<iostream>;
#include<algorithm>;
#include<vector>;
using namespace std;
int n, m;
char ans[110][110];
int dir[4][2] = { 1,0,-1,0,0,1,0,-1 };
 
char get(int x0,int y0) {
    vector<char> tmp;
    if (ans[x0][y0])return ans[x0][y0];
    for (int i = 0;i < 4;i++) {
        int x = x0 + dir[i][0];
        int y = y0 + dir[i][1];
        if (x > n || x <= 0 || y > m || y <= 0)continue;
        if (ans[x][y] != 0)tmp.push_back(ans[x][y]);
    }
    for (char ch = 'A';ch <= 'Z';ch++) {
        bool help = true;
        for (char cch : tmp)
            if (cch == ch) {
                help = false;
                break;
            }
        if (help)return ch;
    }
}
 
int main() {
    ios::sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 1;i <= n;i++) {
        for (int j = 1;j <= m;j++) {
            if (ans[i][j])continue;
            char mychar = get(i, j);
            int len = 0;
            while (i + len <= n && j + len <= m && get(i, j + len) == mychar)len++;
            for (int k = 0;k < len;k++)
                for (int w = 0;w < len;w++)
                    ans[i + k][j + w] = mychar;
        }
    }
    for (int i = 1;i <= n;i++) {
        for (int j = 1;j <= m;j++)
            cout << ans[i][j];
        cout << endl;
    }
}

 

您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~

ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓

【电脑端】戳>>>  https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】  戳>>>  https://mall.csdn.net/item/52471?utm_source=1146287632