https://school.programmers.co.kr/learn/courses/30/lessons/161990
가장 왼쪽좌표와 가장 위쪽좌표, 가장 오른쪽 좌표와 가장 아래쪽좌표를 구하면된다.
W, H 를 구한다.
비교해가면서 점 2개의 값을 담을 min, max를 선언해둔다.
wallpaper을 순회하면서 #을 만났을 때 해당 위치의 좌표 x,y 가 가장 왼쪽인지 위쪽인지 아래쪽인지 오른쪽인지 다 검사해서 갱신해준다.
다 검사한 뒤 answer에 넣어준다.
(다른방법생각) 모든 #좌표를 구해서 컨테이너에 넣어준 뒤 거기서 min, max를 구하는 방법도 좋을 것 같다.
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<string> wallpaper) {
vector<int> answer;
//width, height 구하기
int w = wallpaper[0].length();
int h = wallpaper.size();
//비교하면서 바로 점 2개 값 담기
pair<int,int> min = {50,50};
pair<int,int> max = {0,0};
//#의 좌표 담기
for(int i = 0;i < h; i++)
for(int j = 0; j < w; j++)
{
if(wallpaper[i][j] == '#')
{
if(min.first > j)
min.first = j;
if(min.second > i)
min.second = i;
if(max.first < j + 1)
max.first = j + 1;
if(max.second < i + 1)
max.second = i + 1;
}
}
answer.emplace_back(min.second);
answer.emplace_back(min.first);
answer.emplace_back(max.second);
answer.emplace_back(max.first);
return answer;
}
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<string> wallpaper) {
vector<int> answer{100,100,-100,-100};
const int n = wallpaper.size();
const int m = wallpaper[0].size();
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < m ; ++j)
{
if(wallpaper[i][j] == '#')
{
answer[0] = min(answer[0],i);
answer[1] = min(answer[1],j);
answer[2] = max(answer[2],i+1);
answer[3] = max(answer[3],j+1);
}
}
}
return answer;
}
answer을 초기화 해두고 #의 좌표를 검사할 때 min, max함수를 사용했다.