문제풀이(Problem Solving)

백준, BOJ, 2174번, 로봇 시뮬레이션 C++ [CPP] ★★★

게임이 더 좋아 2022. 9. 5. 11:08
반응형
728x170

어렵지 않았다.

다만 귀찮아서 힘들었다.

구현문제는 역시 뭔가 힘들다.

https://www.acmicpc.net/problem/2174

 

 


 

#맞은 풀이

#include <bits/stdc++.h>

using namespace std;

struct Robot{
    int x;
    int y;
    int dir;
    int number;
};

//N (0,1) : 0    S(0,-1) : 1
//W (-1,0) : 2   E(1,0)  : 3

int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0};
 
int board[101][101]; //로봇의 위치 0(없음) 나머지 숫자(있음)

bool isError;

int main(){
    int A,B;
    cin >> A >> B;
    int N,M;
    cin >> N >> M;
    
    vector<Robot> robots;
    
    for(int i = 0; i<N; i++){
        int x,y;
        char dir;
        cin >> x >> y >> dir;
        Robot r;
        r.x = x;
        r.y = y;
        board[x][y] = i+1; //로봇 배치(번호로 배치)
        
        if(dir == 'N'){
            r.dir = 0;
        }else if(dir == 'S'){
            r.dir = 1;
        }else if(dir == 'W'){
            r.dir = 2;
        }else{
            r.dir = 3;
        }
        
        r.number = i+1;
        robots.push_back(r);
    }
    
    for(int i = 0; i<M; i++){
        int robotNum, repeat;
        char command;
        cin >> robotNum >> command >> repeat;
        
        for(int i = 0; i<repeat; i++){
            //zero base이므로 num-1 번째 로봇을 가리킴
            if(command == 'F'){
                auto target = robots[robotNum-1];
                int curX, curY;
                curX = target.x;
                curY = target.y;
                
                int curDir = target.dir;
                //cout << curDir << '\n';
                int nx = curX + dx[curDir];
                int ny = curY + dy[curDir];
                //cout << nx << " " << ny << '\n';
                
                //범위를 벗어나면
                if(nx < 1 || nx > A || ny < 1 || ny > B ){
                    cout << "Robot "<< robotNum <<" crashes into the wall" << '\n';
                    isError = true;
                    break;
                }
                
                //가려는 곳에 로봇이 있다면
                if(board[nx][ny] != 0){

                    cout << "Robot "<< robotNum <<" crashes into robot "<< board[nx][ny] << '\n';
                    isError = true;
                    break;
                }
                
                board[curX][curY] = 0;
                board[nx][ny] = target.number;
                //아래에서 target을 가지고 원본 데이터를 바꾸려고함... 지역변수인데..ㅠ  ★★★★★★★★★★★★★
                robots[robotNum-1].x = nx;
                robots[robotNum-1].y = ny;
                
            }
            //왼쪽 90도
            else if(command == 'L'){
                auto target = robots[robotNum-1];
                switch(target.dir){
                    case 0:
                        robots[robotNum-1].dir = 2;
                        break;
                    case 1:
                        robots[robotNum-1].dir = 3;
                        break;
                    case 2:
                        robots[robotNum-1].dir = 1;
                        break;
                    case 3:
                        robots[robotNum-1].dir = 0;
                        break;
                }
            }
            //오른쪽 90도
            else{
                auto target = robots[robotNum-1];
                switch(target.dir){
                    case 0:
                        robots[robotNum-1].dir = 3;
                        break;
                    case 1:
                        robots[robotNum-1].dir = 2;
                        break;
                    case 2:
                        robots[robotNum-1].dir = 0;
                        break;
                    case 3:
                        robots[robotNum-1].dir = 1;
                        break;
                }
            }
            if(isError)break;
        }
        if(isError)break;
    }
    if(!isError)cout << "OK" << '\n';

    return 0;
}

 

여기서 가장 중요한 것은

출력이다.

그리고 지역변수로 원본을 바꾸려한 바보같은 나를 꾸짖어야 한다.

그리고 방향같은 경우는 case 문을 쓰려면 숫자로 바꿔서 활용하자.

문자 그대로하면 귀찮아진다.

반응형
그리드형