본문 바로가기
dev/코딩테스트

[Codetree] 2개의 사탕

by dev-everyday 2025. 4. 7.
반응형

문제

https://www.codetree.ai/ko/frequent-problems/problems/two-candies/description?introductionSetId=&bookmarkId=

 

삼성 코딩테스트 기출 문제 설명: 2개의 사탕 | 코드트리

삼성전자 코딩테스트 기출 문제 2개의 사탕의 상세 설명입니다. 문제 요구사항을 정확히 파악하고 효율적인 알고리즘을 설계해보세요.

www.codetree.ai

 

풀이

BFS로 구현하였다.

BFS로 넘겨줄 때 필요한 정보를 Candy 구조체를 통해서 나타내었고 빨간 공의 x, y 좌표와 파란 공의 x, y 좌표 그리고 depth를 하나의 구조체로 설정해서 넘겼다.

 

코드

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, m;
int visited[11][11][11][11];
int dx[4]={-1,1,0,0}, dy[4]={0,0,-1,1};
vector<vector<char>> v(11);

struct Candy{
    int rx, ry, bx, by, depth;
};

pair<int, int> move(int x, int y, int dir, int& count){
    count = 0;
    while(1){
        int nx=x+dx[dir];
        int ny=y+dy[dir];
        if(nx<0||ny<0||nx>n-1||ny>m-1)break;
        if(v[nx][ny]=='#')break;
        x=nx, y=ny;
        count++;
        if (v[nx][ny] == 'O') break;
    }
    return make_pair(x, y);
}

int main(){
    cin >> n >> m;

    int rx, ry, bx, by, depth;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            char k;
            cin >> k;
            v[i].push_back(k);
            if(v[i][j]=='R'){
                rx = i;
                ry = j;
                v[i][j]='.';
            } else if(v[i][j]=='B'){
                bx = i;
                by = j;
                v[i][j]='.';
            }
        }
    }

    queue<Candy> q;
    q.push({rx, ry, bx, by, 0});

    int ans = 11;
    while(!q.empty()){
        Candy candy = q.front();
        q.pop();
        rx = candy.rx;
        ry = candy.ry;
        bx = candy.bx;
        by = candy.by;
        depth = candy.depth;

        if(depth>10){
            continue;
        }

        for(int i=0;i<4;i++){
            int redCount=0, blueCount=0;
            pair<int,int> newRedCandy = move(rx, ry, i, redCount);       
            pair<int,int> newBlueCandy = move(bx, by, i, blueCount);       
            int nrx = newRedCandy.first, nry = newRedCandy.second, nbx = newBlueCandy.first, nby = newBlueCandy.second;

           if(nrx==nbx&&nry==nby&&v[nbx][nby]!='O'){
               if(redCount>blueCount){
                    nrx-=dx[i];
                    nry-=dy[i];
                } else{
                    nbx-=dx[i];
                    nby-=dy[i];
                }
           }

           if(v[nbx][nby]=='O'){
                continue;
           }
           if(v[nrx][nry]=='O'){
                ans = min(ans, depth+1);
                continue;
           }

            if(visited[nrx][nry][nbx][nby]==0){
                visited[nrx][nry][nbx][nby]=1;
                q.push({nrx, nry, nbx, nby, depth+1});
            }
        }
    }

    cout << (ans==11?-1:ans) <<endl;

    return 0;
}
반응형

'dev > 코딩테스트' 카테고리의 다른 글

[Codetree] 바이러스 검사  (0) 2025.04.04
[Programmers - DP] N으로 표현  (0) 2025.03.24
[BOJ - 2169] 로봇 조종하기  (1) 2025.03.20
[Softeer - Lv2] 회의실 예약  (6) 2025.02.28
[Softeer - Lv2] 전광판  (1) 2025.02.27