반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. BFS

2. Map STL


[1697번] 숨바꼭질 :: http://www.crocus.co.kr/627

위의 문제에서 단 하나가 수정되었다.


            if (x * <= MAX && x * >= && visit[x * 2== 0)
                q.push(pii(cnt, x * 2));


이 부분이 숨바꼭질 문제에서는 


            if (x * <= MAX && x * >= && visit[x * 2== 0)
                q.push(pii(cnt + 1, x * 2));


이었는데, 결국 순간이동 시 0초의 시간이 든다고 했으니 cnt + 1이 아닌 cnt로 바꾸어주면 문제가 해결이 된다.


소스 코드 : 


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
#include <cstdio>
#include <iostream>
#include <queue>
#include <limits.h>
 
using namespace std;
 
#define MAX 100000
 
typedef pair<intint> pii;
queue <pii> q;
 
int visit[200001];
 
int main()
{
    int pos, target;
    scanf("%d %d"&pos, &target);
 
    q.push(pii(0, pos));
 
    int ret = INT_MAX;
    int cnt = 0;
 
    if (pos == target)
        printf("0");
 
 
    else
    {
        while (!q.empty())
        {
            int x = q.front().second;
            int cnt = q.front().first;
 
            q.pop();
            visit[x] = 1;
 
            if (x == target)
                ret = min(ret, cnt);
 
            if (x * <= MAX && x * >= && visit[x * 2== 0)
                q.push(pii(cnt, x * 2));
 
            if (x - >= && x - <= MAX && visit[x - 1== 0)
                q.push(pii(cnt + 1, x - 1));
 
            if (x + <= MAX && x + >= && visit[x + 1== 0)
                q.push(pii(cnt + 1, x + 1));
 
        }
        printf("%d", ret);
    }
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[1949번] 우수 마을  (0) 2017.03.03
[1620번] 나는야 포켓몬 마스터 이다솜  (0) 2017.03.03
[12851번] 숨바꼭질 2  (0) 2017.02.28
[1697번] 숨바꼭질  (0) 2017.02.28
[2014번] 소수의 곱  (1) 2017.02.28