본문 바로가기

알고리즘/SW EXPERT

4112. 이상한 피라미드 탐험

4112. 이상한 피라미드 탐험


BFS 문제였기 때문에, 각 노드간에 이동할 수 있는지 여부만 추려내면 되는 문제였다.


근데 나는 딱히 방법이 떠오르지 않아서 약간 노가다 식으로 진행했다.


일단 1인경우, 맨 왼쪽 대각선인 경우, 맨 오른쪽 대각선이 경우, 그외 이렇게 나누어서 문제를 풀었다.


중복계산을 피하기 위해 tc안에 들어가기 전에,


각 숫자들을 for문으로 돌면서 해당하는 라인과 왼쪽인지 오른쪽인지를 판단하도록 했다.


이때 1은 왼쪽,오른쪽 둘다 걸쳐 있다고 체크를 해줬다.


그리고 BFS를 돌면서, 다음 정점을 찾을 때는 1인지,왼쪽대각선인지,오른쪽대각선인지, 그 외 경우인지 나누어서 함수를 진행했다. 


그리고 사실 이동할 때 1이상 1만이하인지를 체크해줘야 하는데 귀찮아서 배열을 아주 크게 만들어서 문제를 풀었다.


<정답 코드>


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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef struct {
    bool left = false;
    bool right = false;
    int line = 0;
}n;
int dist[20001];
bool check[20001];
n num[20001];
 
void init()
{
    for (int i = 0; i < 10001; i++)
    {
        dist[i] = 0;
        check[i] = false;
    }
}
 
void chk_left(int x, queue<int> &q)
{
    int L = num[x].line;
    if (!check[x + 1 - L])
    {
        check[x + 1 - L] = true;
        q.push(x + 1 - L);
        dist[x + 1 - L] = dist[x] + 1;
    }
    if (!check[x + 1])
    {
        check[x + 1= true;
        q.push(x + 1);
        dist[x + 1= dist[x] + 1;
    }
    if (!check[x + 1 + L])
    {
        check[x + 1 + L] = true;
        q.push(x + 1 + L);
        dist[x + 1 + L] = dist[x] + 1;
    }
    if (!check[x + L])
    {
        check[x + L] = true;
        q.push(x +L);
        dist[x +L] = dist[x] + 1;
    }
 
}
 
void chk_right(int x, queue<int> &q)
{
    int L = num[x].line;
    if (!check[x - L])
    {
        check[x - L] = true;
        q.push(x - L);
        dist[x - L] = dist[x] + 1;
    }
    if (!check[x - 1])
    {
        check[x - 1= true;
        q.push(x - 1);
        dist[x - 1= dist[x] + 1;
    }
    if (!check[x  + L])
    {
        check[x  + L] = true;
        q.push(x  + L);
        dist[x  + L] = dist[x] + 1;
    }
    if (!check[x + 1 + L])
    {
        check[x + L+1= true;
        q.push(x + L+1);
        dist[x + L+1= dist[x] + 1;
    }
}
void chk(int x, queue<int> &q)
{
    int L = num[x].line;
    if (!check[x - L])
    {
        check[x - L] = true;
        q.push(x - L);
        dist[x - L] = dist[x] + 1;
    }
    if (!check[x - L + 1])
    {
        check[x - L+1= true;
        q.push(x - L+1);
        dist[x - L+1= dist[x] + 1;
    }
    if (!check[x - 1])
    {
        check[x - 1= true;
        q.push(x - 1);
        dist[x - 1= dist[x] + 1;
    }
    if (!check[x + 1])
    {
        check[x +1= true;
        q.push(x + 1);
        dist[x +1= dist[x] + 1;
    }
    if (!check[x + L])
    {
        check[x + L] = true;
        q.push(x + L);
        dist[x + L] = dist[x] + 1;
    }
    if (!check[x + L + 1])
    {
        check[x + L+1= true;
        q.push(x + L+1);
        dist[x + L+1= dist[x] + 1;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int tc;
    cin >> tc;
 
    int sum = 1;
    for (int i = 1; sum <= 10000; i++)
    {
        num[sum].left = true;
        sum += i;
    }
    sum = 1;
    for (int i = 2; sum <= 10000; i++)
    {
        num[sum].right = true;
        sum += i;
    }
    sum = 1;
    for (int i = 1; sum <= 10000; i++)
    {
        for (int j = sum; j < sum + i; j++)
        {
            num[j].line = i;
        }
        sum += i;
    }
 
    for (int t = 1; t <= tc; t++)
    {
        init();
        int a, b;
        cin >> a >> b;
        init();
        queue<int> q;
        q.push(a);
 
        while (!q.empty())
        {
            int now = q.front();
            q.pop();
 
            if (now == b)
            {
                break;
            }
 
            if (num[now].left && num[now].right)
            {
                if (!check[2])
                {
                    check[2= true;
                    q.push(2);
                    dist[2= dist[now] + 1;
                }
                if (!check[3])
                {
                    check[3= true;
                    q.push(3);
                    dist[3= dist[now] + 1;
                }
                continue;
            }
 
            if (num[now].left && !num[now].right)
            {
                chk_left(now, q);
                continue;
            }
 
            if (!num[now].left && num[now].right)
            {
                chk_right(now, q);
                continue;
            }
 
            chk(now, q);
            continue;
        }
 
        cout << "#" << t << " " << dist[b] << "\n";
 
    }
    return 0;
}
cs


반응형

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

1249.보급로  (0) 2018.04.06
2814. 최장 경로  (0) 2018.03.30
4111. 무선 단속 카메라  (0) 2018.03.27
3124. 최소 스패닝 트리  (0) 2018.03.23
4050. 재관이의 대량 할인  (0) 2018.03.21