본문 바로가기
  • 소소한 개발자 이야기
Algorithm Study/BOJ with C++

(BAEKJOON) 13460번: 구슬 탈출2

by Siwan_Min 2020. 7. 8.
728x90

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

 

13460번: 구슬 탈출 2

첫 번째 줄에는 보드의 세로, 가로 크기를 의미하는 두 정수 N, M (3 ≤ N, M ≤ 10)이 주어진다. 다음 N개의 줄에 보드의 모양을 나타내는 길이 M의 문자열이 주어진다. 이 문자열은 '.', '#', 'O', 'R', 'B'

www.acmicpc.net

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
 
struct INFO {
    int ry, rx, by, bx, count;
};
 
INFO start;
char map[10][11];
 
using namespace std;
 
int bfs(){
    const int dy[] = {-1100};
    const int dx[] = {00-11};\
    
    int visited[10][10][10][10= {0, };
    queue <INFO> q;
    q.push(start);
    visited[start.ry][start.rx][start.by][start.bx] = 1;
 
    int ret = -1;
    while (!q.empty())
    {
        INFO cur = q.front(); 
        q.pop();
        if(cur.count>10break;
        if(map[cur.ry][cur.rx] == 'O' && map[cur.by][cur.bx] != 'O'){
            ret = cur.count;
            break;
        }
        for(int dir = 0; dir < 4++dir){
            
            int next_ry = cur.ry;
            int next_rx = cur.rx;
            int next_by = cur.by;
            int next_bx = cur.bx;
 
            while(1){
                if(map[next_ry][next_rx] != '#' && map[next_ry][next_rx] !='O'){
                    next_ry += dy[dir], next_rx += dx[dir];
                }
                else {
                    if(map[next_ry][next_rx] == '#'  ){
                        next_ry -= dy[dir], next_rx -= dx[dir];
                    }
                    break;
                }
            }
 
            while(1){
                if(map[next_by][next_bx] != '#' && map[next_by][next_bx] !='O'){
                    next_by += dy[dir], next_bx += dx[dir];
                }
                else {
                    if(map[next_by][next_bx] == '#'  ){
                        next_by -= dy[dir], next_bx -= dx[dir];
                    }
                    break;
                }
            }
 
            if(next_ry == next_by && next_rx == next_bx){
                if(map[next_ry][next_rx] != 'O'){
                    int red_dist = abs(next_ry - cur.ry) + abs(next_rx - cur.rx);
                    int blue_dist = abs(next_by - cur.by) + abs(next_bx - cur.bx);
                    if(red_dist > blue_dist){
                        next_ry -= dy[dir], next_rx -= dx[dir];
                    }
                    else{
                        next_by -= dy[dir], next_bx -= dx[dir];
                    }
                }
            }
            if(visited[next_ry][next_rx][next_by][next_bx] == 0){
                visited[next_ry][next_rx][next_by][next_bx] = 1;
                INFO next;
                next.ry = next_ry;
                next.rx = next_rx;
                next.by = next_by;
                next.bx = next_bx;
                next.count = cur.count + 1;
                q.push(next);
            }
        }
    }
    return ret;
}
 
int main(){
    int n, m;
   scanf("%d %d"&n, &m);
   for(int i = 0; i < n; ++i){
       scanf("%s", map[i]);
   }
 
    for(int y = 0; y < n; ++y){
        for(int x = 0; x < m; ++x){
            if(map[y][x] == 'R'){
                start.ry = y, start.rx = x;
            }
            if(map[y][x] == 'B'){
                start.by = y, start.bx = x;
            }
        }
    }
    start.count = 0;
 
    int ret = bfs();
    cout<<ret<<endl;
    return 0
}

 

728x90

댓글