Skip to content

Commit f5fb9fa

Browse files
committed
feat:그래프탐색
1 parent 83b67e5 commit f5fb9fa

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

PR-template.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## PR제목
2+
처음 문제를 푼 경우,
3+
- type1 : [Solved / Failed] PRG_문제명
4+
- type2 : [Solved / Failed] PRG_문제명(복습필요)
5+
이후 복습을 한경우,
6+
- [Solved / Failed] PRG_문제명(복습횟수)
7+
- ex) [Solved / Failed] PRG_문제명(2)
8+
---
9+
110
## 문제 및 풀이과정
211

312
[문제명]()
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,57 @@
11
package Week2.개인.단지번호붙이기;
22

3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
310
public class suhyun {
11+
12+
static int n, ans = 0;
13+
static char[][] graph;
14+
static int[][] dir = {{0,1}, {1,0}, {0,-1}, {-1,0}};
15+
static List<Integer> houses = new ArrayList<Integer>();
16+
public static void main(String[] args) throws IOException {
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
n = Integer.parseInt(br.readLine());
19+
graph = new char[n][n];
20+
for(int i=0; i<n; i++){
21+
String str = br.readLine();
22+
for(int j=0; j<n; j++){
23+
graph[i][j] = str.charAt(j);
24+
}
25+
}
26+
for(int i=0; i<n; i++){
27+
for(int j=0; j<n; j++){
28+
if(graph[i][j] == '1'){
29+
dfs(i,j);
30+
houses.add(ans);
31+
ans = 0;
32+
}
33+
}
34+
}
35+
System.out.println(houses.size());
36+
Collections.sort(houses);
37+
for(int i=0; i<houses.size(); i++){
38+
System.out.println(houses.get(i));
39+
}
40+
}
41+
42+
static int dfs(int x, int y){
43+
ans += 1;
44+
graph[x][y] = '0';
45+
for(int i=0; i<4; i++){
46+
int nx = x + dir[i][0];
47+
int ny = y + dir[i][1];
48+
if(nx < 0 || ny < 0 || nx >= n || ny >= n){
49+
continue;
50+
}
51+
if(graph[nx][ny] == '1'){
52+
dfs(nx, ny);
53+
}
54+
}
55+
return ans;
56+
}
457
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
si = sys.stdin.readline
3+
sys.setrecursionlimit(10000)
4+
n = int(si())
5+
graph = [list(map(int, si().strip())) for _ in range(n)]
6+
dir = [[0,1], [1,0], [0,-1], [-1,0]]
7+
answer = []
8+
ans = 0
9+
10+
def dfs(x, y):
11+
global ans
12+
ans += 1
13+
graph[x][y] = 0
14+
15+
for i in range(4):
16+
nx = x + dir[i][0]
17+
ny = y + dir[i][1]
18+
19+
if nx < 0 or ny < 0 or nx >= n or ny >= n:
20+
continue
21+
if graph[nx][ny] == 1:
22+
dfs(nx, ny)
23+
24+
25+
for x, row in enumerate(graph):
26+
for y, col in enumerate(row):
27+
if col == 1:
28+
dfs(x, y)
29+
answer.append(ans)
30+
ans = 0
31+
32+
print(len(answer))
33+
answer.sort()
34+
for i in answer:
35+
print(i)
+51
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
11
package Week2.개인.유기농배추;
22

3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
38
public class suhyun {
9+
10+
static int t, m, n, k, a, b;
11+
static StringTokenizer st;
12+
static int[][] graph;
13+
static int[][] dir = {{0,1}, {1,0}, {0,-1}, {-1,0}};
14+
public static void main(String[] args) throws IOException {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
t = Integer.parseInt(br.readLine());
17+
for(int i=0; i<t; i++){
18+
st = new StringTokenizer(br.readLine(), " ");
19+
m = Integer.parseInt(st.nextToken());
20+
n = Integer.parseInt(st.nextToken());
21+
k = Integer.parseInt(st.nextToken());
22+
graph = new int[n][m];
23+
for(int j=0; j<k; j++){
24+
st = new StringTokenizer(br.readLine(), " ");
25+
a = Integer.parseInt(st.nextToken());
26+
b = Integer.parseInt(st.nextToken());
27+
graph[b][a] = 1;
28+
}
29+
int ans = 0;
30+
for(int x=0; x<graph.length; x++){
31+
for(int y=0; y<graph[x].length; y++){
32+
if(graph[x][y] == 1){
33+
dfs(x, y);
34+
ans += 1;
35+
}
36+
}
37+
}
38+
System.out.println(ans);
39+
}
40+
}
41+
42+
static void dfs(int x, int y){
43+
graph[x][y] = 0;
44+
for(int i=0; i<4; i++){
45+
int nx = x + dir[i][0];
46+
int ny = y + dir[i][1];
47+
if(nx < 0 || ny < 0 || nx >= n || ny >= m){
48+
continue;
49+
}
50+
if(graph[nx][ny] == 1){
51+
dfs(nx, ny);
52+
}
53+
}
54+
}
455
}

Week2/개인/유기농배추/suhyun.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
si = sys.stdin.readline
3+
sys.setrecursionlimit(100000)
34

45
dir = [[0,1], [1,0], [0,-1], [-1,0]]
56

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
sys.setrecursionlimit(100000)
3+
si = sys.stdin.readline
4+
t = int(si())
5+
dir = [[0,1], [1,0], [0,-1], [-1,0]]
6+
7+
def dfs(x, y):
8+
graph[x][y] = 0
9+
10+
for i in range(4):
11+
nx = x + dir[i][0]
12+
ny = y + dir[i][1]
13+
14+
if nx < 0 or ny < 0 or nx >= n or ny >= m:
15+
continue
16+
if graph[nx][ny] == 1:
17+
dfs(nx, ny)
18+
19+
20+
for i in range(t):
21+
m, n, k = map(int, si().split())
22+
graph = [[0] * m for _ in range(n)]
23+
for j in range(k):
24+
y, x = map(int, si().split())
25+
graph[x][y] = 1
26+
27+
ans = 0
28+
for a, row in enumerate(graph):
29+
for b, col in enumerate(row):
30+
if graph[a][b] == 1:
31+
dfs(a, b)
32+
# print("a = " + str(a) + ", b = " + str(b))
33+
ans += 1
34+
# print(graph)
35+
print(ans)

0 commit comments

Comments
 (0)