我服了,自查了一天,一直20个点AC11个,求助!!!

B4005 [GESP202406 四级] 黑白方块

wntt @ 2024-09-16 21:18:11

HELP!!!
#include<bits/stdc++.h>
using namespace std;
int pass()
{
    return 0;
}

int main()
{
    //input
    int n, m;
    cin >> n >> m;
    int a[n][m] = {0};

    for (int i = 0; i < n; ++i) {
        char str[m];
        cin >> str;

        for (int j = 0; j < m; ++j) {
            if (str[j] == '0') {
                a[i][j] = 1;
            }
            else if (str[j] == '1') {
                a[i][j] = 2;
            }
            else {
                pass;
            }
        }
    }

    //statistics
    int max = 0;

    //Matrix ergodicity
    for (int x1 = 0; x1 <= n; ++x1) {
        for (int y1 = 0; y1 <= m; ++y1) {
            for (int x2 = x1; x2 <= n; ++x2) {
                for (int y2 = y1; y2 <= m; ++y2) {
                    //Balance judgment
                    int white = 0, black = 0;

                    for (int i = 0; i < n; ++i) {
                        for (int j = 0; j < m; ++j) {
                            if (i >= x1 && i <= x2 && j >= y1 && j <= y2) {
                                if (a[i][j] == 1) {
                                    white++;
                                }
                                else if (a[i][j] == 2) {
                                    black++;
                                }
                            }
                        }
                    }

                    if (white == black) {
                        int x = x2 - x1, y = y2 - y1, area;
                        area = x*y;

                        if (max < area) {
                            max = area;
                        }
                    }
                }
            }
        }
    }

    //output
    cout << max << endl;
    return 0;
}

自查了一天,一直做不出来,啊啊啊


by TillTheEnd @ 2024-09-16 21:40:36

@wntt

  1. 最后统计答案时,边长是 x_2-x_1+1,而非 x_2-x_1


by TillTheEnd @ 2024-09-16 21:44:41

另外不要用 cin 搭配字符串数组,用 std::string


by wntt @ 2024-09-16 22:01:31

@TillTheEnd 这样吗?

string str;
cin>>str;
cout<<str[num];

by wntt @ 2024-09-16 22:02:16

@TillTheEnd 谢谢指导,已关注


by TillTheEnd @ 2024-09-16 22:07:43

@wntt string 是字符串,str[num]char 类型的,意思是 str 的第 num 位。


by TillTheEnd @ 2024-09-16 22:08:41

应该是对的吧


by wntt @ 2024-09-16 23:27:00

@TillTheEnd 没事,我其他的改了就AC了,谢谢大佬


|