ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 BOJ 2536 버스 갈아타기
    Problem Solving/BOJ 2020. 12. 5. 20:35

    시도1 .

    현재위치 cx,cy를 큐에 넣고

    이 위치를 지나가는 버스를 큐를 pop 할때마다 탐색하여 

    타지않은 버스를 타고 그 버스의 경로에 뿌리며 큐에 넣는 방법은

     

    시간초과

     

     

    시도2.

    버스의 경로를 보고 교차점이 존재하면 서로 연결된 노드로 판단한다. ( 5000 * 5000 ) 

    - 가로 가로  , 가로 세로 , 세로 가로, 세로 세로  4가지 케이스로 나누어서 판단.

     

    bfs 탐색

     

    시간과 메모리가 조금 크긴한데 통과하였다.

     

     

     

     

    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    #include<iostream>
    #include<memory.h>
    #include<vector>
    #include<queue>
    #include<unordered_map>
    #include<algorithm>
    #include<string>
    #include<cmath>
    #define INF 1e9
    using namespace std;
    using lld = long long int;
    using pii = pair<intint>;
    int n, m, k;
     
    struct BUS {
        int sx, sy, ex, ey;
    };
    struct POSI {
        int num, cnt;
    };
     
    bool chk[5001];
    BUS bus[5001];
    vector<int> v[5001];
    int sx,sy,dx, dy;
    int ans;
     
    bool range(int x1, int x2, int y1, int y2) {
        if (x1 <= y1 && y2 <= x2) return true;
        if (y1 <= x1 && x2 <= y2) return true;
        if (x1 <= y1 && y1 <= x2) return true;
        if (x1 <= y2 && y2 <= x2) return true;
        return false;
    }
     
    void bfs(int cur) {
        queue<POSI> q;
        q.push({ cur,1 });
        chk[cur] = true;
     
        while (!q.empty()) {
            POSI cur = q.front(); q.pop();
     
            int nn = cur.num;
     
            if (bus[nn].sx == bus[nn].ex) {
                if (bus[nn].sx == dx && bus[nn].sy <= dy && dy <= bus[nn].ey) {
                    ans = min(ans,cur.cnt);
                    break;
                }
            }
            else {
                if (bus[nn].sy == dy && bus[nn].sx <= dx && dx <= bus[nn].ex) {
                    ans = min(ans, cur.cnt);
                    break;
                }
            }
     
            for (int i = 0; i < v[nn].size(); i++) {
                if (chk[v[nn][i]]) continue;
                chk[v[nn][i]] = true;
                q.push({ v[nn][i], cur.cnt + 1 });
            }
     
     
        }
     
     
    }
     
     
    int main() {
        scanf("%d %d %d"&m, &n, &k);
     
        for (int i = 0; i < k; i++) {
            int a, b, c, d, e;
            scanf("%d %d %d %d %d"&a, &b, &c, &d, &e);
            bus[a].sx = min(b,d); 
            bus[a].sy = min(c,e); 
            bus[a].ex = max(b,d); 
            bus[a].ey = max(c,e);
        }
        
        for (int i = 1; i < k; i++) {
            for (int j = i + 1; j <= k; j++) {
                if (bus[i].sy == bus[i].ey) {
                    if (bus[j].sy == bus[j].ey) { 
                        if (bus[i].sy == bus[j].sy && range(bus[i].sx, bus[i].ex, bus[j].sx, bus[j].ex)) {
                            v[i].push_back(j);
                            v[j].push_back(i);
                        }
                    }
                    else {
                        int mm = bus[j].sy;
                        int mmm = bus[j].ey;
                        if (bus[i].sx <= bus[j].sx && bus[j].sx <= bus[i].ex && mm <= bus[i].sy && bus[i].sy <= mmm) {
                            v[i].push_back(j);
                            v[j].push_back(i);
                        }
                        
                    }
     
                }
                else if (bus[i].sx == bus[i].ex) {
                    if (bus[j].sx == bus[j].ex) {
                        if (bus[i].sx == bus[j].sx && range(bus[i].sy, bus[i].ey,bus[j].sy, bus[j].ey)) {
                            v[i].push_back(j);
                            v[j].push_back(i);
                        }
                    }
                    else {
                        int mm = bus[j].sx;
                        int mmm = bus[j].ex;
                        if (bus[i].sy <= bus[j].sy && bus[j].sy<=bus[i].ey && mm <= bus[i].sx && bus[i].sx <= mmm) {
                            v[i].push_back(j);
                            v[j].push_back(i);
                        }
     
                    }
     
                }
            }
        }
        
     
        scanf("%d %d %d %d"&sx, &sy, &dx, &dy);
     
        
        ans = INF;
        for (int i = 1; i <= k; i++) {
            memset(chk, falsesizeof(chk));
            if (bus[i].sx == bus[i].ex) {
                if (bus[i].sx == sx && bus[i].sy <= sy && sy <= bus[i].ey) {
                    bfs(i);
                }
            }
            else {
                if (bus[i].sy == sy && bus[i].sx <= sx && sx <= bus[i].ex) {
                    bfs(i);
                }
            }
        }
     
        printf("%d", ans);
     
     
        return 0;
    }
    cs

    'Problem Solving > BOJ' 카테고리의 다른 글

    백준 BOJ 2568 - 전깃줄-2  (0) 2020.12.31
    백준 BOJ 14725 개미굴  (0) 2020.12.29
    백준 BOJ 5719 거의 최단 경로  (0) 2020.12.05
    백준 BOJ 2904 수학은 너무 쉬워  (0) 2020.10.31
    19237 BOJ 어른상어  (0) 2020.10.11
Designed by Tistory.