algorithm/문제 풀이

99클럽 코테 스터디 19일차 TIL - 프로그래머스 공원 산책

ssoheeh 2024. 4. 19. 21:10

#include <bits/stdc++.h>

using namespace std;
pair<int,int> start;
int board[52][52];
vector<int> solution(vector<string> park, vector<string> routes) {
    vector<int> answer;
    int re[2];
    for(int j=0;j<park.size();j++){
        for(int i=0;i<park[j].length();i++){
            if(park[j][i]=='S'){
                start = make_pair(j,i);
            }else if(park[j][i]=='O'){
                board[j][i]= 1;
            }else if(park[j][i]=='X'){
                board[j][i]= -1;
            }
        }
    }
    
    queue<pair<int,int>> q;
    q.push(start);
    int cnt = 0;
    while(!q.empty()){
        pair<int,int> x = q.front();
        q.pop();
        pair<int,int> nxt;
        if(cnt>=routes.size())break;
        int n = routes[cnt][2]-'0';
        bool check = false;
        switch(routes[cnt][0]){
            case 'E':
                nxt = make_pair(x.first, x.second+n);
                break;
            case 'W':
                nxt = make_pair(x.first, x.second-n);
                break;
            case 'S':
                nxt = make_pair(x.first+n, x.second);
                break;
            case 'N':
                nxt = make_pair(x.first-n, x.second);
                break;
        }
       
        if(nxt.first>=park.size()||nxt.first<0||nxt.second>=park[0].length()||nxt.second<0){
            q.push(x);
            cnt++;
            continue;
                                                                                           }
        switch(routes[cnt][0]){
            case 'E':
                for(int i=1;i<=n;i++){
                    if(board[x.first][x.second+i]==-1){
                        check = true;
                        break;
                    }
                }
                cnt++;
                break;
            case 'W':
                
                for(int i=1;i<=n;i++){
                    if(board[x.first][x.second-i]==-1){
                        check = true;
                        break;
                    }
                }
                cnt++;
                break;
            case 'S':
                for(int i=1;i<=n;i++){
                    if(board[x.first+i][x.second]==-1){
                        check = true;
                        break;
                    }
                }
                cnt++;
                break;
            case 'N':
                for(int i=1;i<=n;i++){
                    if(board[x.first-i][x.second]==-1){
                        check = true;
                        break;
                    }
                }
                cnt++;
                break;
        }
        
        if(check){
            q.push(x);
            continue;
        }
      
        re[0] = nxt.first;
        re[1] = nxt.second;
        q.push(nxt);
        
    }
    if(re[0]>=park.size()||re[1]>=park[0].length()){
        answer.push_back(start.first);
    answer.push_back(start.second);
    }else{
    answer.push_back(re[0]);
    answer.push_back(re[1]);
    }
    return answer;
}

bfs 문제인데 엣지케이스(시작부분)을 생각하지 못해서 테스트케이스 2개를 통과하지 못했었다

 

 

오늘의 회고

  • 코드를 좀 더 깔끔하게 짤 수 있도록 연습하자
  • 엣지케이스 고려에 좀 더 힘쓸 것!
  • 내일(4/20) 학습 예정 : 중간고사 공부