์๊ฐ ์ ํ | ๋ฉ๋ชจ๋ฆฌ ์ ํ | ์ ๋ต ๋น์จ |
1 ์ด | 128 MB | 44.604 % |
2644๋ฒ: ์ด์๊ณ์ฐ
์ฌ๋๋ค์ 1, 2, 3, โฆ, n (1โคnโค100)์ ์ฐ์๋ ๋ฒํธ๋ก ๊ฐ๊ฐ ํ์๋๋ค. ์ ๋ ฅ ํ์ผ์ ์ฒซ์งธ ์ค์๋ ์ ์ฒด ์ฌ๋์ ์ n์ด ์ฃผ์ด์ง๊ณ , ๋์งธ ์ค์๋ ์ด์๋ฅผ ๊ณ์ฐํด์ผ ํ๋ ์๋ก ๋ค๋ฅธ ๋ ์ฌ๋์ ๋ฒํธ๊ฐ ์ฃผ์ด์ง๋ค. ๊ทธ๋ฆฌ๊ณ ์ ์งธ ์ค์๋ ๋ถ๋ชจ ์์๋ค ๊ฐ์ ๊ด๊ณ์ ๊ฐ์ m์ด ์ฃผ์ด์ง๋ค. ๋ท์งธ ์ค๋ถํฐ๋ ๋ถ๋ชจ ์์๊ฐ์ ๊ด๊ณ๋ฅผ ๋ํ๋ด๋ ๋ ๋ฒํธ x,y๊ฐ ๊ฐ ์ค์ ๋์จ๋ค. ์ด๋ ์์ ๋์ค๋ ๋ฒํธ x๋ ๋ค์ ๋์ค๋ ์ ์ y์ ๋ถ๋ชจ ๋ฒํธ๋ฅผ ๋ํ๋ธ๋ค. ๊ฐ ์ฌ๋์ ๋ถ๋ชจ๋ ์ต๋
www.acmicpc.net
์ ์ฒด ์ฌ๋ ์ n, ์ด์๋ฅผ ๊ณ์ฐํด์ผ ํ๋ ์๋ก ๋ค๋ฅธ ์ฌ๋์ ๋ฒํธ x, y ๊ด๊ณ์ ์ m ์ด ์ฃผ์ด์ง๋ฉด ์๋์ m๊ฐ์ ์ค์ ๋ถ๋ชจ-์์ ๊ด๊ณ๋ฅผ ๋ํ๋ด๋ a,b๊ฐ ์ฃผ์ด์ง๋ค.
m๊ฐ์ ์ค์ ํด๋นํ๋ ๊ด๊ณ์ ๋ฐ๋ผ์ vector ๋ก 2์ฐจ์ ๋ฐฐ์ด์ ํ์ฑํ๊ณ bfs๋ฅผ ํ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์๋ค.
๋ฉ๋ชจ๋ฆฌ | ์๊ฐ |
1988 KB | 0 ms |
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
|
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int, int> p;
int n, m, x, y;
vector<vector<int>> v;
queue<p> q;
int chk[101];
int main() {
scanf("%d %d %d %d", &n, &x, &y, &m);
v.resize(n * m);
for (int i = 0, a, b; i < m; i++) {
scanf("%d %d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
q.push({ x,0 });
while (!q.empty()) {
int pnum = q.front().first;
int pcnt = q.front().second;
q.pop();
if (pnum == y) {
printf("%d", pcnt);
return 0;
}
for (auto e : v[pnum]) {
if (!chk[e]) {
chk[e] = 1;
q.push({ e, pcnt + 1 });
}
}
}
printf("-1");
return 0;
}
|
cs |
'๐ฅ PS(Problem Solving) ๐ฅ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] #1541 ์์ด๋ฒ๋ฆฐ ๊ดํธ (0) | 2019.09.04 |
---|---|
[BOJ] #2573 ๋น์ฐ (0) | 2019.09.04 |
[BOJ] #2667 ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (0) | 2019.09.04 |
[BOJ] #6603 ๋ก๋ (0) | 2019.09.04 |
[BOJ] #14502 ์ฐ๊ตฌ์ (0) | 2019.09.04 |
๋๊ธ