코딩 테스트/프로그래머스 코딩테스트
[프로그래머스] 공원 산책 C++
ingus kinematics
2025. 3. 8. 15:24
https://school.programmers.co.kr/learn/courses/30/lessons/172928?language=cpp
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
개어렵다..
테스트는 통과했지만
채점 결과, 무자비한 Segmentation fault...
#include <string>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<string> park, vector<string> routes) {
vector<int> answer;
vector<vector<char>> map;
vector<char> map_row;
int cur_row = 0;
int cur_col = 0;
int SIZE_ROW = 0;
int SIZE_COL = 0;
// 2차원 벡터에 지도 정보 넣기 -> map
// 시작 지점 S 찾아두기 -> cur_row, cur_col
for(int i=0; i<park.size(); i++){
map_row.clear();
for(int j=0; j<park[i].size(); j++){
char map_info = park[i][j];
if(map_info == 'S'){
cur_row = i;
cur_col = j;
// cout << "find the start point = " << cur_row << ", " << cur_col << endl;
}
map_row.push_back(map_info);
}
map.push_back(map_row);
}
// 맵 크기 입력
SIZE_ROW = map.size()-1;
SIZE_COL = map[0].size()-1;
// cout << "SIZE_ROW = " << SIZE_ROW << endl;
// cout << "SIZE_COL = " << SIZE_COL << endl;
// Routes 탐색
for(int i=0; i<routes.size(); i++){
string route_info = routes[i];
char dir = route_info[0];
int dist = route_info[2] - 48;
// printf("============================\n");
// printf("(dir = %c, dist = %d)\n", dir, dist);
// 이동하기
int temp_row = cur_row;
int temp_col = cur_col;
// printf("Cur Pos (%d) = (%d, %d)\n", i, cur_row, cur_col);
bool flag = true;
for(int d=1; d<=dist; d++){
// printf("Temp Pos (%d) = (%d, %d)\n", d, temp_row, temp_col);
// temp_row = cur_row;
// temp_col = cur_col;
if(dir == 'N'){
temp_row -= 1;
}
else if(dir == 'S'){
temp_row += 1;
}
else if(dir == 'W'){
temp_col -= 1;
}
else if(dir == 'E'){
temp_col += 1;
}
// 조건 1. 장애물 -> 무효 처리
if(map[temp_row][temp_col] == 'X')
{
// printf("조건 1에 의한 무효\n");
flag = false;
break;
}
// 조건 2. 위치 제한 -> 무효 처리
if(temp_row > SIZE_ROW ||
temp_row < 0 ||
temp_col > SIZE_COL ||
temp_col < 0)
{
// printf("조건 2에 의한 무효\n");
flag = false;
break;
}
flag &= flag;
}
if(flag == true)
{
// printf("위치 적용!\n");
cur_row = temp_row;
cur_col = temp_col;
// printf("위치 적용 이후 좌표 = (%d, %d)\n", cur_row, cur_col);
}
}
// printf("Goal = (%d, %d)", cur_row, cur_col);
answer.push_back(cur_row);
answer.push_back(cur_col);
return answer;
}
코드가 너무 기네요.. 다른 사람들의 풀이를 보고 다시 한 번 생각해보겠습니다.
아,, GPT 한테 물어보니까 조건 1과 조건 2의 순서만 바꿔주면 되는거였네요..
#include <string>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<string> park, vector<string> routes) {
vector<int> answer;
vector<vector<char>> map;
vector<char> map_row;
int cur_row = 0;
int cur_col = 0;
int SIZE_ROW = 0;
int SIZE_COL = 0;
// 2차원 벡터에 지도 정보 넣기 -> map
// 시작 지점 S 찾아두기 -> cur_row, cur_col
for(int i=0; i<park.size(); i++){
map_row.clear();
for(int j=0; j<park[i].size(); j++){
char map_info = park[i][j];
if(map_info == 'S'){
cur_row = i;
cur_col = j;
// cout << "find the start point = " << cur_row << ", " << cur_col << endl;
}
map_row.push_back(map_info);
}
map.push_back(map_row);
}
// 맵 크기 입력
SIZE_ROW = map.size()-1;
SIZE_COL = map[0].size()-1;
// cout << "SIZE_ROW = " << SIZE_ROW << endl;
// cout << "SIZE_COL = " << SIZE_COL << endl;
// Routes 탐색
for(int i=0; i<routes.size(); i++){
string route_info = routes[i];
char dir = route_info[0];
int dist = route_info[2] - 48;
// printf("============================\n");
// printf("(dir = %c, dist = %d)\n", dir, dist);
// 이동하기
int temp_row = cur_row;
int temp_col = cur_col;
// printf("Cur Pos (%d) = (%d, %d)\n", i, cur_row, cur_col);
bool flag = true;
for(int d=1; d<=dist; d++){
// printf("Temp Pos (%d) = (%d, %d)\n", d, temp_row, temp_col);
// temp_row = cur_row;
// temp_col = cur_col;
if(dir == 'N'){
temp_row -= 1;
}
else if(dir == 'S'){
temp_row += 1;
}
else if(dir == 'W'){
temp_col -= 1;
}
else if(dir == 'E'){
temp_col += 1;
}
// 조건 2. 위치 제한 -> 무효 처리
if(temp_row > SIZE_ROW ||
temp_row < 0 ||
temp_col > SIZE_COL ||
temp_col < 0)
{
// printf("조건 2에 의한 무효\n");
flag = false;
break;
}
// 조건 1. 장애물 -> 무효 처리
if(map[temp_row][temp_col] == 'X')
{
// printf("조건 1에 의한 무효\n");
flag = false;
break;
}
flag &= flag;
}
if(flag == true)
{
// printf("위치 적용!\n");
cur_row = temp_row;
cur_col = temp_col;
// printf("위치 적용 이후 좌표 = (%d, %d)\n", cur_row, cur_col);
}
}
// printf("Goal = (%d, %d)", cur_row, cur_col);
answer.push_back(cur_row);
answer.push_back(cur_col);
return answer;
}
왜냐하면 temp_row, temp_col의 좌표가 벗어날 수 있기 떄문에..
GPT가 저보다 코드를 잘 짜네요 ㅎㅎ..