문제 출처 : https://www.acmicpc.net/problem/14226

<C++Code>

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
#include<iostream>
#include<queue>
#define max 10001
using namespace std;
queue<pair<int,pair<int,int> > >q;
int n;
bool v[max][max];
 
 
void bfs(int start)
{
    q.push({0,{start,0}});
    v[start][0]=true;
    while(!q.empty())
    {
        int x=q.front().second.first;
        int c=q.front().second.second;
        int cnt=q.front().first;
        q.pop();
        //cout<<x<<' ';
        //cout<<cnt<<' ';
            if(x==n)
        {
            cout<<cnt<<endl;
            break;
        }
        int next=x;
        if(next>0&&!v[x][next])
        {
            q.push({cnt+1,{x,next}});
            v[x][next]=true;
        }
        next=x+c;
        if(next>0&&!v[next][c])
        {
            q.push({cnt+1,{next,c}});
            v[next][c]=true;
        }
        next=x-1;
        if(next>0&&!v[next][c])
        {
            q.push({cnt+1,{next,c}});
            v[next][c]=true;
        }
    }
    
}
 
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    cin>>n;
    
    bfs(1);
    
    return 0;
    
}
cs

'백준 온라인 저지 > BFS, DFS (그래프)' 카테고리의 다른 글

2206번_벽 부수고 이동하기  (0) 2020.02.18
1261번_알고스팟  (0) 2019.12.16
2146번_다리 만들기(아직)  (0) 2019.12.15
16940번_BFS 스페셜 저지  (1) 2019.12.15
16964번_DFS 스페셜 저지  (0) 2019.12.15

+ Recent posts