[프로그래머스] 거리두기 확인하기

1 minute read

문제 설명

https://programmers.co.kr/learn/courses/30/lessons/81302

제한사항


풀이

50분
단순하게 탐색하는 쉬운 문제였다. 이렇게까지 오래 생각할 문제가 아니었는데… 생각이 너무 많았나 싶기도 하고, 딴짓을 하지는 않았지만 집중력이 부족했나 싶기도 하다.

코드

// 06:11 ~ 07:00
#include <string>
#include <vector>

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

bool indexCheck(int x, int y){
    if(x < 0 || y < 0 || x >= 5 || y >= 5) return false;
    return true;
}

bool distCheck(vector<string>& place){
    for(int i =0; i < 5; ++i){
        for(int j=0; j < 5; ++j){
            if(place[i][j] != 'P') continue;
            
            for(int d = 0; d < 4; ++d){
                int ni = i + dy[d];
                int nj = j + dx[d];
                if(indexCheck(ni, nj) == false) continue;
                if(place[ni][nj] == 'P'){
                    return false;
                }
                
                if(indexCheck(ni + dy[d], nj + dx[d]) == false) continue;
                if(place[ni][nj] != 'X' && place[ni + dy[d]][nj + dx[d]] == 'P'){
                    return false;
                }
            }
            
            for(int d = 0; d < 4; d += 2){
                int ni = i + dy[d + 1];
                int nj = j + dx[d];
                if(indexCheck(ni, nj) == false) continue;
                if(place[ni][nj] == 'P' && (place[i][nj] != 'X' || place[ni][j] != 'X')){
                    return false;
                }
                
                ni = i + dy[(d + 3) % 4];
                if(indexCheck(ni, nj) == false) continue;
                if(place[ni][nj] == 'P' && (place[i][nj] != 'X' || place[ni][j] != 'X')){
                    return false;
                }
            }
            
        }
    }
    return true;
}

vector<int> solution(vector<vector<string>> places) {
    vector<int> answer;
    for(vector<string> place : places){
        if(distCheck(place)) answer.push_back(1);
        else answer.push_back(0);
    }
    return answer;
}