Skip to content

Commit 01e82b2

Browse files
committed
Update
1 parent c99521b commit 01e82b2

36 files changed

+580
-0
lines changed

12/1.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
string str;
6+
int summary = 0;
7+
8+
int main(void) {
9+
cin >> str;
10+
11+
// 왼쪽 부분의 자릿수의 합 더하기
12+
for (int i = 0; i < str.size() / 2; i++) {
13+
summary += str[i] - '0';
14+
}
15+
16+
// 오른쪽 부분의 자릿수의 합 빼기
17+
for (int i = str.size() / 2; i < str.size(); i++) {
18+
summary -= str[i] - '0';
19+
}
20+
21+
// 왼쪽 부분과 오른쪽 부분의 자릿수 합이 동일한지 검사
22+
if (summary == 0) cout << "LUCKY" << '\n';
23+
else cout << "READY" << '\n';
24+
}

12/2.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
string str;
6+
vector<char> result;
7+
int value = 0;
8+
9+
int main(void) {
10+
cin >> str;
11+
12+
// 문자를 하나씩 확인하며
13+
for (int i = 0; i < str.size(); i++) {
14+
// 알파벳인 경우 결과 리스트에 삽입
15+
if (isalpha(str[i])) {
16+
result.push_back(str[i]);
17+
}
18+
// 숫자는 따로 더하기
19+
else {
20+
value += str[i] - '0';
21+
}
22+
}
23+
24+
// 알파벳을 오름차순으로 정렬
25+
sort(result.begin(), result.end());
26+
27+
// 알파벳을 차례대로 출력
28+
for (int i = 0; i < result.size(); i++) {
29+
cout << result[i];
30+
}
31+
32+
// 숫자가 하나라도 존재하는 경우 가장 뒤에 출력
33+
if (value != 0) cout << value;
34+
35+
cout << '\n';
36+
}

12/3.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int solution(string s) {
6+
int answer = s.size();
7+
// 1개 단위(step)부터 압축 단위를 늘려가며 확인
8+
for (int step = 1; step < s.size() / 2 + 1; step++) {
9+
string compressed = "";
10+
string prev = s.substr(0, step); // 앞에서부터 step만큼의 문자열 추출
11+
int cnt = 1;
12+
// 단위(step) 크기만큼 증가시키며 이전 문자열과 비교
13+
for (int j = step; j < s.size(); j += step) {
14+
// 이전 상태와 동일하다면 압축 횟수(count) 증가
15+
if (prev == s.substr(j, step)) cnt += 1;
16+
// 다른 문자열이 나왔다면(더 이상 압축하지 못하는 경우라면)
17+
else {
18+
compressed += (cnt >= 2)? to_string(cnt) + prev : prev;
19+
prev += s.substr(j, step); // 다시 상태 초기화
20+
cnt = 1;
21+
}
22+
}
23+
// 남아있는 문자열에 대해서 처리
24+
compressed += (cnt >= 2)? to_string(cnt) + prev : prev;
25+
// 만들어지는 압축 문자열이 가장 짧은 것이 정답
26+
answer = min(answer, (int)compressed.size());
27+
}
28+
return answer;
29+
}

12/4.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// 2차원 리스트 90도 회전하기
6+
vector<vector<int> > rotateMatrixBy90Degree(vector<vector<int> > a) {
7+
int n = a.size(); // 행 길이 계산
8+
int m = a[0].size(); // 열 길이 계산
9+
vector<vector<int> > result(n, vector<int>(m)); // 결과 리스트
10+
for (int i = 0; i < n; i++) {
11+
for (int j = 0; j < m; j++) {
12+
result[j][n - i - 1] = a[i][j];
13+
}
14+
}
15+
return result;
16+
}
17+
18+
// 자물쇠의 중간 부분이 모두 1인지 확인
19+
bool check(vector<vector<int> > newLock) {
20+
int lockLength = newLock.size() / 3;
21+
for (int i = lockLength; i < lockLength * 2; i++) {
22+
for (int j = lockLength; j < lockLength * 2; j++) {
23+
if (newLock[i][j] != 1) {
24+
return false;
25+
}
26+
}
27+
}
28+
return true;
29+
}
30+
31+
bool solution(vector<vector<int> > key, vector<vector<int> > lock) {
32+
int n = lock.size();
33+
int m = key.size();
34+
// 자물쇠의 크기를 기존의 3배로 변환
35+
vector<vector<int> > newLock(n * 3, vector<int>(n * 3));
36+
// 새로운 자물쇠의 중앙 부분에 기존의 자물쇠 넣기
37+
for (int i = 0; i < n; i++) {
38+
for (int j = 0; j < n; j++) {
39+
newLock[i + n][j + n] = lock[i][j];
40+
}
41+
}
42+
43+
// 4가지 방향에 대해서 확인
44+
for (int rotation = 0; rotation < 4; rotation++) {
45+
key = rotateMatrixBy90Degree(key); // 열쇠 회전
46+
for (int x = 0; x < n * 2; x++) {
47+
for (int y = 0; y < n * 2; y++) {
48+
// 자물쇠에 열쇠를 끼워 넣기
49+
for (int i = 0; i < m; i++) {
50+
for (int j = 0; j < m; j++) {
51+
newLock[x + i][y + j] += key[i][j];
52+
}
53+
}
54+
// 새로운 자물쇠에 열쇠가 정확히 들어 맞는지 검사
55+
if (check(newLock)) return true;
56+
// 자물쇠에서 열쇠를 다시 빼기
57+
for (int i = 0; i < m; i++) {
58+
for (int j = 0; j < m; j++) {
59+
newLock[x + i][y + j] -= key[i][j];
60+
}
61+
}
62+
}
63+
}
64+
}
65+
return false;
66+
}

12/5.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int n, k, l;
6+
int arr[101][101]; // 맵 정보
7+
vector<pair<int, char> > info; // 방향 회전 정보
8+
9+
// 처음에는 오른쪽을 보고 있으므로(동, 남, 서, 북)
10+
int dx[] = {0, 1, 0, -1};
11+
int dy[] = {1, 0, -1, 0};
12+
13+
int turn(int direction, char c) {
14+
if (c == 'L') direction = (direction == 0)? 3 : direction - 1;
15+
else direction = (direction + 1) % 4;
16+
return direction;
17+
}
18+
19+
int simulate() {
20+
int x = 1, y = 1; // 뱀의 머리 위치
21+
arr[x][y] = 2; // 뱀이 존재하는 위치는 2로 표시
22+
int direction = 0; // 처음에는 동쪽을 보고 있음
23+
int time = 0; // 시작한 뒤에 지난 '초' 시간
24+
int index = 0; // 다음에 회전할 정보
25+
queue<pair<int, int> > q; // 뱀이 차지하고 있는 위치 정보(꼬리가 앞쪽)
26+
q.push({x, y});
27+
28+
while (true) {
29+
int nx = x + dx[direction];
30+
int ny = y + dy[direction];
31+
// 맵 범위 안에 있고, 뱀의 몸통이 없는 위치라면
32+
if (1 <= nx && nx <= n && 1 <= ny && ny <= n && arr[nx][ny] != 2) {
33+
// 사과가 없다면 이동 후에 꼬리 제거
34+
if (arr[nx][ny] == 0) {
35+
arr[nx][ny] = 2;
36+
q.push({nx, ny});
37+
int px = q.front().first;
38+
int py = q.front().second;
39+
q.pop();
40+
arr[px][py] = 0;
41+
}
42+
// 사과가 있다면 이동 후에 꼬리 그대로 두기
43+
if (arr[nx][ny] == 1) {
44+
arr[nx][ny] = 2;
45+
q.push({nx, ny});
46+
}
47+
}
48+
// 벽이나 뱀의 몸통과 부딪혔다면
49+
else {
50+
time += 1;
51+
break;
52+
}
53+
// 다음 위치로 머리를 이동
54+
x = nx;
55+
y = ny;
56+
time += 1;
57+
if (index < l && time == info[index].first) { // 회전할 시간인 경우 회전
58+
direction = turn(direction, info[index].second);
59+
index += 1;
60+
}
61+
}
62+
return time;
63+
}
64+
65+
int main(void) {
66+
cin >> n >> k;
67+
68+
// 맵 정보(사과 있는 곳은 1로 표시)
69+
for (int i = 0; i < k; i++) {
70+
int a, b;
71+
cin >> a >> b;
72+
arr[a][b] = 1;
73+
}
74+
75+
// 방향 회전 정보 입력
76+
cin >> l;
77+
for (int i = 0; i < l; i++) {
78+
int x;
79+
char c;
80+
cin >> x >> c;
81+
info.push_back({x, c});
82+
}
83+
84+
cout << simulate() << '\n';
85+
}

12/6.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// 현재 설치된 구조물이 '가능한' 구조물인지 확인하는 함수
6+
bool possible(vector<vector<int> > answer) {
7+
for (int i = 0; i < answer.size(); i++) {
8+
int x = answer[i][0];
9+
int y = answer[i][1];
10+
int stuff = answer[i][2];
11+
if (stuff == 0) { // 설치된 것이 '기둥'인 경우
12+
bool check = false;
13+
// '바닥 위'라면 정상
14+
if (y == 0) check = true;
15+
// '보의 한 쪽 끝 부분 위' 혹은 '다른 기둥 위'라면 정상
16+
for (int j = 0; j < answer.size(); j++) {
17+
if (x - 1 == answer[j][0] && y == answer[j][1] && 1 == answer[j][2]) {
18+
check = true;
19+
}
20+
if (x == answer[j][0] && y == answer[j][1] && 1 == answer[j][2]) {
21+
check = true;
22+
}
23+
if (x == answer[j][0] && y - 1 == answer[j][1] && 0 == answer[j][2]) {
24+
check = true;
25+
}
26+
}
27+
if (!check) return false; // 아니라면 거짓(False) 반환
28+
}
29+
else if (stuff == 1) { // 설치된 것이 '보'인 경우
30+
bool check = false;
31+
bool left = false;
32+
bool right = false;
33+
// '한쪽 끝부분이 기둥 위' 혹은 '양쪽 끝부분이 다른 보와 동시에 연결'이라면 정상
34+
for (int j = 0; j < answer.size(); j++) {
35+
if (x == answer[j][0] && y - 1 == answer[j][1] && 0 == answer[j][2]) {
36+
check = true;
37+
}
38+
if (x + 1 == answer[j][0] && y - 1 == answer[j][1] && 0 == answer[j][2]) {
39+
check = true;
40+
}
41+
if (x - 1 == answer[j][0] && y == answer[j][1] && 1 == answer[j][2]) {
42+
left = true;
43+
}
44+
if (x + 1 == answer[j][0] && y == answer[j][1] && 1 == answer[j][2]) {
45+
right = true;
46+
}
47+
}
48+
if (left && right) check = true;
49+
if (!check) return false; // 아니라면 거짓(False) 반환
50+
}
51+
}
52+
return true;
53+
}
54+
55+
vector<vector<int> > solution(int n, vector<vector<int> > build_frame) {
56+
vector<vector<int> > answer;
57+
// 작업(frame)의 개수는 최대 1,000개
58+
for (int i = 0; i < build_frame.size(); i++) {
59+
int x = build_frame[i][0];
60+
int y = build_frame[i][1];
61+
int stuff = build_frame[i][2];
62+
int operate = build_frame[i][3];
63+
if (operate == 0) { // 삭제하는 경우
64+
// 일단 삭제를 해 본 뒤에
65+
int index = 0;
66+
for (int j = 0; j < answer.size(); j++) {
67+
if (x == answer[j][0] && y == answer[j][1] && stuff == answer[j][2]) {
68+
index = j;
69+
}
70+
}
71+
vector<int> erased = answer[index];
72+
answer.erase(answer.begin() + index);
73+
if (!possible(answer)) { // 가능한 구조물인지 확인
74+
answer.push_back(erased); // 가능한 구조물이 아니라면 다시 설치
75+
}
76+
}
77+
if (operate == 1) { // 설치하는 경우
78+
// 일단 설치를 해 본 뒤에
79+
vector<int> inserted;
80+
inserted.push_back(x);
81+
inserted.push_back(y);
82+
inserted.push_back(stuff);
83+
answer.push_back(inserted);
84+
if (!possible(answer)) { // 가능한 구조물인지 확인
85+
answer.pop_back(); // 가능한 구조물이 아니라면 다시 제거
86+
}
87+
}
88+
}
89+
// 정렬된 결과를 반환
90+
sort(answer.begin(), answer.end());
91+
return answer;
92+
}

0 commit comments

Comments
 (0)