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

[트리] 1976244 유영민 #345

Open
wants to merge 3 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
45 changes: 45 additions & 0 deletions 08_DFS & BFS/도전/1844.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
typedef pair<int, int> pi;
//
int solution(vector<vector<int>> maps)
{
int answer = 0;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
int n = maps.size();
int m = maps[0].size();

// 시작점 0,0에서 BFS 실행
queue<pi> q;
q.push({0, 0});
while (!q.empty())
{
int r = q.front().first;
int c = q.front().second;
q.pop();

for (int k = 0; k < 4; k++)
{
int nr = r + dr[k];
int nc = c + dc[k];
if (nr < n && nr >= 0 && nc < m && nc >= 0 && maps[nr][nc] == 1)
{
maps[nr][nc] = maps[r][c] + 1;
q.push({nr, nc});
}
}
}

if (maps[n - 1][m - 1] == 1)
{
answer = -1;
}
else
{
answer = maps[n - 1][m - 1];
}
return answer;
}
93 changes: 93 additions & 0 deletions 08_DFS & BFS/도전/19538.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

// BFS
vector<int> tookMin(vector<int> &rumor_mongers, int n, vector<vector<int>> &nbr_graph)
{
int cnt = 0;
queue<int> gossip;
queue<int> influenced;

// 모두 처음에는 안 믿는다.
vector<int> believed(n + 1, -1);
vector<int> cnt_nbr_believers(n + 1, 0);

// 유포자를 큐에 넣는다 (0으로 셋팅)
for (int rm : rumor_mongers)
{
believed[rm] = 0;
gossip.push(rm);
}

// 루머 유포 시작
while (!gossip.empty())
{
cnt++;
int node = gossip.front();
gossip.pop();

for (int j = 0; j < nbr_graph[node].size(); j++)
{
int next_node = nbr_graph[node][j];
// 안 믿는 사람이지만, 여론에 따라 믿게 되는 경우
if (believed[next_node] == -1 && ++cnt_nbr_believers[next_node] >= (nbr_graph[next_node].size() + 1) / 2)
{
influenced.push(next_node);
gossip.push(next_node);
}
}

// node에 의해 영향 받은 사람들이 동시에 믿게 된다.
while (!influenced.empty())
{
believed[influenced.front()] = believed[node] + 1;
influenced.pop();
}
}
cout << cnt << "*\n";
return believed;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

// 입력
int n, m, nbr, rumor_monger;
cin >> n;
vector<vector<int>> nbr_graph(n + 1);
vector<int> rumor_mongers;

for (int i = 1; i <= n; i++)
{
while (true)
{
cin >> nbr;
if (!nbr)
{
break;
}
nbr_graph[i].push_back(nbr);
}
}
cin >> m;
while (m--)
{
cin >> rumor_monger;
rumor_mongers.push_back(rumor_monger);
}

// 계산
vector<int> ans = tookMin(rumor_mongers, n, nbr_graph);

// 출력
for (int i = 1; i <= n; i++)
{
cout << ans[i] << ' ';
}
}
75 changes: 75 additions & 0 deletions 08_DFS & BFS/필수/1325.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

vector<int> mostEfficientHack(vector<vector<int>> &relate, int n)
{
vector<int> ans;
int max_cnt = 0;

// 1부터 n번 컴퓨터까지 다 시작해본다. (브루트 포스)
for (int i = 1; i <= n; i++)
{
stack<int> s;
vector<bool> visited(n + 1, false);

s.push(i);
visited[i] = true;
int cnt = 1;
while (!s.empty())
{
int node = s.top();
s.pop();

for (int j = 0; j < relate[node].size(); j++)
{
int next_node = relate[node][j];
if (!visited[next_node])
{
// 해킹된 컴퓨터 있을때마다 count
visited[next_node] = true;
cnt++;
s.push(next_node);
}
}
}
// 최종 cnt로 max 업데이트 및 새로운 list
if (cnt > max_cnt)
{
max_cnt = cnt;
ans.resize(0);
ans.push_back(i);
}
else if (cnt == max_cnt)
{
ans.push_back(i);
}
}
return ans;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

int n, m, a, b;
cin >> n >> m;
vector<vector<int>> relate(n + 1);

while (m--)
{
cin >> a >> b;
relate[b].push_back(a);
}

vector<int> ans = mostEfficientHack(relate, n);

for (int comp : ans)
{
cout << comp << ' ';
}
}
64 changes: 64 additions & 0 deletions 08_DFS & BFS/필수/2615.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> board(19, vector<int>(19, 0));
int dr[] = {1, 1, 1, 0, 0, -1, -1, -1};
int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};

// 같은 방향으로 계속 전진하다가 다른 숫자 만나면 or 바둑판 범위 넘어가면 depth 리턴
int inARow(int i, int d, int r, int c, int color)
{
if (r < 0 || r >= 19 || c < 0 || c >= 19 || board[r][c] != color)
{
return d;
}

int nr = r + dr[i];
int nc = c + dc[i];

return inARow(i, d + 1, nr, nc, color);
}

int main()
{

for (int r = 0; r < 19; r++)
{
for (int c = 0; c < 19; c++)
{
cin >> board[r][c];
}
}

for (int r = 0; r < 19; r++)
{
for (int c = 0; c < 19; c++)
{
if (!board[r][c])
{
continue;
}

for (int i = 0; i < 8; i++)
{
// 리턴했을때 연속depth가 5였으면 승리!! //양방향 고려해줌 //양방향이므로 교집합 하나 빼줌
if (5 == inARow(i, 0, r, c, board[r][c]) + inARow(7 - i, 0, r, c, board[r][c]) - 1)
{
// 밑에서 왼쪽 방향으로 가는 경우에만 시작점이 최대왼쪽이 아니라서, 업데이트해줘야함
if (i == 0)
{
r += 4;
c -= 4;
}
cout << board[r][c] << '\n';
cout << r + 1 << ' ' << c + 1;
return 0;
}
}
}
}

cout << '0';
}
85 changes: 85 additions & 0 deletions 08_DFS & BFS/필수/4963.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int, int> pi;
int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};

// bfs를 통한 섬 개수 세기

int cntIsland(vector<vector<int>> &map, int h, int w)
{
// 미개척 땅이 1이므로 2부터 셈
int cnt = 1;
queue<pi> ut;

// cout << "^__^";

for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
// 개척 안한 땅 발견하면 "새 섬이다!"
if (map[i][j] == 1)
{
cnt++;
map[i][j] = cnt;
ut.push({i, j});

// 해당 섬 끝까지 마킹
while (!ut.empty())
{
pi land = ut.front();
ut.pop();

for (int k = 0; k < 8; k++)
{
int nr = land.first + dr[k];
int nc = land.second + dc[k];

if (nr >= 0 && nr < h && nc >= 0 && nc < w && map[nr][nc] == 1)
{
map[nr][nc] = cnt;
ut.push({nr, nc});
}
}
}

}
}
}
return --cnt;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

int w, h;

while (true)
{
cin >> w >> h;
if (!w && !h)
{
break;
}

vector<vector<int>> map(h, vector<int>(w, 0));

for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cin >> map[i][j];
}
}

cout << cntIsland(map, h, w) << '\n';
}
}
Loading