문제
https://level.goorm.io/exam/195689/%EA%B5%AC%EB%A6%84-%EC%B0%BE%EA%B8%B0-%EA%B9%83%EB%B0%9C/quiz/1
구름LEVEL
난이도별 다양한 문제를 해결함으로써 SW 역량을 향상시킬 수 있습니다.
level.goorm.io
코드
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int board[][] = new int [n][n];
for(int i=0;i<n;i++){
st = new StringTokenizer(br.readLine());
for(int j=0;j<n;j++){
board[i][j] = Integer.parseInt(st.nextToken());
}
}
int dx [] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy [] = {1, 0, -1, 1, -1, 1, 0, -1};
int result = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(board[i][j] == 0){
int cnt = 0;
for(int t=0;t<8;t++){
int nx = i + dx[t];
int ny = j + dy[t];
if(nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if(board[nx][ny] == 1) cnt += 1;
}
if(cnt == k){
result+=1;
}
}
}
}
System.out.println(result);
}
}