From 22c51616e3b1d2a0d436b707fc47d040e0b00744 Mon Sep 17 00:00:00 2001 From: Yeonsoo <127713796+sooooscode@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:14:32 +0900 Subject: [PATCH 1/6] Create 2615.cpp --- 08_DFS_BFS/2615.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 08_DFS_BFS/2615.cpp diff --git a/08_DFS_BFS/2615.cpp b/08_DFS_BFS/2615.cpp new file mode 100644 index 0000000..7fa5095 --- /dev/null +++ b/08_DFS_BFS/2615.cpp @@ -0,0 +1,73 @@ +//DFS_BFS 필수 2번: 2615 오목 +#include +#include + +using namespace std; + +bool isInside(int x, int y) +{ + return x >= 0 && x < 19 && y >= 0 && y < 19; +} + +int checkWin(const vector>& board, int x, int y) { + int dx[4] = {1, 0, 1, -1}; // 가로, 세로, 대각선 방향을 나타내는 x 좌표 변화값 + int dy[4] = {0, 1, 1, 1}; // 가로, 세로, 대각선 방향을 나타내는 y 좌표 변화값 + + for (int dir = 0; dir < 4; ++dir) + { + int count = 1; // 연속된 바둑알의 개수 초기화 + int nx = x + dx[dir]; // 다음 검사할 x 좌표 계산 + int ny = y + dy[dir]; // 다음 검사할 y 좌표 계산 + + while (isInside(nx, ny) && board[nx][ny] == board[x][y]) + { + count++; // 같은 색 바둑알 개수 증가 + nx += dx[dir]; // 다음 검사할 x 좌표 갱신 + ny += dy[dir]; // 다음 검사할 y 좌표 갱신 + } + + if (count == 5) { // 연속된 바둑알이 5개인 경우 + // 승부가 결정된 경우 + int prev_x = x - dx[dir]; + int prev_y = y - dy[dir]; + if (!isInside(prev_x, prev_y) || board[prev_x][prev_y] != board[x][y]) { + // 승리한 플레이어의 색과 다른 색이 놓인 경우에만 승부로 인정 + return board[x][y]; + } + } + } + + return 0; // 승리 조건 미충족 +} + +int main() +{ + vector> board(19, vector(19, 0)); // 19x19 바둑판 초기화 + + // 바둑판 상태 입력 + for (int i = 0; i < 19; ++i) + { + for (int j = 0; j < 19; ++j) { + cin >> board[i][j]; // 각 위치의 바둑알 상태 입력 + } + } + + for (int i = 0; i < 19; ++i) + { + for (int j = 0; j < 19; ++j) + { + if (board[i][j] != 0) + { // 빈 칸이 아닌 경우 + int result = checkWin(board, i, j); // 승리 여부 확인 + if (result != 0) + { // 승부가 결정된 경우 + cout << result << '\n' << i + 1 << ' ' << j + 1 << '\n'; // 승리한 플레이어와 위치 출력 + return 0; + } + } + } + } + + cout << "0\n"; // 아직 승부가 결정되지 않은 경우 + return 0; +} From a4a038e3fc1185de0b1af4572b66b9a5ccad7cd2 Mon Sep 17 00:00:00 2001 From: Yeonsoo <127713796+sooooscode@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:15:44 +0900 Subject: [PATCH 2/6] Create 4963.cpp --- 08_DFS_BFS/4963.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 08_DFS_BFS/4963.cpp diff --git a/08_DFS_BFS/4963.cpp b/08_DFS_BFS/4963.cpp new file mode 100644 index 0000000..e42c706 --- /dev/null +++ b/08_DFS_BFS/4963.cpp @@ -0,0 +1,92 @@ +//DFS_BFS 필수 3번: 4963 섬의 개수 +#include +#include +#include + +using namespace std; + +const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; +const int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; + +bool isInside(int x, int y, int w, int h) +{ + return (x >= 0 && x < w && y >= 0 && y < h); +} + +void bfs(int x, int y, vector>& grid, vector>& visited) +{ + int w = grid.size(); + int h = grid[0].size(); + + queue> q; + q.push(make_pair(x, y)); + visited[x][y] = true; + + while (!q.empty()) + { + int curX = q.front().first; + int curY = q.front().second; + q.pop(); + + for (int i = 0; i < 8; ++i) + { + int newX = curX + dx[i]; + int newY = curY + dy[i]; + + if (isInside(newX, newY, w, h) && grid[newX][newY] == 1 && !visited[newX][newY]) + { + q.push(make_pair(newX, newY)); + visited[newX][newY] = true; + } + } + } +} + +int countIslands(vector>& grid) +{ + int w = grid.size(); + int h = grid[0].size(); + vector> visited(w, vector(h, false)); + int islandCount = 0; + + for (int i = 0; i < w; ++i) + { + for (int j = 0; j < h; ++j) + { + if (grid[i][j] == 1 && !visited[i][j]) + { + bfs(i, j, grid, visited); + islandCount++; + } + } + } + + return islandCount; +} + +int main() +{ + while (true) + { + int w, h; + cin >> w >> h; + + if (w == 0 && h == 0) { + break; + } + + vector> grid(w, vector(h)); + for (int i = 0; i < h; ++i) + { + for (int j = 0; j < w; ++j) + { + cin >> grid[j][i]; + } + } + + int islandCount = countIslands(grid); + cout << islandCount << "\n"; + } + + return 0; +} From 7c6a509372a0d48bf71a3ab2f5b8976dec9e5afa Mon Sep 17 00:00:00 2001 From: Yeonsoo <127713796+sooooscode@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:54:45 +0900 Subject: [PATCH 3/6] Create 1325.cpp --- 08_DFS_BFS/1325.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 08_DFS_BFS/1325.cpp diff --git a/08_DFS_BFS/1325.cpp b/08_DFS_BFS/1325.cpp new file mode 100644 index 0000000..5783f71 --- /dev/null +++ b/08_DFS_BFS/1325.cpp @@ -0,0 +1,71 @@ +//DFS_BFS 필수 1번: 1325 효율적인 해킹 +#include +#include +#include +#include + +using namespace std; + +vector> adj; +vector visit; +vector result; +int maxCount = -1; + +void bfs(int start) +{ + queue q; + q.push(start); + visit[start] = true; + int count = 0; + + while (!q.empty()) + { + int node = q.front(); + q.pop(); + + for (int next : adj[node]) + { + if (!visit[next]) + { + q.push(next); + visit[next] = true; + count++; + } + } + } + + result[start] = count; + maxCount = max(maxCount, count); +} + +int main() +{ + int n, m; + cin >> n >> m; + + adj.resize(n + 1); + visit.resize(n + 1, false); + result.resize(n + 1, 0); + + for (int i = 0; i < m; i++) + { + int a, b; + cin >> a >> b; + adj[b].push_back(a); + } + + for (int i = 1; i <= n; i++) + { + fill(visit.begin(), visit.end(), false); + bfs(i); + } + + for (int i = 1; i <= n; i++) + { + if (result[i] == maxCount) { + cout << i << " "; + } + } + + return 0; +} From fc0bba4325a42a53e9de9ed67eaee7bbb5a0bc78 Mon Sep 17 00:00:00 2001 From: Yeonsoo <127713796+sooooscode@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:55:53 +0900 Subject: [PATCH 4/6] Create 19538.cpp --- 08_DFS_BFS/19538.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 08_DFS_BFS/19538.cpp diff --git a/08_DFS_BFS/19538.cpp b/08_DFS_BFS/19538.cpp new file mode 100644 index 0000000..59347a8 --- /dev/null +++ b/08_DFS_BFS/19538.cpp @@ -0,0 +1,65 @@ +// DFS_BFS 도전 1번: 19538 루머 +#include +#include +#include +#include + +using namespace std; + +int n, m; +vector> graph; +queue q; +vector time; +vector neighbor; + +void bfs() { + while (!q.empty()) { + int nowNode = q.front(); + q.pop(); + + for (int next : graph[nowNode]) { + // 이웃에게 난 루머를 알고있다고 전파 + neighbor[next]++; + + // 시간이 기록 돼 있지 않고 && 본인 노드 수 / 2 <= 본인 이웃 노드가 알고있는 루머 수 + if (time[next] == -1 && (graph[next].size() + 1) / 2 <= neighbor[next]) { + q.push(next); + time[next] = time[nowNode] + 1; + } + } + } +} + +int main() { + cin >> n; + graph.resize(n + 1); + time.resize(n + 1, -1); + neighbor.resize(n + 1, 0); + + for (int i = 1; i <= n; i++) { + int a; + while (true) { + cin >> a; + if (a == 0) break; + graph[i].push_back(a); + } + } + + cin >> m; + + for (int i = 0; i < m; i++) { + int node; + cin >> node; + time[node] = 0; + q.push(node); + } + + bfs(); + + for (int i = 1; i <= n; i++) { + cout << time[i]; + if (i != n) cout << " "; + } + + return 0; +} From 43954635e2ce02c1c2315a57920e9562fff66d4d Mon Sep 17 00:00:00 2001 From: Yeonsoo <127713796+sooooscode@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:56:37 +0900 Subject: [PATCH 5/6] =?UTF-8?q?Create=20=EA=B2=8C=EC=9E=84=5F=EB=A7=B5=5F?= =?UTF-8?q?=EC=B5=9C=EB=8B=A8=EA=B1=B0=EB=A6=AC.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\353\213\250\352\261\260\353\246\254.cpp" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "08_DFS_BFS/\352\262\214\354\236\204_\353\247\265_\354\265\234\353\213\250\352\261\260\353\246\254.cpp" diff --git "a/08_DFS_BFS/\352\262\214\354\236\204_\353\247\265_\354\265\234\353\213\250\352\261\260\353\246\254.cpp" "b/08_DFS_BFS/\352\262\214\354\236\204_\353\247\265_\354\265\234\353\213\250\352\261\260\353\246\254.cpp" new file mode 100644 index 0000000..48e52ef --- /dev/null +++ "b/08_DFS_BFS/\352\262\214\354\236\204_\353\247\265_\354\265\234\353\213\250\352\261\260\353\246\254.cpp" @@ -0,0 +1,53 @@ +//DFS_BFS 도전 2번: 게임_맵_최단거리 +#include +#include +using namespace std; + +int solution(vector> maps) +{ + int n = maps.size(); + int m = maps[0].size(); + + vector> dist(n, vector(m, -1)); + queue> q; + + // 시작 위치와 도착 위치 초기화 + q.push({0, 0}); + dist[0][0] = 1; + + // 상하좌우 이동 방향 + int dr[] = {-1, 1, 0, 0}; + int dc[] = {0, 0, -1, 1}; + + while (!q.empty()) + { + int r = q.front().first; + int c = q.front().second; + q.pop(); + + // 상대 팀 진영에 도착하면 거리 반환 + if (r == n - 1 && c == m - 1) { + return dist[r][c]; + } + + // 상하좌우 이동 + for (int i = 0; i < 4; i++) + { + int nr = r + dr[i]; + int nc = c + dc[i]; + + // 유효한 위치인지 확인 + if (nr >= 0 && nr < n && nc >= 0 && nc < m) + { + // 벽이 아니고 아직 방문하지 않은 곳인 경우 + if (maps[nr][nc] == 1 && dist[nr][nc] == -1) { + dist[nr][nc] = dist[r][c] + 1; + q.push({nr, nc}); + } + } + } + } + + // 상대 팀 진영에 도착할 수 없는 경우 + return -1; +} From a8cd6ff8652e0f3bb3fe737394718c1cced8514f Mon Sep 17 00:00:00 2001 From: Yeonsoo <127713796+sooooscode@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:57:19 +0900 Subject: [PATCH 6/6] Update 19538.cpp --- 08_DFS_BFS/19538.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/08_DFS_BFS/19538.cpp b/08_DFS_BFS/19538.cpp index 59347a8..5cbc2b6 100644 --- a/08_DFS_BFS/19538.cpp +++ b/08_DFS_BFS/19538.cpp @@ -13,16 +13,19 @@ vector time; vector neighbor; void bfs() { - while (!q.empty()) { + while (!q.empty()) + { int nowNode = q.front(); q.pop(); - for (int next : graph[nowNode]) { + for (int next : graph[nowNode]) + { // 이웃에게 난 루머를 알고있다고 전파 neighbor[next]++; // 시간이 기록 돼 있지 않고 && 본인 노드 수 / 2 <= 본인 이웃 노드가 알고있는 루머 수 - if (time[next] == -1 && (graph[next].size() + 1) / 2 <= neighbor[next]) { + if (time[next] == -1 && (graph[next].size() + 1) / 2 <= neighbor[next]) + { q.push(next); time[next] = time[nowNode] + 1; } @@ -30,15 +33,19 @@ void bfs() { } } -int main() { +int main() +{ + cin >> n; graph.resize(n + 1); time.resize(n + 1, -1); neighbor.resize(n + 1, 0); - for (int i = 1; i <= n; i++) { + for (int i = 1; i <= n; i++) + { int a; - while (true) { + while (true) + { cin >> a; if (a == 0) break; graph[i].push_back(a); @@ -47,7 +54,8 @@ int main() { cin >> m; - for (int i = 0; i < m; i++) { + for (int i = 0; i < m; i++) + { int node; cin >> node; time[node] = 0; @@ -56,7 +64,8 @@ int main() { bfs(); - for (int i = 1; i <= n; i++) { + for (int i = 1; i <= n; i++) + { cout << time[i]; if (i != n) cout << " "; }