이 영역을 누르면 첫 페이지로 이동
caputdraconis 블로그의 첫 페이지로 이동

caputdraconis

페이지 맨 위로 올라가기

caputdraconis

네트워크 관점에서의 클라우드 컴퓨팅을 공부하는 중입니다 :)

[C++] 거리두기 확인하기(2021 카카오 채용연계형 인턴십 코딩테스트)

  • 2021.11.03 17:33
  • 🥚고리즘
글 작성자: caputdraconis
반응형

/*
2021 카카오 채용연계형 인턴십 코딩테스트 문제에 대한 C++ 해결방법입니다.
2번째 코드는 같은 문제를 BFS로 해결한 코드입니다.
*/

 

문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/81302

 

코딩테스트 연습 - 거리두기 확인하기

[["POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"], ["POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"], ["PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"], ["OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"], ["PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"]] [1, 0, 1, 1, 1]

programmers.co.kr

 

 

#include <bits/stdc++.h>

// 생략 가능(gcc)
#define and &&
#define or ||

using namespace std;

// 기준점 기준 상하좌우 체크(맨해튼 거리 1)
int dx1[4] = {-1, 0, 1, 0};
int dy1[4] = {0, -1, 0, 1};

// 기준점 기준 대각선 체크(맨해튼 거리 2)
int dx2[4] = {1, 1, -1, -1};
int dy2[4] = {1, -1, -1, 1};

// 기준점 기준 직선거리 2 체크(맨해튼 거리 2)
int dx3[4] = {-2, 0, 2, 0};
int dy3[4] = {0, -2, 0, 2};

bool OOB(int x, int y)
{
    // Check Out of Bound
    // return true => Out of Bound Happend
    return x < 0 or x > 4 or y < 0 or y > 4;
}

int chk(vector<string> board)
{
    // return true => there are people only who obeyed to rule
    // return false => there is a person who not obeyed to rule

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            if (board[i][j] == 'X' or board[i][j] == 'O')
                continue;

            // 여기부터는 board[i][j] == 'P"

            for (int dir = 0; dir < 4; dir++)
            {
                int nx = i + dx1[dir];
                int ny = j + dy1[dir];

                if (OOB(nx, ny))
                    continue;
                if (board[nx][ny] == 'P')
                    return false;
            }

            for (int dir = 0; dir < 4; dir++)
            {
                int nx = i + dx2[dir];
                int ny = j + dy2[dir];

                if (OOB(nx, ny))
                    continue;

                if (board[nx][ny] == 'P')
                {
                    // 거쳐오는 길에 파티션이 있나 체크
                    if (board[i][ny] != 'X' or board[nx][j] != 'X')
                        return false;
                }
            }

            for (int dir = 0; dir < 4; dir++)
            {
                int nx = i + dx3[dir];
                int ny = j + dy3[dir];

                if (OOB(nx, ny))
                    continue;
                if (board[nx][ny] == 'P')
                {
                    // 전 테이블에 파티션이 있는 경우 체크
                    if (board[i + dx1[dir]][j + dy1[dir]] != 'X')
                        return false;
                }
            }
        }
    }
    return true;
}

vector<int> solution(vector<vector<string>> places)
{
    vector<int> answer;

    for (int room = 0; room < 5; room++)
    {
        answer.push_back(chk(places.at(room)));
    }

    return answer;
}

 

 

With BFS

#include <bits/stdc++.h>

#define X first
#define Y second

using namespace std;

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

int dist[5][5];

bool OOB(int x, int y)
{
    // Check Out of Bound
    // return true => Out of Bound Happend
    return x < 0 or x > 4 or y < 0 or y > 4;
}

int chk(vector<string> board)
{
    // return true => there are people only who obeyed to rule
    // return false => there is a person who not obeyed to rule
    queue<pair<int, int>> Q;
    fill(&dist[0][0], &dist[4][5], -1);

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            if (board[i][j] == 'O' or board[i][j] == 'X')
            {
                continue;
            }

            dist[i][j] = 0;
            Q.push(make_pair(i, j));

            while (!Q.empty())
            {
                pair<int, int> cur = Q.front();
                Q.pop();

                for (int dir = 0; dir < 4; dir++)
                {
                    int nx = cur.X + dx[dir];
                    int ny = cur.Y + dy[dir];

                    if (OOB(nx, ny))
                        continue;

                    if (i == nx && j == ny)
                        continue;
                    dist[nx][ny] = dist[cur.X][cur.Y] + 1;

                    if (dist[nx][ny] > 2)
                        continue;
                    if (board[nx][ny] == 'X')
                        continue;

                    if (board[nx][ny] == 'P')
                    {
                        return false;
                    }

                    if (board[nx][ny] == 'O' && dist[nx][ny] != 2)
                        Q.push(make_pair(nx, ny));
                }
            }
        }
    }
    return true;
}

vector<int> solution(vector<vector<string>> places)
{
    vector<int> answer;

    for (int room = 0; room < 5; room++)
    {
        answer.push_back(chk(places.at(room)));
    }

    return answer;
}

 

반응형

댓글

이 글 공유하기

  • 구독하기

    구독하기

  • 카카오톡

    카카오톡

  • 라인

    라인

  • 트위터

    트위터

  • Facebook

    Facebook

  • 카카오스토리

    카카오스토리

  • 밴드

    밴드

  • 네이버 블로그

    네이버 블로그

  • Pocket

    Pocket

  • Evernote

    Evernote

다른 글

  • [Algorithms] Dynamic Programming

    [Algorithms] Dynamic Programming

    2023.05.26
  • [C++] 백준 BOJ 21939 문제 추천 시스템 Version 1

    [C++] 백준 BOJ 21939 문제 추천 시스템 Version 1

    2021.11.18
  • [Python] 숫자 문자열과 영단어(2021 카카오 채용연계형 인턴십 코딩테스트)

    [Python] 숫자 문자열과 영단어(2021 카카오 채용연계형 인턴십 코딩테스트)

    2021.11.03
  • [C++] 백준 BOJ 5525 IOIOI

    [C++] 백준 BOJ 5525 IOIOI

    2021.10.28
다른 글 더 둘러보기

정보

caputdraconis 블로그의 첫 페이지로 이동

caputdraconis

  • caputdraconis의 첫 페이지로 이동

검색

메뉴

    카테고리

    • 분류 전체보기 (168)
      • Cloud (3)
      • Computer Network (12)
      • Database (2)
      • Terraform (2)
      • 🥚고리즘 (13)
      • 겅부겅부🙃 (10)
        • Naver CS50 코칭스터디 (2)
        • Machine Learning (1)
        • Computing System (6)
      • 언어&프레임워크 (20)
        • Python (4)
        • Django (10)
        • Node JS (1)
        • C++ (2)
        • Java (1)
        • Flutter (2)
      • Security (76)
        • WebHacking Study (11)
        • 지옥방 스터디 (22)
        • 여름방학 스터디 (2)
        • PWN Study (6)
        • SUA Reversing Study (3)
        • PWN (3)
        • WebHacking (20)
        • Reversing (4)
      • 알고 있으면 도움되지 않을까,,? (23)
      • 일상다반사 (1)
      • 근황 정리 (1)
      • 42 Seoul (1)
        • Setting (1)

    최근 글

    인기 글

    댓글

    공지사항

    아카이브

    태그

    • 파이썬기초
    • Python
    • 웹해킹.kr
    • 파이썬함수
    • 드림핵
    • 리스트함수
    • 파이썬
    • old-16

    나의 외부 링크

    • Github
    • solved.ac
    • caputdraconis@kakao.com

    정보

    caputdraconis의 caputdraconis

    caputdraconis

    caputdraconis

    블로그 구독하기

    • 구독하기
    • RSS 피드

    방문자

    • 전체 방문자
    • 오늘
    • 어제

    티스토리

    • 티스토리 홈
    • 이 블로그 관리하기
    • 글쓰기
    Powered by Tistory / Kakao. Copyright © caputdraconis.

    티스토리툴바