https://www.acmicpc.net/problem/21608
21608번: 상어 초등학교
상어 초등학교에는 교실이 하나 있고, 교실은 N×N 크기의 격자로 나타낼 수 있다. 학교에 다니는 학생의 수는 N2명이다. 오늘은 모든 학생의 자리를 정하는 날이다. 학생은 1번부터 N2번까지 번호
www.acmicpc.net
저는 각각의 빈 공간에 대해 근처에 좋아하는 학생이 몇명이 존재하는지, 빈칸은 몇개 존재하는지 확인하고 이를 비교하여 빈칸을 선택하여 해당 학생을 넣었습니다.
마지막으로 전체 배열을 돌면서 근처에 좋아하는 학생이 몇명있나 확인하고 점수를 합산하였습니다.
(처음에 빈칸을 찾으면서 점수를 계산하게 되면 초반에 추가된 학생들은 좋아하는 학생의 점수를 측정하기 힘들어져서 이렇게 구현하였습니다.)
package BOJ.Simulation;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_21608 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int[][] map = new int[n][n];
int[][] friend = new int[n*n+1][4];
for(int i = 0 ; i < n*n ; i++){
st = new StringTokenizer(br.readLine()," ");
int num = stoi.apply(st.nextToken());
for(int j = 0 ; j < 4; j++){
friend[num][j] = stoi.apply(st.nextToken());
}
position(map,num,friend[num],n);
}
int result = calValue(map,n,friend);
// print(map,n);
System.out.println(result);
}
private static final int[] SCORE = {0,1,10,100,1000};
private static void print(int[][] map, int n){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
System.out.print(map[i][j]+" ");
}
System.out.println();
}
}
private static int calValue(int[][] map, int n, int[][] friend) {
int result = 0;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
int num = map[i][j];
int[] cnt = checkValue(i,j,map,n,friend[num]);
result += SCORE[cnt[0]];
}
}
return result;
}
private static final int[] dy = {-1,0,1,0};
private static final int[] dx = {0,1,0,-1};
private static void position(int[][] map, int num, int[] friend,int n) {
int resultY = 0;
int resultX = 0;
int resultFriendCnt = -1;
int resultEmptyCnt = -1;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(map[i][j] != 0){
continue;
}
int[] cnt = checkValue(i,j,map,n,friend);
if(resultFriendCnt < cnt[0]){
resultFriendCnt = cnt[0];
resultEmptyCnt = cnt[1];
resultY = i;
resultX = j;
}else if(resultFriendCnt == cnt[0] && resultEmptyCnt < cnt[1]){
resultEmptyCnt = cnt[1];
resultY = i;
resultX = j;
}
}
}
map[resultY][resultX] = num;
}
private static int[] checkValue(int y, int x, int[][] map,int n,int[] friend) {
int friendCnt = 0;
int emptyCnt = 0;
for(int i = 0 ; i < 4 ; i++){
int ny = y + dy[i];
int nx = x + dx[i];
if(checkBound(ny,nx,n)){
if(map[ny][nx] == 0){
emptyCnt++;
continue;
}
for(int j = 0 ; j < 4 ; j++){
if(friend[j] == map[ny][nx]){
friendCnt++;
break;
}
}
}
}
return new int[] {friendCnt,emptyCnt};
}
private static boolean checkBound(int ny, int nx, int n) {
if(ny >= 0 && ny < n && nx >= 0 && nx < n){
return true;
}
return false;
}
}
'알고리즘' 카테고리의 다른 글
백준 1756번 피자 굽기 (0) | 2022.04.08 |
---|---|
백준 11967번 불켜기 (JAVA) (0) | 2022.04.07 |
백준 20058번 마법사 상어와 파이어스톰 (JAVA) (0) | 2022.04.05 |
백준 19237번 어른 상어 (JAVA) (0) | 2022.04.04 |
백준 17825번 주사위 윷놀이 (JAVA) (0) | 2022.04.03 |