Skip to content

Commit

Permalink
finish chapter 09~11
Browse files Browse the repository at this point in the history
  • Loading branch information
rhs0266 committed Jun 5, 2021
1 parent c0ed412 commit c63d33e
Show file tree
Hide file tree
Showing 31 changed files with 2,084 additions and 9 deletions.
18 changes: 9 additions & 9 deletions 강의 자료/02-알고리즘/09~11-그래프 탐색/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
|문제 이름|문제 링크|답안 코드 링크|
|---|---|---|
|DFS와 BFS|[링크](http://boj.kr/1260)|[링크]|
|DFS와 BFS|[링크](http://boj.kr/1260)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-%EA%B7%B8%EB%9E%98%ED%94%84%20%ED%83%90%EC%83%89/%EB%AC%B8%EC%A0%9C%EB%B3%84%20%EC%BD%94%EB%93%9C/1260-DFS%EC%99%80%20BFS)|
|단지번호붙이기|[링크](http://boj.kr/2667)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/2667-단지번호%20붙이기)|
|물통|[링크](http://boj.kr/2251)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/2251-물통)|
|연구소|[링크](http://boj.kr/14502)|[링크]|
|연구소|[링크](http://boj.kr/14502)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/14502-연구소)|
|**연습문제**|||
|유기농 배추|[링크](http://boj.kr/1012)|[링크]|
|연결 요소의 개수|[링크](http://boj.kr/11724)|[링크]|
|섬의 개수|[링크](http://boj.kr/4963)|[링크]|
||[링크](http://boj.kr/3184)|[링크]|
|바이러스|[링크](http://boj.kr/2606)|[링크]|
|경로 찾기|[링크](http://boj.kr/11403)|[링크]|
|트리의 부모 찾기|[링크](http://boj.kr/11725)|[링크]|
|유기농 배추|[링크](http://boj.kr/1012)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/1012-유기농%20배추)|
|연결 요소의 개수|[링크](http://boj.kr/11724)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/11724-연결%20요소의%20개수)|
|섬의 개수|[링크](http://boj.kr/4963)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/4963-섬의%20개수)|
||[링크](http://boj.kr/3184)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/3184-양)|
|바이러스|[링크](http://boj.kr/2606)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/2606-바이러스)|
|경로 찾기|[링크](http://boj.kr/11403)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/11403-경로%20찾기)|
|트리의 부모 찾기|[링크](http://boj.kr/11725)|[링크](https://github.com/rhs0266/FastCampus/tree/main/%EA%B0%95%EC%9D%98%20%EC%9E%90%EB%A3%8C/02-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98/09~11-그래프%20탐색/문제별%20코드/11725-트리의%20부모%20찾기)|
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
static FastReader scan = new FastReader();
static StringBuilder sb = new StringBuilder();

static int N, M, K;
static int[][] a;
static boolean[][] visit;
static int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

static void input() {
M = scan.nextInt();
N = scan.nextInt();
K = scan.nextInt();
a = new int[N][M];
for (int i = 0; i < K; i++) {
int y = scan.nextInt(), x = scan.nextInt();
a[x][y] = 1;
}
visit = new boolean[N][M];
}

// x, y 를 갈 수 있다는 걸 알고 방문한 상태
static void dfs(int x, int y) {
/* TODO */
}

static void pro() {
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (!visit[i][j] && a[i][j] == 1) {
// 새로운 배추흰지렁이 발견!
/* TODO */
}
}
}

System.out.println(ans);
}

public static void main(String[] args) {
int T = scan.nextInt();
while (T-- > 0) {
input();
pro();
}
}


static class FastReader {
BufferedReader br;
StringTokenizer st;

public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}

public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}

String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}

int nextInt() {
return Integer.parseInt(next());
}

long nextLong() {
return Long.parseLong(next());
}

double nextDouble() {
return Double.parseDouble(next());
}

String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <algorithm>
#include <string>
typedef long long int ll;
using namespace std;

#define NM 55

int N, M, K;
int a[NM][NM];

void input() {
cin >> M >> N >> K;
for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) a[i][j] = 0;
for (int i = 1; i <= K; i++) {
int x, y;
cin >> y >> x;
x++, y++;
a[x][y] = 1;
}
}

int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
bool visit[NM][NM];

// x, y 를 갈 수 있다는 걸 알고 방문한 상태
void dfs(int x, int y) {
visit[x][y] = true;
for (int k = 0; k < 4; k++) {
int nx = x + dir[k][0];
int ny = y + dir[k][1];
if (nx < 1 || ny < 1 || nx > N || ny > M) continue; // 지도를 벗어나는 곳으로 가는가?
if (a[nx][ny] == 0) continue; // 갈 수 있는 칸인지 확인해야 한다.
if (visit[nx][ny]) continue; // 이미 방문한 적이 있는 곳인가?
dfs(nx, ny);
}
}
void pro() {
int ans = 0;
for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) visit[i][j] = false;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
if (a[i][j] == 1 && !visit[i][j]) {
ans++;
dfs(i, j);
}
cout << ans << "\n";
}


int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T;
cin >> T;
while (T--) {
input();
pro();
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
static FastReader scan = new FastReader();
static StringBuilder sb = new StringBuilder();

static int N, M, K;
static int[][] a;
static boolean[][] visit;
static int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

static void input() {
M = scan.nextInt();
N = scan.nextInt();
K = scan.nextInt();
a = new int[N][M];
for (int i = 0; i < K; i++) {
int y = scan.nextInt(), x = scan.nextInt();
a[x][y] = 1;
}
visit = new boolean[N][M];
}

// x, y 를 갈 수 있다는 걸 알고 방문한 상태
static void dfs(int x, int y) {
visit[x][y] = true;
for (int k = 0; k < 4; k++) {
int nx = x + dir[k][0];
int ny = y + dir[k][1];
if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue; // 지도를 벗어나는 곳으로 가는가?
if (a[nx][ny] == 0) continue; // 갈 수 있는 칸인지 확인해야 한다.
if (visit[nx][ny]) continue; // 이미 방문한 적이 있는 곳인가?
dfs(nx, ny);
}
}

static void pro() {
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (!visit[i][j] && a[i][j] == 1) {
// 새로운 배추흰지렁이 발견!
ans++;
dfs(i, j);
}
}
}

System.out.println(ans);
}

public static void main(String[] args) {
int T = scan.nextInt();
while (T-- > 0) {
input();
pro();
}
}


static class FastReader {
BufferedReader br;
StringTokenizer st;

public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}

public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}

String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}

int nextInt() {
return Integer.parseInt(next());
}

long nextLong() {
return Long.parseLong(next());
}

double nextDouble() {
return Double.parseDouble(next());
}

String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import sys
from collections import deque
si = sys.stdin.readline
sys.setrecursionlimit(100000)

dir = [[1, 0], [0, 1], [-1, 0], [0, -1]]
def dfs(x, y):
visit[x][y] = True
for dx, dy in dir:
nx, ny = x + dx, y + dy
if nx < 0 or nx >= n or ny < 0 or ny >= m: continue
if visit[nx][ny]: continue
if a[nx][ny] == 0: continue
dfs(nx, ny)

T = int(si())
while T:
T -= 1
m, n, K = map(int, si().split())
a = [[0] * m for _ in range(n)]
for i in range(K):
y, x = map(int, si().split())
a[x][y] = 1

visit = [[False] * m for _ in range(n)]

ans = 0
for i in range(n):
for j in range(m):
if a[i][j] == 0 or visit[i][j]:
continue
ans += 1
dfs(i, j)

print(ans)
Loading

0 comments on commit c63d33e

Please sign in to comment.