문제 풀이

 

 bfs 알고리즘을 활용하여 방문하지 않은 길을 따라 도착점이 나올 때까지 탐색하였습니다.

 

 

코드

 

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
#include <cstdio>
#include <queue>
 
using namespace std;
 
int ans;
int map[16][16];
bool isVisit[16][16];
int dx[] = { 00-11 };
int dy[] = { -1100 };
 
void bfs(int a, int b)
{
    queue <pair<intint>> q;
    q.push(make_pair(a, b));
    isVisit[a][b] = true;
 
    while (!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && ny >= 0 && nx < 16 && ny < 16)
            {
                //도착점이라면 ans = 1로 하고, 리턴
                if (map[nx][ny] == 3)
                {
                    ans = 1;
                    return;
                }
                //방문한 적 없는 길인 경우
                else if (map[nx][ny] == 0 && isVisit[nx][ny] == false)
                {
                    isVisit[nx][ny] = true;
                    q.push(make_pair(nx, ny));
                }
            }
        }
    }
}
 
int main() {
 
    while(1)
    {
        int tc;
        scanf("%d"&tc);
        ans = 0;
        int x = 0, y = 0//starting point;
        for (int i = 0; i < 16; i++)
        {
            for (int j = 0; j < 16; j++)
            {
                scanf("%1d"&map[i][j]);
 
                if (map[i][j] == 1)
                    isVisit[i][j] = true;
 
                else if (map[i][j] == 2)
                {
                    x = i;
                    y = j;
                    isVisit[i][j] = false;
                }
 
                else
                    isVisit[i][j] = false;
            }
        }
 
        bfs(x, y);
 
        printf("#%d %d\n", tc, ans);
        
        if (tc == 10)
            break;
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5; text-decoration:none">Colored by Color Scripter

 

 

제출 결과

 

 

 

문제 출처

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14vXUqAGMCFAYD&categoryId=AV14vXUqAGMCFAYD&categoryType=CODE&&&

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

+ Recent posts