Notice
                              
                          
                        
                          
                          
                            Recent Posts
                            
                        
                          
                          
                            Recent Comments
                            
                        
                          
                          
                            Link
                            
                        
                    | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
                            Tags
                            
                        
                          
                          - 줄 세우기
 - C
 - directx
 - 2294
 - 언리얼엔진5
 - 프로그래머스
 - RVO
 - UnrealEngine5
 - 오블완
 - Unreal Engine5
 - C++
 - 팰린드롬 만들기
 - GeeksForGeeks
 - NRVO
 - 1563
 - Frustum
 - const
 - baekjoon
 - Programmers
 - IFileDialog
 - 티스토리챌린지
 - winapi
 - softeer
 - RootMotion
 - UnrealEngine4
 - algorithm
 - TObjectPtr
 - UE5
 - DirectX11
 - 백준
 
                            Archives
                            
                        
                          
                          - Today
 
- Total
 
Game Develop
[Algorithm] Baekjoon 17142번 : 연구소 3 본문
https://www.acmicpc.net/problem/17142
17142번: 연구소 3
인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 바이러스는 활성 상태와 비활성 상태가 있다. 가장 처음에 모든 바이러스는 비활성 상태이고,
www.acmicpc.net
| 
 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 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
 | 
 struct Node 
{ 
    int y; 
    int x; 
    int turn = 0; 
}; 
int n, m; 
int arr[51][51] = { 0 }; 
int dirs[4][2] = { {-1,0}, {1,0}, {0,-1}, {0,1} }; 
int targetCount = 0; 
vector<Node> viruses; 
vector<int> virusIndexes; 
int BFS() 
{ 
    bool visited[51][51] = { false }; 
    int answerCount = 0; 
    queue<Node> q; 
    for (int i = 0; i < m; ++i) 
    { 
        q.push({ viruses[virusIndexes[i]] }); 
        visited[viruses[virusIndexes[i]].y][viruses[virusIndexes[i]].x] = true; 
    } 
    while (!q.empty()) 
    { 
        int curY = q.front().y; 
        int curX = q.front().x; 
        int curTurn = q.front().turn; 
        q.pop(); 
        for (int i = 0; i < 4; ++i) 
        { 
            int nextY = curY + dirs[i][0]; 
            int nextX = curX + dirs[i][1]; 
            if (nextY < 0 || nextY == n) continue; 
            if (nextX < 0 || nextX == n) continue; 
            if (visited[nextY][nextX]) continue; 
            if (arr[nextY][nextX] == 1) continue; 
            if (arr[nextY][nextX] == 0) 
            { 
                ++answerCount; 
            } 
            if (answerCount == targetCount) 
            { 
                return curTurn + 1; 
            } 
            visited[nextY][nextX] = true; 
            q.push({ nextY,nextX, curTurn + 1 }); 
        } 
    } 
    return 0x3f3f3f3f; 
} 
vector<int> answers; 
void DFS(int index, int count) 
{ 
    if (count == m) 
    { 
        answers.push_back(BFS()); 
    } 
    for (int i = index + 1; i < viruses.size(); ++i) 
    { 
        virusIndexes.push_back(i); 
        DFS(i, count + 1); 
        virusIndexes.pop_back(); 
    } 
} 
int main() 
{ 
    ios::sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL); 
    cin >> n >> m; 
    for (int i = 0; i < n; ++i) 
    { 
        for (int j = 0; j < n; ++j) 
        { 
            cin >> arr[i][j]; 
            if (arr[i][j] == 0) 
            { 
                ++targetCount; 
            } 
            if (arr[i][j] == 2) 
            { 
                viruses.push_back({ i,j }); 
            } 
        } 
    } 
    if (targetCount == 0) 
    { 
        cout << 0; 
        return 0; 
    } 
    DFS(-1, 0); 
    sort(answers.begin(), answers.end()); 
    if (answers[0] == 0x3f3f3f3f) cout << -1; 
    else cout << answers[0]; 
} 
 | 
cs | 
브루트포스 + 시뮬레이션 문제라고 할 수 있다.
전부 바이러스로 감염시키는데 필요한 최소시간을 구해야하는데, 바이러스 위치를 전부 뽑아줘야된다.
비활성화 바이러스개수중에서 m개만큼을 뽑고, 그걸로 BFS를 활용해서 시뮬레이션하면 된다.
input값은 작긴한데, 그렇다고 왠지 매 BFS마다 tempArr에다가 arr을 복사하는식으로 하면 시간제한걸릴 것 같아서 그렇게 안하도록 작성했다.
빈공간 없이 벽만 있거나, 비활성화바이러스 ( 값 2로 주어지는것)만 있다면, 퍼뜨릴필요가 없으니 0을 출력해야한다는것만 인지하자.
'Algorithm > Baekjoon' 카테고리의 다른 글
| [Algorithm] Baekjoon 15683번 : 감시 (1) | 2023.12.28 | 
|---|---|
| [Algorithm] Baekjoon 1021번 : 회전하는 큐 (0) | 2023.12.27 | 
| [Algorithm]Baekjoon 2660번 : 회장 뽑기 (1) | 2023.12.24 | 
| [Algorithm]Baekjoon 11060번 : 점프 점프 (0) | 2023.12.15 | 
| [Algorithm]Baekjoon 1495번 : 기타리스트 (0) | 2023.12.14 | 
          