반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. BFS


기본 BFS에 memset을 하며 각 숫자를 계속 찾아가는 방식을 취하면 된다.


w,h의 값이 더 커지거나 memset을 하기 싫다면 visit를 int형으로 바꾸어 visitCnt를 이용해보자.


시간적인 측면에서 많은 이득을 볼 것이다.(대신 bool->int형으로 바뀌기에 메모리는 더 많이 먹게된다.)




소스 코드 : 


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
#include <iostream>
#include <cstdio>
#include <queue>
#include <memory.h>
 
using namespace std;
 
typedef pair<intint> pii;
typedef pair<charint> pci;
 
char arr[1003][1003];
bool visit[1003][1003];
 
int dy[4= { 1,0,-1,};
int dx[4= { 0,1,0,-};
 
int main()
{
    int w, h, n;
    int sx, sy;
    scanf("%d %d %d"&w, &h, &n);
 
    for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++)
        {
            scanf(" %c"&arr[i][j]);
            if (arr[i][j] == 'S')
                sy = i, sx = j;
        }
 
 
    queue<pair<pci, pii> > q;
 
    q.push({ {'1'0}, {sy, sx} });
 
    while (!q.empty())
    {
        int y = q.front().second.first;
        int x = q.front().second.second;
        char cnt = q.front().first.first;
        int num = q.front().first.second;
 
        q.pop();
        
        if (visit[y][x])
            continue;
 
        visit[y][x] = true;
 
        if (arr[y][x] == cnt)
        {
            while (!q.empty())
                q.pop();
 
            q.push({ {cnt + 1, num}, {y,x} });
            memset(visit, falsesizeof(visit));
            
            if(cnt == n + '0')
            {
                printf("%d", num);
                return 0;
            }
        
            continue;
        }
 
        
        for (int i = 0; i < 4; i++)
        {
            int ny = y + dy[i];
            int nx = x + dx[i];
 
            if (!(<= ny && ny < w) || !(<= nx && nx < h) || visit[ny][nx] || arr[ny][nx] == 'X')
                continue;
 
            q.push({ {cnt, num + 1},{ny,nx} });
        }
 
    }
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus



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
#include <iostream>
#include <cstdio>
#include <queue>
 
using namespace std;
 
typedef pair<intint> pii;
typedef pair<charint> pci;
 
char arr[1003][1003];
int visit[1003][1003];
int visitCnt = 1;
 
int dy[4= { 1,0,-1,};
int dx[4= { 0,1,0,-};
 
int main()
{
    int w, h, n;
    int sx, sy;
    scanf("%d %d %d"&w, &h, &n);
 
    for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++)
        {
            scanf(" %c"&arr[i][j]);
            if (arr[i][j] == 'S')
                sy = i, sx = j;
        }
 
 
    queue<pair<pci, pii> > q;
 
    q.push({ {'1'0}, {sy, sx} });
 
    while (!q.empty())
    {
        int y = q.front().second.first;
        int x = q.front().second.second;
        char cnt = q.front().first.first;
        int num = q.front().first.second;
 
        q.pop();
        
        if (visit[y][x] == visitCnt)
            continue;
 
        visit[y][x] = visitCnt;
 
        if (arr[y][x] == cnt)
        {
            while (!q.empty())
                q.pop();
 
            q.push({ {cnt + 1, num}, {y,x} });
            visitCnt++;
            
            if(cnt == n + '0')
            {
                printf("%d", num);
                return 0;
            }
        
            continue;
        }
 
        
        for (int i = 0; i < 4; i++)
        {
            int ny = y + dy[i];
            int nx = x + dx[i];
 
            if (!(<= ny && ny < w) || !(<= nx && nx < h) || visit[ny][nx] == visitCnt || arr[ny][nx] == 'X')
                continue;
 
            q.push({ {cnt, num + 1},{ny,nx} });
        }
 
    }
 
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

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

[3022번] 식사 예절  (0) 2017.11.10
[14891번] 톱니바퀴  (0) 2017.11.09
[3186번] 소변기  (0) 2017.10.25
[14888번] 연산자 끼워넣기  (0) 2017.10.25
[14729번] 칠무해  (0) 2017.10.11