Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
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
Tags
more
Archives
Today
Total
관리 메뉴

main

[Array-10/Java] 봉우리 본문

Algorithm

[Array-10/Java] 봉우리

1984 2022. 11. 25. 21:19

* 자바(Java) 알고리즘 문제풀이 입문: 코딩테스트 대비

import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int N = Integer.parseInt(br.readLine());
		int[][] arr = new int[N][N];

		for (int i = 0; i < N; i++) {
			StringTokenizer stk = new StringTokenizer(br.readLine(), " ");
			for (int j = 0; j < N; j++) {
				arr[i][j] = Integer.parseInt(stk.nextToken());
			}
		}

		System.out.println(checkPeak(arr));
	}

	public static int checkPeak(int[][] arr) {
		int count = 0;

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

		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr.length; j++) {
				int check = 1;
				for (int k = 0; k < dx.length; k++) {
					int x = i + dx[k];
					int y = j + dy[k];
					if (x >= 0 && x < arr.length && y >= 0 && y < arr.length && arr[i][j] <= arr[x][y]) {
						check = 0;
					}
				}
				if (check == 1) {
					count++;
				}
			}
		}

		return count;
	}

}
728x90

'Algorithm' 카테고리의 다른 글

[백준-2407번/Java] 조합  (0) 2022.11.26
[백준-3036번/Java] 링  (0) 2022.11.23
[백준-1002번/Java] 터렛  (0) 2022.11.22
[백준-3053번/Java] 택시 기하학  (0) 2022.11.22
[백준-2477번/Java] 참외밭  (0) 2022.11.22
Comments