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

hyerang0125 #42

Open
wants to merge 7 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
36 changes: 36 additions & 0 deletions WEEK3-A/hyerang0125/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
> 🖇 2021 2학기 3주차 - 깊이 우선 탐색, 너비 우선 탐색

</br>

# 1️⃣ Depth-First Search?

## Depth-First Search 정의

- 루트 노드(혹은 다른 임의의 노드)에서 시작해서 다음 분기(branch)로 넘어가기 전에 해당 분기를 완벽하게 탐색하는 방법
- 넒게 탐색하기 전에 깊게 탐색하는 것이 특징이다.

</br>

## Depth-First Search 특징

1. 자기 자신을 호출하는 **순환 알고리즘의 형태**를 가지고 있다. 따라서 모든 노드를 방문하고자 하는 경우에 사용한다.
2. 전위 순회(Pre-Order Traversals)를 포함한 다른 형태의 트리 순회는 모두 DFS의 한 종류이다.
3. 어떤 노트를 방문 했었는지 여부를 반드시 검사해야 한다. (검사 x -> 무한루프 위험)

</br>

# 2️⃣ Breadth-First Search

## Breadth-First Search 정의

- 루트 노드(혹은 다른 임의의 노드)에서 시작해서 인접한 노드를 먼저 탐색하는 방법
- 시작 정점으로부터 가까운 정점을 먼저 방문하고 멀리 떨어져있는 정점을 나중에 방문하는 순회 방법이다.
- 즉, 깊게 탐색하기 전에 넓게 탐색한다.

## Breadth-First Search 특징

1. 두 노드 사이의 최단 경로 혹은 임의의 경로를 찾고 싶을 때 주로 사용한다.
2. DFS와 다르게 재귀적으로 동작하지 않고, 큐를 사용하여 반복적인 형태로 구현한다.
3. Prim, Dijkstra 알고리즘과 유사하다.

</br>
96 changes: 96 additions & 0 deletions WEEK3-A/hyerang0125/boj14502.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <bits/stdc++.h>

using namespace std;

int n, m, cntM = 0;
int mapL[8][8];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};

void bfs()
{
int x, y, mx, my, cnt;
int virus[8][8];
queue<pair<int, int>> q;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
virus[i][j] = mapL[i][j];
if (virus[i][j] == 2)
{ //바이러스 위치 저장
q.push({i, j});
}
}
}
while (!q.empty())
{
x = q.front().first;
y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) //바이러스 상하좌우로 이동
{
mx = x + dx[i];
my = y + dy[i];
if (0 <= mx && mx < n && 0 <= my && my < m)
{
if (virus[mx][my] == 1 || virus[mx][my] == 2) //감염상태거나 벽이면 continue
continue;
virus[mx][my] = 2; //감염
q.push({mx, my});
}
}
}
cnt = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (virus[i][j] == 0)
++cnt;
}
}
cntM = max(cntM, cnt); //안전지대 최대값 저장
}

void make_wall(int cnt)
{
if (cnt == 3) //벽은 무조건 3개 세우는 것이므로 3개가 되었을 때 안전지대 검사
{
bfs();
return;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mapL[i][j] == 2 || mapL[i][j] == 1)
continue;
mapL[i][j] = 1;
make_wall(cnt + 1);
mapL[i][j] = 0;
}
}
}

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

cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> mapL[i][j];
}
}

make_wall(0);

cout << cntM;

return 0;
}
84 changes: 84 additions & 0 deletions WEEK3-A/hyerang0125/boj2206.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <bits/stdc++.h>

using namespace std;

int n, m, cntM = 987654321;
int mapL[1000][1000];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};

void bfs()
{
int x, y, mx, my, cnt;
bool flag;
queue<pair<int, int>> q;
bool check[1000][1000];
memset(check, false, sizeof(check));
q.push({0, 0});
check[0][0] = true;
cnt = 0;
while (!q.empty())
{
x = q.front().first;
y = q.front().second;
q.pop();
if (x == n - 1 && y == m - 1)
{
cntM = min(cntM, cnt);
return;
}
flag = false;
for (int i = 0; i < 4; i++) //갔다가 아닌거에서 오류남 미치겠음 그냥 내가 벽을 부수고 싶은 심정임
{
mx = x + dx[i];
my = y + dy[i];
if (0 <= mx && mx < n && 0 <= my && my < m)
{
if (mapL[mx][my] == 1 || check[mx][my])
continue;
++cnt;
flag = true;
check[mx][my] = true;
q.push({mx, my});
}
}
}
}

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

cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> mapL[i][j];
}
}

bfs(); //원래 최단거리

//벽 부수고 최단거리
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mapL[i][j] == 0)
continue; //벽 아니면 바꿀 필요 x
mapL[i][j] = 0;
bfs();
mapL[i][j] = 1;
}
}

if (cntM == 987654321)
cout << "-1";
else
cout << cntM;

return 0;
}
67 changes: 67 additions & 0 deletions WEEK3-A/hyerang0125/boj9019.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <bits/stdc++.h>

using namespace std;

bool check[10000];

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

string result;
int A, B, temp, now, n;
cin >> n;
for (int i = 0; i < n; i++)
{

cin >> A >> B;
memset(check, false, sizeof(check));
queue<pair<int, string>> q;
q.push({A, ""});
check[A] = true;
while (!q.empty())
{
now = q.front().first;
result = q.front().second;
q.pop();
if (now == B)
{
cout << result << "\n";
break;
}
//D
temp = now * 2 % 10000;
if (!check[temp])
{
check[temp] = true;
q.push({temp, result + "D"});
}
//S
//처음에 now - 1 == 0으로 해서 틀렸었는데 n == 0일 때 9999를 출력해야 하는 것이었음,,,
temp = now - 1 < 0 ? 9999 : now - 1;
if (!check[temp])
{
check[temp] = true;
q.push({temp, result + "S"});
}
//L
temp = (now % 1000) * 10 + now / 1000;
if (!check[temp])
{
check[temp] = true;
q.push({temp, result + "L"});
}
//R
temp = (now % 10) * 1000 + now / 10;
if (!check[temp])
{
check[temp] = true;
q.push({temp, result + "R"});
}
}
}

return 0;
}
62 changes: 62 additions & 0 deletions WEEK4-A/hyerang0125/108030.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <bits/stdc++.h>

using namespace std;

long long matrix[5][5];
long long temp[5][5];
long long result[5][5];
long long N, B;

// 행렬 곱셈 구현
void matrix_m(long long m1[5][5], long long m2[5][5]){
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
temp[i][j] = 0;
for(int k=0; k<N; k++){
temp[i][j] += m1[i][k] * m2[k][j];
}
temp[i][j] %= 1000;
}
}

for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
m1[i][j] = temp[i][j];
}
}
}

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

cin >> N >> B;
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
cin >> matrix[i][j];
}
result[i][i] = 1;
}

//빠른 거듭제곱 알고리즘
while(B){
// B가 홀수이면 matrix^B를 matrix * matrix^(B-1)로 바꾸고 matrix를 결과값에 곱한다.
// B가 짝수이면 matrix^B를 (matrix^2)^(B/2) -> matrix를 제곱하고 B를 2로 나눈다.
// B가 0이면 종료
if(B % 2 == 1)
matrix_m(result, matrix);
matrix_m(matrix, matrix);
B /= 2;
}

for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
cout << result[i][j] <<" ";
}
cout <<"\n";
}

return 0;
}
36 changes: 36 additions & 0 deletions WEEK4-A/hyerang0125/14601.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// (1, 1) 부터 타일을 채워나가는 방식을 사용하려 함

#include <bits/stdc++.h>

using namespace std;

int MAP[128][128];

void draw(int row, int col)
{

// ㄴ
if (row - 1 >= 0 && col)

// ㄱ

// ㄱ 대칭

// ㄴ 대칭
}

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

memset(MAP, 0, sizeof(MAP));
int k, x, y;

cin >> k;
cin >> x >> y;
MAP[x][y] = -1; // 하수구 위치 표시

return 0;
}
Loading