Skip to content

Commit 84269fc

Browse files
committed
Update
1 parent afe740e commit 84269fc

File tree

9 files changed

+662
-0
lines changed

9 files changed

+662
-0
lines changed

12/7.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

13/1.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// 도시의 개수, 도로의 개수, 거리 정보, 출발 도시 번호
6+
int n, m, k, x;
7+
vector<int> graph[300001];
8+
// 모든 도시에 대한 최단 거리 초기화
9+
vector<int> d(300001, -1);
10+
11+
int main(void) {
12+
cin >> n >> m >> k >> x;
13+
14+
// 모든 도로 정보 입력 받기
15+
for (int i = 0; i < m; i++) {
16+
int a, b;
17+
cin >> a >> b;
18+
graph[a].push_back(b);
19+
}
20+
21+
// 출발 도시까지의 거리는 0으로 설정
22+
d[x] = 0;
23+
24+
// 너비 우선 탐색(BFS) 수행
25+
queue<int> q;
26+
q.push(x);
27+
while (!q.empty()) {
28+
int now = q.front();
29+
q.pop();
30+
// 현재 도시에서 이동할 수 있는 모든 도시를 확인
31+
for (int i = 0; i < graph[now].size(); i++) {
32+
int nextNode = graph[now][i];
33+
// 아직 방문하지 않은 도시라면
34+
if (d[nextNode] == -1) {
35+
// 최단 거리 갱신
36+
d[nextNode] = d[now] + 1;
37+
q.push(nextNode);
38+
}
39+
}
40+
}
41+
42+
// 최단 거리가 K인 모든 도시의 번호를 오름차순으로 출력
43+
bool check = false;
44+
for (int i = 1; i <= n; i++) {
45+
if (d[i] == k) {
46+
cout << i << '\n';
47+
check = true;
48+
}
49+
}
50+
51+
// 만약 최단 거리가 K인 도시가 없다면, -1 출력
52+
if (!check) cout << -1 << '\n';
53+
}

13/2.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, m;
6+
int arr[8][8]; // 초기 맵 배열
7+
int temp[8][8]; // 벽을 설치한 뒤의 맵 배열
8+
9+
// 4가지 이동 방향에 대한 배열
10+
int dx[] = {-1, 0, 1, 0};
11+
int dy[] = {0, 1, 0, -1};
12+
13+
int result;
14+
15+
// 깊이 우선 탐색(DFS)을 이용해 각 바이러스가 사방으로 퍼지도록 하기
16+
void virus(int x, int y) {
17+
for (int i = 0; i < 4; i++) {
18+
int nx = x + dx[i];
19+
int ny = y + dy[i];
20+
// 상, 하, 좌, 우 중에서 바이러스가 퍼질 수 있는 경우
21+
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
22+
if (temp[nx][ny] == 0) {
23+
// 해당 위치에 바이러스 배치하고, 다시 재귀적으로 수행
24+
temp[nx][ny] = 2;
25+
virus(nx, ny);
26+
}
27+
}
28+
}
29+
}
30+
31+
// 현재 맵에서 안전 영역의 크기 계산하는 메서드
32+
int getScore() {
33+
int score = 0;
34+
for (int i = 0; i < n; i++) {
35+
for (int j = 0; j < m; j++) {
36+
if (temp[i][j] == 0) {
37+
score += 1;
38+
}
39+
}
40+
}
41+
return score;
42+
}
43+
44+
// 깊이 우선 탐색(DFS)을 이용해 울타리를 설치하면서, 매 번 안전 영역의 크기 계산
45+
void dfs(int count) {
46+
// 울타리가 3개 설치된 경우
47+
if (count == 3) {
48+
for (int i = 0; i < n; i++) {
49+
for (int j = 0; j < m; j++) {
50+
temp[i][j] = arr[i][j];
51+
}
52+
}
53+
// 각 바이러스의 위치에서 전파 진행
54+
for (int i = 0; i < n; i++) {
55+
for (int j = 0; j < m; j++) {
56+
if (temp[i][j] == 2) {
57+
virus(i, j);
58+
}
59+
}
60+
}
61+
// 안전 영역의 최대값 계산
62+
result = max(result, getScore());
63+
return;
64+
}
65+
// 빈 공간에 울타리를 설치
66+
for (int i = 0; i < n; i++) {
67+
for (int j = 0; j < m; j++) {
68+
if (arr[i][j] == 0) {
69+
arr[i][j] = 1;
70+
count += 1;
71+
dfs(count);
72+
arr[i][j] = 0;
73+
count -= 1;
74+
}
75+
}
76+
}
77+
}
78+
79+
int main(void) {
80+
cin >> n >> m;
81+
82+
for (int i = 0; i < n; i++) {
83+
for (int j = 0; j < m; j++) {
84+
cin >> arr[i][j];
85+
}
86+
}
87+
88+
dfs(0);
89+
cout << result << '\n';
90+
}

13/3.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Virus {
6+
public:
7+
int index;
8+
int second;
9+
int x;
10+
int y;
11+
Virus(int index, int second, int x, int y) {
12+
this->index = index;
13+
this->second = second;
14+
this->x = x;
15+
this->y = y;
16+
}
17+
// 정렬 기준은 '번호가 낮은 순서'
18+
bool operator <(Virus &other) {
19+
return this->index < other.index;
20+
}
21+
};
22+
23+
int n, k;
24+
// 전체 보드 정보를 담는 배열
25+
int graph[200][200];
26+
// 바이러스에 대한 정보를 담는 리스트
27+
vector<Virus> viruses;
28+
29+
// 바이러스가 퍼져나갈 수 있는 4가지의 위치
30+
int dx[] = {-1, 0, 1, 0};
31+
int dy[] = {0, 1, 0, -1};
32+
33+
int main(void) {
34+
cin >> n >> k;
35+
36+
// 보드 정보를 한 줄 단위로 입력
37+
for (int i = 0; i < n; i++) {
38+
for (int j = 0; j < n; j++) {
39+
cin >> graph[i][j];
40+
// 해당 위치에 바이러스가 존재하는 경우
41+
if (graph[i][j] != 0) {
42+
// (바이러스 종류, 시간, 위치 X, 위치 Y) 삽입
43+
viruses.push_back(Virus(graph[i][j], 0, i, j));
44+
}
45+
}
46+
}
47+
48+
// 정렬 이후에 큐로 옮기기 (낮은 번호의 바이러스가 먼저 증식하므로)
49+
sort(viruses.begin(), viruses.end());
50+
queue<Virus> q;
51+
for (int i = 0; i < viruses.size(); i++) {
52+
q.push(viruses[i]);
53+
}
54+
55+
int target_s, target_x, target_y;
56+
cin >> target_s >> target_x >> target_y;
57+
58+
// 너비 우선 탐색(BFS) 진행
59+
while (!q.empty()) {
60+
Virus virus = q.front();
61+
q.pop();
62+
// 정확히 second만큼 초가 지나거나, 큐가 빌 때까지 반복
63+
if (virus.second == target_s) break;
64+
// 현재 노드에서 주변 4가지 위치를 각각 확인
65+
for (int i = 0; i < 4; i++) {
66+
int nx = virus.x + dx[i];
67+
int ny = virus.y + dy[i];
68+
// 해당 위치로 이동할 수 있는 경우
69+
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
70+
// 아직 방문하지 않은 위치라면, 그 위치에 바이러스 넣기
71+
if (graph[nx][ny] == 0) {
72+
graph[nx][ny] = virus.index;
73+
q.push(Virus(virus.index, virus.second + 1, nx, ny));
74+
}
75+
}
76+
}
77+
}
78+
79+
cout << graph[target_x - 1][target_y - 1] << '\n';
80+
}

13/4.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// "균형잡힌 괄호 문자열"의 인덱스 반환
6+
int balancedIndex(string p) {
7+
int count = 0; // 왼쪽 괄호의 개수
8+
for (int i = 0; i < p.size(); i++) {
9+
if (p[i] == '(') count += 1;
10+
else count -= 1;
11+
if (count == 0) return i;
12+
}
13+
return -1;
14+
}
15+
16+
// "올바른 괄호 문자열"인지 판단
17+
bool checkProper(string p) {
18+
int count = 0; // 왼쪽 괄호의 개수
19+
for (int i = 0; i < p.size(); i++) {
20+
if (p[i] == '(') count += 1;
21+
else {
22+
if (count == 0) { // 쌍이 맞지 않는 경우에 false 반환
23+
return false;
24+
}
25+
count -= 1;
26+
}
27+
}
28+
return true; // 쌍이 맞는 경우에 true 반환
29+
}
30+
31+
string solution(string p) {
32+
string answer = "";
33+
if (p == "") return answer;
34+
int index = balancedIndex(p);
35+
string u = p.substr(0, index + 1);
36+
string v = p.substr(index + 1);
37+
// "올바른 괄호 문자열"이면, v에 대해 함수를 수행한 결과를 붙여 반환
38+
if (checkProper(u)) {
39+
answer = u + solution(v);
40+
}
41+
// "올바른 괄호 문자열"이 아니라면 아래의 과정을 수행
42+
else {
43+
answer = "(";
44+
answer += solution(v);
45+
answer += ")";
46+
u = u.substr(1, u.size() - 2); // 첫 번째와 마지막 문자를 제거
47+
for (int i = 0; i < u.size(); i++) {
48+
if (u[i] == '(') u[i] = ')';
49+
else u[i] = '(';
50+
}
51+
answer += u;
52+
}
53+
return answer;
54+
}

13/5.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n;
6+
// 연산을 수행하고자 하는 수 리스트
7+
vector<int> arr;
8+
// 더하기, 빼기, 곱하기, 나누기 연산자 개수
9+
int add, sub, mul, divi;
10+
11+
// 최솟값과 최댓값 초기화
12+
int minValue = 1e9;
13+
int maxValue = -1e9;
14+
15+
// 깊이 우선 탐색 (DFS) 메서드
16+
void dfs(int i, int now) {
17+
// 모든 연산자를 다 사용한 경우, 최솟값과 최댓값 업데이트
18+
if (i == n) {
19+
minValue = min(minValue, now);
20+
maxValue = max(maxValue, now);
21+
}
22+
else {
23+
// 각 연산자에 대하여 재귀적으로 수행
24+
if (add > 0) {
25+
add -= 1;
26+
dfs(i + 1, now + arr[i]);
27+
add += 1;
28+
}
29+
if (sub > 0) {
30+
sub -= 1;
31+
dfs(i + 1, now - arr[i]);
32+
sub += 1;
33+
}
34+
if (mul > 0) {
35+
mul -= 1;
36+
dfs(i + 1, now * arr[i]);
37+
mul += 1;
38+
}
39+
if (divi > 0) {
40+
divi -= 1;
41+
dfs(i + 1, now / arr[i]);
42+
divi += 1;
43+
}
44+
}
45+
}
46+
47+
int main(void) {
48+
cin >> n;
49+
for (int i = 0; i < n; i++) {
50+
int x;
51+
cin >> x;
52+
arr.push_back(x);
53+
}
54+
cin >> add >> sub >> mul >> divi;
55+
56+
// DFS 메서드 호출
57+
dfs(1, arr[0]);
58+
59+
// 최댓값과 최솟값 차례대로 출력
60+
cout << maxValue << '\n' << minValue << '\n';
61+
}

0 commit comments

Comments
 (0)