알고리즘/프로그래머스 1단계
[프로그래머스 1단계] 바탕화면 정리 - c++
셉인
2024. 3. 9. 11:51
728x90
[프로그래머스 1단계] 바탕화면 정리 - c++
풀이방법
result : [시작 y좌표 , 시작 x 좌표 , 끝 y좌표, 끝 x좌표] 를 출력해줘야한다.
시작지점 x,y좌표(start_x,start_y) 는 제일 크기가 작아야 하니깐 제일 작은 것을 넣어준다
근데 0으로 초기화 하면 첫 값이 1 일때 값이 안들어가는 상황이 발생하므로 무조건 첫 수보다 큰 수인 1000으로 초기화 해줬다.
종료지점 x,y좌표(lastline_x,lastline_y) 은 제일 크기가 커야한다. 그래서 그냥 무조건 0보다 크니깐 0으로 초기화 해서 변수를 설정해줬다.
그리고 키포인트가 마지막에 lastline_x,lastline_y좌표에 +1을 해줘야한다는 것이다.
마우스 보면 #이 위치한 곳이 아닌, +1+1해준 (#을 포함하는 곳) 곳에 위치한 것을 볼 수 있다.
개선점
min, max 함수 이용하면 좀 더 간결한 코드를 작성할 수 있다.
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<int> solution(vector<string> wallpaper) {
vector<int> answer;
int lastline_x=0;
int lastline_y=0;
int start_x=1000;
int start_y=1000;
for(int y=0; y<wallpaper.size(); y++){
for(int x=0; x<wallpaper[y].size(); x++){
if(wallpaper[y][x]=='#'){
if(y<start_y){
start_y=y;
}
if(start_x>x){
start_x=x;
}
if(x>lastline_x){
lastline_x=x;
}
if(y>lastline_y){
lastline_y=y;
}
}
}
}
answer.push_back(start_y);
answer.push_back(start_x);
answer.push_back(lastline_y+1);
answer.push_back(lastline_x+1);
return answer;
}
[프로그래머스/1단계] 바탕화면 정리 - c++
정답률 48%
728x90