Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions yumin/25.02.23/13901.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
구현이 싫어요
*/

# include <iostream>
# include <vector>

using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);

int r, c;
cin >> r >> c;

vector<vector<bool>> obstacle(r, vector<bool>(c, false));
vector<vector<bool>> visited(r, vector<bool>(c, false));

int k;
cin >> k;

for (int i = 0; i < k; i++) {
int br, bc;
cin >> br >> bc;
obstacle[br][bc] = true;
}

int sr, sc;
cin >> sr >> sc;

vector<int> order(4);
for (int i = 0; i < 4; i++) {
cin >> order[i];
}

vector<pair<int, int>> deltas(5);
deltas[1] = make_pair(-1, 0);
deltas[2] = make_pair(1, 0);
deltas[3] = make_pair(0, -1);
deltas[4] = make_pair(0, 1);

int curIndex = 0;
int direction = order[curIndex];

int curR = sr, curC = sc;
visited[curR][curC] = true;

// 로봇이 움직일 수 있는 동안 반복 ㄱㄱ
while (true) {
// 한 칸 이동할 다음 위치도 계산해주자.
int nextR = curR + deltas[direction].first;
int nextC = curC + deltas[direction].second;

// 다음 위치가 방의 범위 내에 있고, 장애물도 없고, 아직 방문하지 않은 곳이라면 로봇이 다음 위치로 이동 쓩
if (nextR >= 0 && nextR < r && nextC >= 0 && nextC < c && !obstacle[nextR][nextC] && !visited[nextR][nextC]) {
curR = nextR;
curC = nextC;
visited[curR][curC] = true;
continue;
}

// 현재 방향으로 더 이상 이동할 수 없으므로, 지정된 다음 방향을 찾아보자.
else {
bool moved = false; // 새로운 방향으로 이동했는지 여부를 따지는 변수

// 새로운 방향과 위치 변수..
for (int i = 1; i <= 4; i++) {
int newIndex = (curIndex + i) % 4;
int newDirection = order[newIndex];
nextR = curR + deltas[newDirection].first;
nextC = curC + deltas[newDirection].second;

// 해당 위치가 로봇이 갈 수 있는 곳이라면 또 ㄱㄱ
if (nextR >= 0 && nextR < r && nextC >= 0 && nextC < c && !obstacle[nextR][nextC] && !visited[nextR][nextC]) {
curIndex = newIndex;
direction = newDirection;
curR = nextR;
curC = nextC;
visited[curR][curC] = true;
moved = true;
break;
}
}

// 4가지 방향으로 모두 이동할 수 없다면 반복 종료.
if (!moved)
break;
}
}

cout << curR << " " << curC << "\n";
return 0;
}
55 changes: 55 additions & 0 deletions yumin/25.02.23/14496.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
BFS 문제, 늘 먹던 맛 익숙한 맛.
*/

# include <iostream>
# include <vector>
# include <queue>

using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);

int start, target;
cin >> start >> target;

int n, m;
cin >> n >> m;

vector<vector<int>> graph(n + 1);

for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v); // u와 치환 가능한 문자 리스트에 v 추가
graph[v].push_back(u); // v와 치환 가능한 문자 리스트에 u 추가
}

vector<int> distance(n + 1, -1); // 각 문자까지의 치환 횟수를 저장할 벡터
queue<int> q; // BFS 할라고 만든 큐

distance[start] = 0;
q.push(start);

while(!q.empty()) {
int current = q.front(); // 큐의 맨 앞에 있는 문자를 선택해주자.
q.pop(); // 그리고 선택한 문자를 큐에서 제거한다.

if (current == target) { // 목표 문자에 도달할 경우 탐색을 종료한다.
break;
}

// 현재 문자와 치환 가능한 모든 문자를 탐색해주자.
for (int next : graph[current]) {
if (distance[next] == -1) { // 아직 방문하지 않은 문자라면,
distance[next] = distance[current] + 1; // 다음 문자까지의 치환 횟수는 현재 문자까지의 치환 횟수 + 1.
q.push(next); // 다음 문자를 큐에 삽입하고 탐색 ㄱㄱ
}
}
}

cout << distance[target] << "\n";
return 0;
}
44 changes: 44 additions & 0 deletions yumin/25.02.23/16953.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
그리디로 풀었는데 문제 알고리즘 분류를 까보니 그래프..?
*/

# include <iostream>

using namespace std;

int main() {
int a, b;
cin >> a >> b;

int cnt = 0;

while (b > a) {
/*
b의 가장 오른쪽 자리가 1인 경우, 오른쪽의 1을 제거하기 위해 10으로 나눠주고 연산 횟수를 1만큼 증가시켜주자.
*/
if (b % 10 == 1) {
b /= 10;
cnt++;
}

/*
b가 짝수인 경우, 2로 나누어주고 연산 횟수를 1만큼 증가시켜주자.
*/
else if (b % 2 == 0) {
b /= 2;
cnt++;
}

// 위 두 개의 조건을 모두 만족하지 않는 경우에는 더 이상 변환할 수 없으니까 반복 종료.
else {
break;
}
}

if (b == a)
cout << cnt + 1 << "\n";
else
cout << -1 << "\n";

return 0;
}