개발자 김수진

[백준] 2931번. 가스관 본문

알고리즘/백준

[백준] 2931번. 가스관

김수진장 2021. 1. 5. 02:41

[문제]

 

www.acmicpc.net/problem/2931

 

2931번: 가스관

러시아 가스를 크로아티아로 운반하기 위해 자그레브와 모스코바는 파이프라인을 디자인하고 있다. 두 사람은 실제 디자인을 하기 전에 파이프 매니아 게임을 이용해서 설계를 해보려고 한다.

www.acmicpc.net

 

[풀이]

 

맞아서 기분이 좋긴 하지만 뭔가 찝찝하다.

3 5
. . . . .
. M . Z .
. . . . .

 

위와 같은 예제는 답이 제대로 나오지 않는다.

 

시작점을 기준으로 DFS 함수를 통해 한칸씩 나아가도록 하였다.

이 때 현재 위치의 값이 '.' 이라면 solve 함수에서 해당 위치에 들어갈 값을 찾도록 하였다.

 

 

[코드]

 

시간 : 0ms

 

#include <iostream>
#define MAX 26

using namespace std;

struct pos {
    int x,y;
};

char map[MAX][MAX]={0,};

int R,C;

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

pos start;

void solve(int x, int y)
{
    cout << x+1 << " " << y+1 << " ";
    bool dir[4] = {0,};
    
    for(int i=0;i<4;i++){
        
        int nx = x+dx[i];
        int ny = y+dy[i];
        
        if(nx<0 || ny< 0 || nx >= R || ny >= C )   continue;
        
        if(map[nx][ny] != '.'){
            if(i == 0 && (map[nx][ny] == '1' || map[nx][ny] == '4' || map[nx][ny] == '|' || map[nx][ny] =='+'))  dir[i]=true;
            else if(i == 1 && (map[nx][ny] == '3' || map[nx][ny] == '4' || map[nx][ny] == '-' || map[nx][ny] =='+'))  dir[i]=true;
            else if(i == 2 && (map[nx][ny] == '2' || map[nx][ny] == '3' || map[nx][ny] == '|' || map[nx][ny] =='+'))  dir[i]=true;
            else if(i == 3 && (map[nx][ny] == '1' || map[nx][ny] == '2' || map[nx][ny] == '-' || map[nx][ny] =='+'))  dir[i]=true;

        }
    }
    if(dir[0] && dir[1] && dir[2] && dir[3])    cout << "+";
    else if(dir[1] && dir[2])   cout << "1";
    else if(dir[0] && dir[1])   cout << "2";
    else if(dir[0] && dir[3])   cout << "3";
    else if(dir[2] && dir[3])   cout << "4";
    else if(dir[0] && dir[2])   cout << "|";
    else if(dir[1] && dir[3])   cout << "-";
    
    return ;
}

void DFS(int x, int y, int dir)
{
    if(map[x][y] == '.'){
        solve(x,y);
        return ;
    }
    
    int nx = x+dx[dir];
    int ny = y+dy[dir];
    
    if(dir ==0 ){
        if(map[nx][ny] == '|' || map[nx][ny] == '+')    dir = 0;
        else if(map[nx][ny] =='1')    dir= 1;
        else if(map[nx][ny] =='4') dir = 3;
    }
    else if(dir == 1 ){
        if(map[nx][ny] == '-' || map[nx][ny] == '+')    dir = 1;
        else if(map[nx][ny] =='3')    dir= 0;
        else if(map[nx][ny] =='4') dir = 2;
    }
    else if(dir == 2){
        if(map[nx][ny] == '|' || map[nx][ny] == '+')    dir = 2;
        else if(map[nx][ny] =='2')    dir= 1;
        else if(map[nx][ny] =='3') dir = 3;
    }
    else if(dir == 3){
        if(map[nx][ny] == '-' || map[nx][ny] == '+')    dir = 3;
        else if(map[nx][ny] =='1')    dir= 2;
        else if(map[nx][ny] =='2') dir = 0;
    }
    DFS(nx,ny,dir);
    return;
}

int main(void)
{

    cin >> R>>C;
    for (int i=0; i<R; i++){
        for(int j=0;j<C;j++){
            cin >> map[i][j];
            if(map[i][j]== 'M')  start = {i,j};
        }
    }
    
    int Direction =0;
    
    for (int i = 0; i < 4; i++) {
        int nx =start.x + dx[i];
        int ny =start.y + dy[i];

        if (nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
        if (map[nx][ny] != '.') {
            if (nx ==start.x) {
                if (ny == start.y - 1)  Direction = 3;
                else if (ny == start.y+ 1)  Direction = 1;
            }
            else if (ny == start.y) {
                if (nx == start.x - 1)  Direction = 0;
                else if (nx ==start.x + 1)  Direction = 2;
            }
        }
    }

    DFS(start.x,start.y,Direction);
    return 0;
}

'알고리즘 > 백준' 카테고리의 다른 글

[백준]20056번. 마법사 상어와 파이어볼  (2) 2021.01.07
[백준] 15653번. 구슬 탈출4  (0) 2021.01.07
[백준]1756번. 피자 굽기  (0) 2021.01.04
[백준] 13913번. 숨바꼭질4  (0) 2021.01.03
[백준] 2589. 보물섬  (0) 2021.01.01