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
#include<iostream>
#include <string>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<intint > pii;
 
 
int visited[1010101];
int F, S, G, U, D;
 
int dir[2];
 
int BFS() {
 
    queue<int> q;
 
    q.push(S);
    visited[S] = 1;
 
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
 
        if (cur == G)
            return visited[cur] - 1;
        
 
        for (int i = 0; i < 2; i++) {
            int next = cur + dir[i];
            if (1 <= next && next <= F && visited[next] == 0) {
                visited[next] = visited[cur] + 1;
                q.push(next);
            }
        }
    }
    return -1;
}
 
 
int main(void) {
    cin >> F >> S >> G >> U >> D;
 
    dir[0= U;
    dir[1= -D;
    int ans = BFS();
 
    if (ans == -1)
        cout << "use the stairs";
    else
        cout << ans;
 
    return 0;
}
 
 
 
cs

위 코드는 정답 코드

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
#include<iostream>
#include <string>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
 
 
struct pos {
    int cur, cnt;
};
int visited[1010101];
int F, G, S, U, D;
 
int dir[2];
 
int BFS() {
    
    queue<pos> q;
 
    q.push({ S,0 });
    int ans = -1;
    while (!q.empty()) {
        int cur = q.front().cur;
        int cnt = q.front().cnt;
        q.pop();
        visited[cur] = 1;
        
        if (cur == G) {
            ans = cnt;
            break;
        }
        for (int i = 0; i < 2; i++) {
            int next = cur + dir[i];
            if (1 <= next && next <= F && !visited[next])
                q.push({ next,cnt+1 });
        }
    }
    return ans;
}
 
 
int main(void) {
    cin >> F >> S >> G >> U >> D;
    dir[0= U;
    dir[1= -D;
    int ans = BFS();
    if (ans == -1)
        cout << "use the stairs";
    else
        cout << ans;
}
 
 
 
cs

위 코드는 시간초과 발생하는 코드.. queue인자를 두개 사용해서 풀면 시간초과가 나오는데 이유를 뭔지 

모르겠다..

visied 방문체크가 문제였다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    q.push({ S,0 });
    visited[S]=1;
    int ans = -1;
    while (!q.empty()) {
        int cur = q.front().cur;
        int cnt = q.front().cnt;
        q.pop();
        
        if (cur == G) {
            ans = cnt;
            break;
        }
        for (int i = 0; i < 2; i++) {
            int next = cur + dir[i];
            if (1 <= next && next <= F && !visited[next]){
                visited[next]=1;
                q.push({ next,cnt+1 });
            }
        }
    }
 
 
 
cs
 

 

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

[c++] 백준 1976번 여행 가자 (유니온-파인드)  (0) 2022.09.27
백준 17143번 낚시왕  (0) 2021.05.23
백준 17135번 캐슬 디펜스  (0) 2021.05.12
백준 14502번 연구소  (0) 2019.09.19
백준 2805번 나무자르기  (0) 2019.09.15

+ Recent posts