-
Notifications
You must be signed in to change notification settings - Fork 4
/
Lv2_거리두기확인하기.cpp
86 lines (70 loc) · 2.04 KB
/
Lv2_거리두기확인하기.cpp
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
#include <string>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
struct node {
int y;
int x;
int count;
};
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
bool check(int y, int x, int i, vector<vector<string>> &places) {
queue<node> q;
q.push({ y,x,0 });
vector<vector<bool>> visited(5, vector<bool>(5, false));
visited[y][x] = true;
while (!q.empty()) {
int y = q.front().y;
int x = q.front().x;
int count = q.front().count;
q.pop();
if (count == 2)
continue;
for (int k = 0; k < 4; ++k) {
int ny = y + dy[k];
int nx = x + dx[k];
if (ny < 0 || ny >4 || nx < 0 || nx>4 || visited[ny][nx])
continue;
if (places[i][ny][nx] == 'X')
continue;
if (places[i][ny][nx] == 'P' && count <= 1)
return false;
visited[ny][nx] = true;
q.push({ ny,nx,count + 1 });
}
}
return true;
}
vector<int> solution(vector<vector<string>> places) {
vector<int> answer;
for (int i = 0; i < 5; ++i) {
bool flag = false;
for (int j = 0; j < 5; ++j) {
flag = false;
for (int k = 0; k < 5; ++k) {
if (places[i][j][k] == 'P') {
if (!check(j, k, i,places)) {
flag = true;
break;
}
}
}
if (flag)
break;
}
if (flag)
answer.push_back(0);
else
answer.push_back(1);
}
return answer;
}
int main() {
vector<vector<string>> places = {{"POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"}, {"POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"}, {"PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"}, {"OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"}, {"PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"}};
vector<int> ans = solution(places);
for (auto a : ans)
cout << a << " ";
return 0;
}