본문 바로가기

구름톤 챌린지

구름톤 챌린지[JAVA] 9일차 학습 일기

구름톤 챌린지 폭탄 구현하기(2)

 

[느낀점]

7일차에 풀었던 8방탐색문제보다 쉬운버전인 4방탐색문제다. 8방탐색을 한번 풀어서 그런걸까 더 쉽게 느껴졌다. 하지만 여전히 코드는 depth가 깊어 스마트하지 못한 코드같다. 완전탐색문제를 dx/dy 테크닉으로 풀어야 될것같은데 여전히 어려워서 if문 도배로 해버렸다.

 

[결과 코드]

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());
		String[][] map = new String[N][N];
		int[][] score = new int[N][N];
		for(int i=0;i<N;i++) {
			st = new StringTokenizer(br.readLine(), " ");
			for(int j=0;j<N;j++) {
				map[i][j] = st.nextToken();
				score[i][j] = 0;
			}
		}
		
		int max = 0;
		for(int i=0;i<K;i++) {
			st = new StringTokenizer(br.readLine(), " ");
			int y = Integer.parseInt(st.nextToken()) - 1;
			int x = Integer.parseInt(st.nextToken()) - 1;
			if(y-1>=0){
				if(map[y-1][x].equals("@")){
					score[y-1][x]+=2;
				} else if (map[y-1][x].equals("0")){
					score[y-1][x]++;
				}
			}
			if(y+1<N){
				if(map[y+1][x].equals("@")){
					score[y+1][x]+=2;
				} else if (map[y+1][x].equals("0")){
					score[y+1][x]++;
				}
			}
			if(x-1>=0){
				if(map[y][x-1].equals("@")){
					score[y][x-1]+=2;
				} else if (map[y][x-1].equals("0")){
					score[y][x-1]++;
				}
			}
			if(x+1<N){
				if(map[y][x+1].equals("@")){
					score[y][x+1]+=2;
				} else if (map[y][x+1].equals("0")){
					score[y][x+1]++;
				}
			}
			if(map[y][x].equals("@")){
				score[y][x]+=2;
			} else if (map[y][x].equals("0")) {
				score[y][x]++;
			}
		}
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<N;j++) {
				if(score[i][j]>=max) {
					max=score[i][j];
				}
			}
		}
		System.out.println(max);
		
	}
}