1915번: 가장 큰 정사각형

문제접근🤔


//pseudo code
vector<vector<int> > dp
int n,m

cin >> n,m
cin >> dp

while //n+1행부터
	while //m+1열부터

놓쳤던 부분😅


  1. 최대 길이를 구하는 것이 아니라 최대 넓이를 구하는 것

    최대 길이를 구했기 때문에 제곱하면 됨

  2. 최대 길이를 구하는 조건

    			if (i == 0 || j == 0)
    			{
    				max = std::max(dp[i][j], max);
    				continue ;
    			}
    

    나의 코드에서는 [1][어디든] [어디든][1] 은 입력 받은 결과 그대로를 쓰기 때문에 저 인덱스들에 대해서는 max값이 업데이트가 되지 않음

    예를들어

    0 1 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    

    에서 1을 업데이트 하지 못하고 max = 0이 되어 버리기 때문에 조건을 추가해야함

코드😁


5996 KB

12 ms

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int n,m;
std::vector<std::vector<int> > dp;

void input_setting()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
}

void input()
{
	int i;
	int j;
	std::string input_arr;

	i = 0;
	std::cin >> n >> m;
	dp.resize(n + 1, std::vector<int>(m + 1));
	while (++i <= n)
	{
		j = 0;
		std::cin >> input_arr;
		while (++j <= m)
			dp[i][j] = input_arr[j - 1] - '0';
	}
}

void solution()
{
	int i;
	int j;
	int max;

	i = 0;
	max = 0;
	while (++i <= n)
	{
		j = 0;
		while (++j <= m)
		{
			if (dp[i][j] == 0)
				continue ;
			if (i == 0 || j == 0)
			{
				max = std::max(dp[i][j], max);
				continue ;
			}
			dp[i][j] = std::min(std::min(dp[i][j - 1], dp[i - 1][j - 1]), dp[i - 1][j]) + 1;
			max = std::max(dp[i][j], max);
		}
	}
	std::cout << max * max;
}

int main(void)
{
	input_setting();
	input();
	solution();
	return (0);
}