diff --git a/BOJ_16236.cpp b/BOJ_16236.cpp new file mode 100644 index 0000000..79e84f2 --- /dev/null +++ b/BOJ_16236.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct Shark +{ + int x, y, size, eaten; +}; + +int N; +vector> grid; +Shark shark; + +const int dx[] = {-1, 1, 0, 0}; +const int dy[] = {0, 0, -1, 1}; + +bool is_in_bounds(int x, int y) +{ + return x >= 0 && x < N && y >= 0 && y < N; +} + +int bfs() +{ + vector> visited(N, vector(N, false)); + queue> q; + q.push({shark.x, shark.y, 0}); + visited[shark.x][shark.y] = true; + + vector> fish_candidates; + + while (!q.empty()) + { + int x, y, dist; + tie(x, y, dist) = q.front(); + q.pop(); + + for (int i = 0; i < 4; ++i) + { + int nx = x + dx[i], ny = y + dy[i]; + + if (is_in_bounds(nx, ny) && !visited[nx][ny] && grid[nx][ny] <= shark.size) + { + visited[nx][ny] = true; + q.push({nx, ny, dist + 1}); + + if (grid[nx][ny] > 0 && grid[nx][ny] < shark.size) + { + fish_candidates.push_back({dist + 1, nx, ny}); + } + } + } + } + + if (fish_candidates.empty()) + return 0; + + sort(fish_candidates.begin(), fish_candidates.end()); + int dist, fish_x, fish_y; + tie(dist, fish_x, fish_y) = fish_candidates[0]; + + shark.x = fish_x; + shark.y = fish_y; + shark.eaten++; + if (shark.eaten == shark.size) + { + shark.size++; + shark.eaten = 0; + } + + grid[fish_x][fish_y] = 0; + + return dist; +} + +int main() +{ + cin >> N; + grid.resize(N, vector(N)); + + for (int i = 0; i < N; ++i) + { + for (int j = 0; j < N; ++j) + { + cin >> grid[i][j]; + if (grid[i][j] == 9) + { + shark = {i, j, 2, 0}; + grid[i][j] = 0; + } + } + } + + int total_time = 0; + while (true) + { + int time_spent = bfs(); + if (time_spent == 0) + break; + total_time += time_spent; + } + + cout << total_time << endl; + return 0; +} \ No newline at end of file diff --git a/BOJ_20040.cpp b/BOJ_20040.cpp new file mode 100644 index 0000000..928b7de --- /dev/null +++ b/BOJ_20040.cpp @@ -0,0 +1,68 @@ +#include +#include +using namespace std; + +int findParent(vector &parent, int x) +{ + if (parent[x] != x) + { + parent[x] = findParent(parent, parent[x]); + } + return parent[x]; +} + +bool unionNodes(vector &parent, vector &rank, int a, int b) +{ + int rootA = findParent(parent, a); + int rootB = findParent(parent, b); + + if (rootA == rootB) + return true; + + if (rank[rootA] > rank[rootB]) + { + parent[rootB] = rootA; + } + else if (rank[rootA] < rank[rootB]) + { + parent[rootA] = rootB; + } + else + { + parent[rootB] = rootA; + rank[rootA]++; + } + return false; +} + +int main() +{ + int n, m; + cin >> n >> m; + + vector parent(n); + vector rank(n, 0); + + // 초기화 + for (int i = 0; i < n; i++) + { + parent[i] = i; + } + + int result = 0; + + for (int i = 1; i <= m; i++) + { + int a, b; + cin >> a >> b; + + if (unionNodes(parent, rank, a, b)) + { + result = i; // 사이클이 처음으로 발생한 차례 + break; + } + } + + cout << result << endl; + return 0; +} diff --git a/week13/.gitkeep b/week13/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git "a/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_\353\204\244\355\212\270\354\233\214\355\201\254.cpp" "b/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_\353\204\244\355\212\270\354\233\214\355\201\254.cpp" new file mode 100644 index 0000000..c7a0a6f --- /dev/null +++ "b/\355\224\204\353\241\234\352\267\270\353\236\230\353\250\270\354\212\244_\353\204\244\355\212\270\354\233\214\355\201\254.cpp" @@ -0,0 +1,33 @@ +#include +#include + +using namespace std; + +void dfs(int node, vector> &computers, vector &visited) +{ + visited[node] = true; + for (int i = 0; i < computers.size(); i++) + { + if (computers[node][i] == 1 && !visited[i]) + { + dfs(i, computers, visited); + } + } +} + +int solution(int n, vector> computers) +{ + vector visited(n, false); + int networkCount = 0; + + for (int i = 0; i < n; i++) + { + if (!visited[i]) + { + dfs(i, computers, visited); + networkCount++; + } + } + + return networkCount; +} \ No newline at end of file