반응형

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

 

16954번: 움직이는 미로 탈출

욱제는 학교 숙제로 크기가 8×8인 체스판에서 탈출하는 게임을 만들었다. 체스판의 모든 칸은 빈 칸 또는 벽 중 하나이다. 욱제의 캐릭터는 가장 왼쪽 아랫 칸에 있고, 이 캐릭터는 가장 오른쪽

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 체스판을 arr 배열에 입력받고, visited 배열을 이용하여 방문을 체크해 줍니다.

 

2. 현재 위치에서 이동 가능한 좌표들을 queue에 넣은 후 장애물을 옮겨줍니다.

 

3. 가장 오른쪽 위에 도착할 수 있다면 1을 그렇지 않다면 0을 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<queue>
 
using namespace std;
 
char arr[9][8];
int visited[9][8][10];
int temp;
int dy[9= { -1,-1,0,1,1,1,0,-1,0 };
int dx[9= { 0,1,1,1,0,-1,-1,-1,0 };
 
struct node {
    int y;
    int x;
    int cnt;
};
 
void move()
{
    for (int i = 7; i >= 1; i--) {
        for (int j = 0; j < 8; j++) {
            arr[i][j] = arr[i - 1][j];
        }
    }
 
    for (int j = 0; j < 8; j++) {
        arr[0][j] = '.';
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    for (int i = 0; i < 8; i++) {
        cin >> arr[i];
    }
 
    queue<node> q;
 
    q.push({ 8,0,0 });
    visited[8][0][0= 1;
 
    while (!q.empty()) {
        const int y = q.front().y;
        const int x = q.front().x;
        const int cnt = q.front().cnt;
        q.pop();
 
        if (y == 1 && x == 7) {
            cout << 1;
            return 0;
        }
 
        if (temp != cnt) {
            temp = cnt;
            move();
        }
 
        for (int i = 0; i < 8; i++) {
            int yy = y + dy[i];
            int xx = x + dx[i];
 
            if (yy >= 1 && yy < 9 && xx >= 0 && xx < 8) {
                if (arr[yy][xx] == '.' && arr[yy - 1][xx] != '#' && visited[yy][xx][cnt + 1!= 1) {
                    visited[yy][xx][cnt + 1= 1;
                    q.push({ yy,xx,cnt + 1 });
                }
            }
        }
    }
 
    cout << 0;
}
cs
반응형

+ Recent posts