Hoon222y

[BOJ 13460] 째로탈출2 본문

코딩/BOJ & 알고스팟

[BOJ 13460] 째로탈출2

hoon222y 2017. 4. 11. 00:40

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


정해진 출구까지 이동을 하면서 빨간색 구슬이 파란색 구슬보다 빠르게 나오게 만들어주는 문제이다.


먼저 생각을 해볼것은 "어떠한 방향으로 움직였을때 이동한 결과가 같은 라인에 도착하는가? 아닌가?" 인 듯하다.

이동 결과가 같은 라인일 경우에는 동시에 이동하기 때문에 겹치지 않게 처리를 해주는것이 중요하다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
 
using namespace std;
int n,m,ans,rx,ry,bx,by;
bool visit[11][11][11][11];
char arr[22][22];
int dx[4= {0,0,-1,1};
int dy[4= {1,-1,0,0};
 
int bfs(){
    queue<pair<pair<int,int>,pair<int,int>>> q;
    q.push({{ry,rx},{by,bx}});
    visit[ry][rx][by][bx] = true;
    
    int cnt = 0;
    
    while(!q.empty()){
        int qSize = q.size();
        while(qSize--){
            int rry = q.front().first.first;
            int rrx = q.front().first.second;
            int bby = q.front().second.first;
            int bbx = q.front().second.second;
            q.pop();
            
            if (arr[rry][rrx] == 'O' &&( arr[rry][rrx] != arr[bby][bbx])){  //도착했을떄
                return cnt;
            }
            for(int i=0;i<4;i++){
                int t1,t2,t3,t4;
                t1 = rry;
                t2 = rrx;
                t3 = bby;
                t4 = bbx;
                
                while(arr[t1+dy[i]][t2+dx[i]] != '#' && arr[t1][t2] != 'O'){    //빨간색 이동
                    t1 += dy[i];
                    t2 += dx[i];
                }
                while(arr[t3+dy[i]][t4+dx[i]] != '#' && arr[t3][t4] != 'O'){    //파란색 이동
                    t3 += dy[i];
                    t4 += dx[i];
                }
                
                if((t1 == t3) && (t2 == t4)){               // 만약 같은 라인에 있었다면
                    if(arr[t1][t2] == 'O')
                        continue;
                    if (abs(t1 - rry) + abs(t2 - rrx) < abs(t3 - bby) + abs(t4 - bbx)) {
                        t3 -= dy[i];                        //이동을 덜한 부분처리
                        t4 -= dx[i];
                    }
                    else {
                        t1 -= dy[i];
                        t2 -= dx[i];
                    }
                }
                
                if (visit[t1][t2][t3][t4]){
                    continue;
                }
                q.push({{t1,t2} ,{t3,t4}});
                visit[t1][t2][t3][t4] = true;
                
            }
        }
        if(cnt >= 10){
            return -1;
        }
        cnt++;
    }
    return -1;
}
 
int main(){
    scanf("%d%d" , &n,&m);
    memset(visit,false,sizeof(visit));
    
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf(" %c",&arr[i][j]);
            if (arr[i][j] == 'B') {
                by = i;
                bx = j;
            }
            else if (arr[i][j] == 'R') {
                ry = i;
                rx = j;
            }
        }
    }
    
    printf("%d\n" , bfs());
    
    return 0;
}

cs


같은 라인으로 이동할 때 너무 해맸습니다 ..... ㅠㅠ

'코딩 > BOJ & 알고스팟' 카테고리의 다른 글

[BOJ 1261] 알고스팟  (0) 2017.05.24
[BOJ 14499] 주사위 굴리기  (2) 2017.04.11
[BOJ 2206] 벽 부수고 이동하기  (1) 2017.04.07
[BOJ 1613] 역사  (1) 2017.03.28
[BOJ 1201] NMK  (1) 2017.03.06
Comments