-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMountainTop.java
More file actions
62 lines (58 loc) · 1.99 KB
/
MountainTop.java
File metadata and controls
62 lines (58 loc) · 1.99 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package algorithm.array;
import java.util.Scanner;
/**
* 지도 정보가 N*N 격자판에 주어집니다. 각 격자에는 그 지역의 높이가 쓰여있습니다.
* 각 격자 판의 숫자 중 자신의 상하좌우 숫자보다 큰 숫자는 봉우리 지역입니다.
* 봉우리 지역이 몇 개 있는 지 알아내는 프로그램을 작성하세요.
* 격자의 가장자리는 0으로 초기화 되었다고 가정한다.
* 만약 N=5 이고, 격자판의 숫자가 다음과 같다면 봉우리의 개수는 10개입니다.
* ▣ 입력설명
* 첫 줄에 자연수 N이 주어진다.(2<=N<=50)
* 두 번째 줄부터 N줄에 걸쳐 각 줄에 N개의 자연수가 주어진다. 각 자연수는 100을 넘지 않는 다.
* ▣ 출력설명
* 봉우리의 개수를 출력하세요.
* ▣ 입력예제 1
* 5
* 53723
* 37161
* 72534
* 43641
* 87352
* ▣ 출력예제 1 10
*/
class MountainTop {
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, 1, 0, -1};
public static void main(String[] args) {
MountainTop T = new MountainTop();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.print(T.solution(n, arr));
}
public int solution(int n, int[][] arr) {
int answer = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
boolean flag = true;
for (int k = 0; k < 4; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (nx >= 0 && nx < n && ny >= 0 && ny < n && arr[nx][ny] >= arr[i][j]) {
flag = false;
break;
}
}
if (flag) {
answer++;
}
}
}
return answer;
}
}