Skip to content

Commit

Permalink
Update 0x09-BOJ
Browse files Browse the repository at this point in the history
  • Loading branch information
yeochaeeon committed Aug 1, 2024
1 parent 283360b commit 32dcc8a
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main(){
if (c == '(') { // 열리는 괄호면 일단 push
s.push(c);
stick++;
} else { // 닫히는 괄호
} else { // c == ')'
if ( s.top() == '(') { // 레이저
s.push(c);
stick--;
Expand Down
48 changes: 48 additions & 0 deletions 0x09_BFS/Prac/1926-그림.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <queue>
#define X first
#define Y second

int board[502][502];
int vis[502][502];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int main(){
// std::ios::sync_with_stdio(false);
// std::cin.tie(nullptr);

int r,c;
std::cin >> r >> c;
std::queue<std::pair<int,int>> q;

for (int i = 0 ; i < r ; i++){
for (int j = 0; j < c ; j++){
std::cin >> board[i][j];
}
}
int num = 0;
int mxArea = 0;
for (int i = 0; i < r; i++){
for(int j = 0 ; j < c ; j++){
if (board[i][j] == 0 || vis[i][j] == 1) continue;
else {
vis[i][j] = 1;
num++;
q.push({i,j});
int area = 0;
while(!q.empty()){
std::pair<int,int> cur = q.front(); q.pop(); area++;
for(int dir = 0 ; dir < 4 ; dir++){
int nx = cur.X + dx[dir];
int ny = cur.Y + dy[dir];
if(vis[nx][ny] || board[nx][ny] != 1) continue;
vis[nx][ny] = 1;
q.push({nx,ny});
}
}
if ( mxArea < area ) mxArea = area;
}
}
} std::cout << num << "\n" << mxArea;
}
38 changes: 38 additions & 0 deletions 0x09_BFS/Prac/2178-미로탐색.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <queue>
#include <algorithm>

int N,M;
std::string board[102];
int dist[102][102];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int main(){

std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

std::cin >> N >> M;
for (int i = 0 ; i < N ; i++) {
std::cin >> board[i];
}
for(int i = 0; i < N; i++)
std::fill(dist[i],dist[i]+M,-1);

std::queue<std::pair<int,int>> q;
q.push({0,0});
dist[0][0] = 0;
while(!q.empty()){
auto cur = q.front(); q.pop();
for(int dir = 0 ; dir < 4; dir++){
int nx = cur.first + dx[dir];
int ny = cur.second + dy[dir];
if (nx < 0 || ny < 0 || nx >= N || ny >= M ) continue;
if (dist[nx][ny] >= 0 || board[nx][ny] != '1') continue;
dist[nx][ny] = dist[cur.first][cur.second] + 1;
q.push({nx,ny});
}
}
std::cout << dist[N-1][M-1] + 1;
}
48 changes: 48 additions & 0 deletions 0x09_BFS/Prac/7576-토마토.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <queue>

int board[1002][1002] ;
int dis[1002][1002];

int dx[4] = {1,0,-1,0};
int dy[4] = {0,-1,0,1};

int n,m;

int main(){

std::queue<std::pair<int,int> > q;
std::cin >> m >> n ;
for (int i = 0 ; i < n ; i++){
for (int j = 0; j < m; j++){
std::cin >> board[i][j];
if ( board[i][j] == 1) q.push({i,j});
if ( board[i][j] == 0) dis[i][j] = -1;
}
}

while(!q.empty()){
auto cur = q.front(); q.pop();
for(int dir = 0 ; dir < 4; dir++){
int nx = cur.first + dx[dir];
int ny = cur.second + dy[dir];
if ( nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
if ( dis[nx][ny] >= 0 ) continue;
dis[nx][ny] = dis[cur.first][cur.second] + 1;
q.push({nx,ny});
}
}
int maxDist = 0;
for (int i = 0 ; i < n; i++){
for (int j = 0 ; j < m; j++){
// std::cout << dis[i][j] ;
if (dis[i][j] == -1) {
std::cout << -1 ;
return 0;
}
if (dis[i][j] > maxDist) maxDist = dis[i][j];
}
// std::cout << "\n";
}
std::cout << maxDist ;
}
35 changes: 35 additions & 0 deletions 0x09_BFS/Prac/BFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second // pair에서 first, second를 줄여서 쓰기 위해서 사용
int board[502][502] =
{{1,1,1,0,1,0,0,0,0,0},
{1,0,0,0,1,0,0,0,0,0},
{1,1,1,0,1,0,0,0,0,0},
{1,1,0,0,1,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0} }; // 1이 파란 칸, 0이 빨간 칸에 대응
bool vis[502][502]; // 해당 칸을 방문했는지 여부를 저장
int n = 7, m = 10; // n = 행의 수, m = 열의 수
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1}; // 상하좌우 네 방향을 의미
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
queue<pair<int,int> > Q;
vis[0][0] = 1; // (0, 0)을 방문했다고 명시
Q.push({0,0}); // 큐에 시작점인 (0, 0)을 삽입.
while(!Q.empty()){
pair<int,int> cur = Q.front(); Q.pop();
cout << '(' << cur.X << ", " << cur.Y << ") -> ";
for(int dir = 0; dir < 4; dir++){ // 상하좌우 칸을 살펴볼 것이다.
int nx = cur.X + dx[dir];
int ny = cur.Y + dy[dir]; // nx, ny에 dir에서 정한 방향의 인접한 칸의 좌표가 들어감
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue; // 범위 밖일 경우 넘어감
if(vis[nx][ny] || board[nx][ny] != 1) continue; // 이미 방문한 칸이거나 파란 칸이 아닐 경우
vis[nx][ny] = 1; // (nx, ny)를 방문했다고 명시
Q.push({nx,ny});
}
}
}
50 changes: 50 additions & 0 deletions 0x09_BFS/Prac/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>
#include <queue>

using namespace std;
#define X first
#define Y second
int board[1002][1002];
int dist[1002][1002];
int n,m;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> m >> n;
queue<pair<int,int>> Q;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> board[i][j];
if(board[i][j] == 1)
Q.push({i,j});
if(board[i][j] == 0)
dist[i][j] = -1;
}
}
while(!Q.empty()){
auto cur = Q.front(); Q.pop();
for(int dir = 0; dir < 4; dir++){
int nx = cur.X + dx[dir];
int ny = cur.Y + dy[dir];
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if(dist[nx][ny] >= 0) continue;
dist[nx][ny] = dist[cur.X][cur.Y]+1;
Q.push({nx,ny});
}
}
int maxDist = 0;
for (int i = 0 ; i < n; i++){
for (int j = 0 ; j < m; j++){
std::cout << dist[i][j] ;
if (dist[i][j] == -1) {
std::cout << -1 ;
return 0;
}
if (dist[i][j] > maxDist) maxDist = dist[i][j];
}
std::cout << "\n";
}
std::cout << maxDist;
}
1 change: 1 addition & 0 deletions 0x09_BFS/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 0x09_BFS

0 comments on commit 32dcc8a

Please sign in to comment.