내 답-> 맞음
import java.util.*;
public class P2_10 {
public static int solution(int n,int[][] arr) {
int count=0;
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++){
int mount = arr[i][j];
if(mount>arr[i-1][j]&&
mount>arr[i][j-1]&&
mount>arr[i][j+1]&&
mount>arr[i+1][j])
count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int[][] arr = new int[n+2][n+2];
for(int i =1;i<=n;i++){
for(int j=1;j<=n;j++){
int input = kb.nextInt();
arr[i][j]=input;
}
}
System.out.println(solution(n,arr));
}
}
✔ 강의 답
import java.util.*;
public class P2_10 {
public static int solution(int n,int[][] arr) {
int count=0;
int[] kx ={-1,0,0,1};
int[] ky ={0,-1,1,0};
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++){
int mount = arr[i][j];
boolean answer = true;
for(int k=0;k<4;k++){
int nx = i+kx[k];
int ny = j+ky[k];
if(mount<=arr[nx][ny]) {
answer=false;
break;
} //if
}if(answer==true) count++;
}
}
return count;
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int[][] arr = new int[n+2][n+2];
for(int i =1;i<=n;i++){
for(int j=1;j<=n;j++){
int input = kb.nextInt();
arr[i][j]=input;
}
}
System.out.println(solution(n,arr));
}
}