'알고리즘'에 해당되는 글 40건

문제 출처 : https://www.acmicpc.net/problem/6593

 

6593번: 상범 빌딩

문제 당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어 지나갈 수 없거나, 비어있어서 지나갈 수 있게 되어있다. 당신은 각 칸에서 인접한 6개의 칸(동,서,남,북,상,하)으로 1분의 시간을 들여 이동할 수 있다. 즉, 대각선으로 이동하는 것은 불가능하다. 그리고 상범 빌딩의 바깥면도 모두 금으로 막혀있어 출구를 통해서

www.acmicpc.net

이 문제는 BFS 기법을 사용하여 문제를 해결했다.

층수와 가로 세로가 다 있어서 3차원 배열을 선언해서 dx, dy, dz 배열로 이동하는 총 경우의 수를 저장하였다.

아래는 해당 문제를 풀이한 소스 코드이다.

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
#include<stdio.h>
#include<queue>
#pragma warning(disable:4996)
using namespace std;
int L, R, C;
int flag = 0;
char arr[31][31][31];
int visit[31][31][31];
int dx[6= {0,0,1,-1,0,0};
int dy[6= {0,0,0,0,1,-1};
int dz[6= {1,-1,0,0,0,0};
struct list
{
    int x;
    int y;
    int z;
    int cnt = 0;
};
queue <list> que;
void BFS()
{
    while (!que.empty())
    {
        list temp = que.front();
        que.pop();
        if (arr[temp.z][temp.x][temp.y] == 'E')
        {
            flag = 1;
            printf("Escaped in %d minute(s).\n"temp.cnt);
            return;
        }
        for (int i = 0; i < 6; i++)
        {
            int nx = temp.x + dx[i];
            int ny = temp.y + dy[i];
            int nz = temp.z + dz[i];
            if (nx >= 0 && nx < R && ny >= 0 && ny < C && nz >= 0 && nz < L)
            {
                if (arr[nz][nx][ny] != '#' && visit[nz][nx][ny] == 0)
                {
                    list A;
                    A.x = nx;
                    A.y = ny;
                    A.z = nz;
                    A.cnt = temp.cnt + 1;
                    visit[nz][nx][ny] = 1;
                    que.push(A);
                }
            }
        }
    }
}
void reset()
{
    for (int i = 0; i < L; i++)
    {
        for (int j = 0; j < R; j++)
        {
            for (int k = 0; k < C; k++)
            {
                arr[i][j][k] = ' ';
                visit[i][j][k] = 0;
            }
        }
    }
    while (!que.empty())
        que.pop();
    flag = 0;
}
int main()
{
    while (1)
    {
        scanf("%d %d %d"&L, &R, &C);
        if (L == 0 && R == 0 && C == 0)
            return 0;
        for (int i = 0; i < L; i++)
        {
            for (int j = 0; j < R; j++)
            {
                scanf("%s"&arr[i][j]);
            }
        }
        for (int i = 0; i < L; i++)
        {
            for (int j = 0; j < R; j++)
            {
                for (int k = 0; k < C; k++)
                {
                    if (arr[i][j][k] == 'S')
                    {
                        list temp;
                        temp.x = j;
                        temp.y = k;
                        temp.z = i;
                        que.push(temp);
                        visit[i][j][k] = 1;
                    }
                }
            }
        }
        BFS();
        if (flag == 0)
            printf("Trapped!\n");
        reset();
    }
    
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
블로그 이미지

뀨심볼

깃허브 주소는 : https://github.com/hhyc2 입니다~

,