A 문제
주어진 수 만큼 "oox" 패턴을 반복 출력하고, 나머지에 따라 추가 문자를 붙인다.
구현 코드
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int full_cycles = n / 3;
for (int i = 0; i < full_cycles; ++i) {
cout << "oox";
}
int remainder = n % 3;
if (remainder == 1) cout << "o";
else if (remainder == 2) cout << "oo";
return 0;
}
B 문제
각 점에서 다른 모든 점까지의 유클리드 거리를 계산한 후, 가장 먼 점을 찾는다.
구현 코드
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
double dist[MAX_N][MAX_N];
int x[MAX_N], y[MAX_N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> x[i] >> y[i];
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < i; ++j) {
double dx = x[i] - x[j];
double dy = y[i] - y[j];
dist[i][j] = dist[j][i] = sqrt(dx * dx + dy * dy);
}
}
for (int i = 1; i <= n; ++i) {
double max_dist = 0;
for (int j = 1; j <= n; ++j) {
if (i != j) max_dist = max(max_dist, dist[i][j]);
}
for (int j = 1; j <= n; ++j) {
if (abs(dist[i][j] - max_dist) < 1e-9 && i != j) {
cout << j << "\n";
break;
}
}
}
return 0;
}
C 문제
색상별 최솟값을 관리하면서 전체 최댓값을 구한다. 이미 등장한 색상은 기존 위치에서 최솟값을 갱신한다.
구현 코드
#include <bits/stdc++.h>
using namespace std;
map<int, int> color_min;
map<int, bool> seen;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
int value, color;
cin >> value >> color;
if (seen[color]) {
color_min[color] = min(color_min[color], value);
} else {
color_min[color] = value;
seen[color] = true;
}
}
int result = 0;
for (auto& pair : color_min) {
result = max(result, pair.second);
}
cout << result;
return 0;
}
D 문제
에너지가 있는 지점만 방문 가능한 BFS 탐색을 수행한다. 각 칸의 에너지를 업데이트하며 도착점에 도달할 수 있는지 확인한다.
구현 코드
#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 205;
int grid[MAX_SIZE][MAX_SIZE]; // 0: 이동 가능, 1: 벽
int energy[MAX_SIZE][MAX_SIZE]; // 각 지점의 에너지
int max_energy_reached[MAX_SIZE][MAX_SIZE]; // 해당 지점까지 가져온 최대 에너지
int dx[] = {0, 1, -1, 0, 0};
int dy[] = {0, 0, 0, 1, -1};
struct Point {
int x, y;
};
int main() {
int height, width;
cin >> height >> width;
int start_x, start_y, target_x, target_y;
for (int i = 1; i <= height; ++i) {
for (int j = 1; j <= width; ++j) {
char cell;
cin >> cell;
if (cell == '.') grid[i][j] = 0;
else if (cell == '#') grid[i][j] = 1;
else if (cell == 'S') {
grid[i][j] = 0;
start_x = i;
start_y = j;
} else if (cell == 'T') {
grid[i][j] = 0;
target_x = i;
target_y = j;
}
}
}
int portal_count;
cin >> portal_count;
for (int i = 0; i < portal_count; ++i) {
int row, col, power;
cin >> row >> col >> power;
energy[row][col] = power;
}
if (energy[start_x][start_y] == 0) {
cout << "No\n";
return 0;
}
queue<Point> bfs_queue;
bfs_queue.push({start_x, start_y});
max_energy_reached[start_x][start_y] = energy[start_x][start_y];
while (!bfs_queue.empty()) {
Point current = bfs_queue.front();
bfs_queue.pop();
for (int dir = 1; dir <= 4; ++dir) {
int next_x = current.x + dx[dir];
int next_y = current.y + dy[dir];
if (next_x <= 0 || next_y <= 0 || next_x > height || next_y > width)
continue;
if (grid[next_x][next_y] == 1)
continue;
int updated_energy = max(max_energy_reached[current.x][current.y] - 1, energy[next_x][next_y]);
if (updated_energy > max_energy_reached[next_x][next_y]) {
max_energy_reached[next_x][next_y] = updated_energy;
bfs_queue.push({next_x, next_y});
if (next_x == target_x && next_y == target_y) {
cout << "Yes\n";
return 0;
}
}
}
}
cout << "No\n";
return 0;
}