Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add BOJ_16236.cpp #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
108 changes: 108 additions & 0 deletions BOJ_16236.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <tuple>

using namespace std;

struct Shark
{
int x, y, size, eaten;
};

int N;
vector<vector<int>> 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<vector<bool>> visited(N, vector<bool>(N, false));
queue<tuple<int, int, int>> q;
q.push({shark.x, shark.y, 0});
visited[shark.x][shark.y] = true;

vector<tuple<int, int, int>> 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<int>(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;
}
68 changes: 68 additions & 0 deletions BOJ_20040.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
#include <vector>
using namespace std;

int findParent(vector<int> &parent, int x)
{
if (parent[x] != x)
{
parent[x] = findParent(parent, parent[x]);
}
return parent[x];
}

bool unionNodes(vector<int> &parent, vector<int> &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<int> parent(n);
vector<int> 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;
}
Empty file added week13/.gitkeep
Empty file.
33 changes: 33 additions & 0 deletions 프로그래머스_네트워크.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <string>
#include <vector>

using namespace std;

void dfs(int node, vector<vector<int>> &computers, vector<bool> &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<vector<int>> computers)
{
vector<bool> visited(n, false);
int networkCount = 0;

for (int i = 0; i < n; i++)
{
if (!visited[i])
{
dfs(i, computers, visited);
networkCount++;
}
}

return networkCount;
}