일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- IntelliJ
- Spring
- 알고리즘
- 코테
- Greedy
- DP
- 피보나치
- 데이터베이스
- join
- 이펙티브자바
- 우선순위큐
- Database
- 탐욕법
- 프로그래머스
- 깊이우선탐색
- 그리디알고리즘
- BFS
- select
- mariaDB
- Effective Java
- java
- 너비우선탐색
- 다이나믹프로그래밍
- DFS
- db
- 정렬
- 백준
- SQL
- springboot
- mybatis
Archives
- Today
- Total
땀두 블로그
[백준] 1780번 - 종이의 수 본문


dfs로 풀 수 있는 문제이다. 종이를 탐색하고 첫 인덱스와 값이 다른 경우 9등분하여 함수 호출하는 부분을 반복하여서 문제를 해결하였다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class p1780 {
public static int[][] ary;
public static boolean[][] visited;
public static int cnt;
public static int cnt1;
public static int cnt2;
public static int a;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
cnt = 0;
cnt1 = 0;
cnt2 = 0;
a = Integer.parseInt(br.readLine());
ary = new int[a][a];
visited = new boolean[a][a];
for (int i = 0; i < a; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < a; j++) {
ary[i][j] = Integer.parseInt(st.nextToken());
}
}
dfs(0, 0, a);
System.out.println(cnt);
System.out.println(cnt1);
System.out.println(cnt2);
}
public static void dfs(int x, int y, int size) {
// System.out.println(x + " " + y);
int n = ary[x][y];
int c = 0;
if (size < 1) {
return;
}
for (int i = x; i < x + size; i++) {
for (int j = y; j < y + size; j++) {
if (ary[i][j] != n) {
int s = size / 3;
dfs(x, y, s);
dfs(x, y + s, s);
dfs(x, y + 2 * s, s);
dfs(x + s, y, s);
dfs(x + s, y + s, s);
dfs(x + s, y + 2 * s, s);
dfs(x + s * 2, y, s);
dfs(x + s * 2, y + s, s);
dfs(x + s * 2, y + 2 * s, s);
c++;
return;
}
}
}
if (c == 0) {
if (n == -1) {
cnt++;
} else if (n == 0) {
cnt1++;
} else {
cnt2++;
}
}
return;
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 11727번 - 2*n 타일링 2 (0) | 2022.03.22 |
---|---|
[백준] 1080번 - 행렬 (0) | 2022.03.22 |
[백준] 1541번 - 잃어버린 괄호 (0) | 2022.03.22 |
[백준] 15650번 - N과 M (2) (0) | 2022.03.22 |
[백준] 2579번 - 계단 오르기 (0) | 2022.03.22 |
Comments