πŸ”₯ PS(Problem Solving) πŸ”₯/BOJ

[BOJ] #7562 λ‚˜μ΄νŠΈμ˜ 이동

dar0m! 2019. 10. 13. 02:44
μ‹œκ°„ μ œν•œ λ©”λͺ¨λ¦¬ μ œν•œ μ •λ‹΅ λΉ„μœ¨
1 초 256 MB 43.859%

 

 

7562번: λ‚˜μ΄νŠΈμ˜ 이동

문제 체슀판 μœ„μ— ν•œ λ‚˜μ΄νŠΈκ°€ 놓여져 μžˆλ‹€. λ‚˜μ΄νŠΈκ°€ ν•œ λ²ˆμ— 이동할 수 μžˆλŠ” 칸은 μ•„λž˜ 그림에 λ‚˜μ™€μžˆλ‹€. λ‚˜μ΄νŠΈκ°€ μ΄λ™ν•˜λ €κ³  ν•˜λŠ” 칸이 μ£Όμ–΄μ§„λ‹€. λ‚˜μ΄νŠΈλŠ” λͺ‡ 번 움직이면 이 칸으둜 이동할 수 μžˆμ„κΉŒ? μž…λ ₯ μž…λ ₯의 첫째 μ€„μ—λŠ” ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ˜ κ°œμˆ˜κ°€ μ£Όμ–΄μ§„λ‹€. 각 ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€λŠ” μ„Έ μ€„λ‘œ 이루어져 μžˆλ‹€. 첫째 μ€„μ—λŠ” 체슀판의 ν•œ λ³€μ˜ 길이 l(4 ≤ l ≤ 300)이 μ£Όμ–΄μ§„λ‹€. 체슀판의 ν¬κΈ°λŠ” l × l이닀. 체슀판의 각 칸은 두 수의 쌍 {0, ...

www.acmicpc.net

 

문제

체슀판 μœ„μ˜ λ‚˜μ΄νŠΈλ₯Ό λͺ‡ 번 움직여 ν˜„μž¬ μœ„μΉ˜μ—μ„œ λͺ©μ μ§€κΉŒμ§€ 갈 수 μžˆλŠ”μ§€ 횟 수λ₯Ό 좜λ ₯ν•˜λŠ” λ¬Έμ œμ΄λ‹€.

λ‚˜μ΄νŠΈμ˜ 이동

λ‚˜μ΄νŠΈμ˜ 이동을 인덱슀둜 ν‘œν˜„ν•˜μ—¬ dx, dy λΌλŠ” 배열에 μ €μž₯ν•΄λ‘μ—ˆκ³  λ°˜λ³΅λ¬Έμ„ λŒλ©΄μ„œ ν•΄λ‹Ή μΈλ±μŠ€μ—μ„œ λͺ‡ 번째 μ΄λ™μ΄μ—ˆλŠ”μ§€λ₯Ό chk[][]에 μ €μž₯ν•˜κ³ , ν•΄λ‹Ή μΈλ±μŠ€κ°€ λͺ©μ μ§€μ™€ κ°™λ‹€λ©΄ ν•¨μˆ˜λ₯Ό 끝내고 닡을 좜λ ₯ν–ˆλ‹€.

 

μ‹œν–‰μ°©μ˜€

계속 λ§žλŠ”λ° μ™œ ν‹€λ¦΄κΉŒ ν–ˆλ”λ‹ˆ dx λ₯Ό 계속 int dx[] = { -2,-1,1,2,2,1,-1,-1 } 둜 μ“°κ³  μžˆμ—ˆλ‹€. μ œλŒ€λ‘œ 고치고 성곡. λ„ˆλ¬΄λ‚˜λ„ ν—ˆλ¬΄ν–ˆλ‹€. 미리미리 μ‹€μˆ˜ν•˜μ§€ μ•Šκ²Œ κΌΌκΌΌν•˜κ²Œ ν™•μΈν•˜μž.

λ©”λͺ¨λ¦¬ μ‹œκ°„
2352  KB 20 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
43
44
45
46
47
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
typedef pair<intint> p;
int dx[] = { -2,-1,1,2,2,1,-1,-2 }, dy[] = { 1,2,2,1,-1,-2,-2,-1 };
int t, n, chk[305][305], ans;
p start, destination;
void bfs(int x, int y) {
    if (x == destination.first && y == destination.second) {
        ans = 0
        return;
    }
    queue <p> q;
    q.push({ x, y });
    chk[x][y] = 0;
    
    while (!q.empty()) {
        int r = q.front().first, c = q.front().second;
        q.pop();
        for (int i = 0; i < 8; i++) {
            int nr = r + dx[i], nc = c + dy[i];
 
            if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
            if (chk[nr][nc] > 0continue;
            chk[nr][nc] = chk[r][c] + 1;
            if (nr == destination.first && nc == destination.second) {
                ans = chk[nr][nc]; return;
            }
            q.push({ nr, nc });
        }
    }
}
int main() {
    scanf("%d"&t);
    while (t--) {
        scanf("%d"&n);
        scanf("%d %d %d %d"&start.first, &start.second, &destination.first, &destination.second);
        memset(chk, -1sizeof(chk));
        ans = 0;
 
        bfs(start.first, start.second);
 
        printf("%d\n", ans);        
    }
    return 0;
}
cs

 

μ°Έκ³  사항

for 문을 μ•„λž˜μ™€ 같이 μž‘μ„±ν•˜κ²Œ 되면 μ½”λ“œλŠ” 쑰금 쀄어듀지 λͺ°λΌλ„ μ‹œκ°„μ€ 4 ms 만큼 μ¦κ°€ν•œλ‹€!

1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < 8; i++) {
    int nr = r + dx[i], nc = c + dy[i];
 
    if (nr >= 0 && nr < n && nc >= 0 && nc < n && chk[nr][nc] < 0) {
        chk[nr][nc] = chk[r][c] + 1;
        if (nr == destination.first && nc == destination.second) {
            ans = chk[nr][nc]; return;
        }
        q.push({ nr, nc });
    }
}
cs