본문 바로가기
JAVA

구름톤 챌린지 9일차

by 동백05 2023. 8. 25.

https://level.goorm.io/exam/195691/%ED%8F%AD%ED%83%84-%EA%B5%AC%ED%98%84%ED%95%98%EA%B8%B0-2/quiz/1

 

구름LEVEL

난이도별 다양한 문제를 해결함으로써 SW 역량을 향상시킬 수 있습니다.

level.goorm.io

 

 

import java.util.Scanner;

class Main {
	public static String[][] bomb;
	public static int[][] check;
	public static int[] plusx = {0,-1,0,1,0};
	public static int[] plusy = {0,0,-1,0,1};
	public static int N;
	
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		int K = sc.nextInt();
		sc.nextLine();
		
		bomb = new String[N][N];
		check = new int[N][N];
		
		for(int i=0; i<N; i++){
			String thisline = sc.nextLine();
			bomb[i] = thisline.split(" ");
		}
		
		for(int i=0;i<K;i++){
			int x = sc.nextInt()-1;
			int y = sc.nextInt()-1;
			checkBomb(x,y);
			}
		
		int maxCount = -1;
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				maxCount = Math.max(maxCount,check[i][j]);
			}
		}
		
		System.out.println(maxCount);
	}
	
	public static void checkBomb(int x,int y){
		for(int a=0;a<5;a++){
			int dx = x+plusx[a];
			int dy = y+plusy[a];
			if(0<=dx && dx<N && 0<=dy && dy<N){
				if(!bomb[dx][dy].equals("#")){
					if(bomb[dx][dy].equals("@")){
						check[dx][dy]+=2;
					}else{
						check[dx][dy]+=1;
					}
				}
			}
		}
	}
}

원래는 약간 다른 방식으로 풀었었는데

for(int i=1; i<=N; i++){
			String thisline = sc.nextLine();
            for(int j=1; j<=N; j++){
				bomb[i][j] = thisline.split(" ")[j-1];
            }
		}

해당 방법은 몇몇 테스트 케이스에서 타임아웃이 생겼었다.

'JAVA' 카테고리의 다른 글

구름톤 챌린지 12일차 - 발전기  (0) 2023.08.29
구름톤 챌린지 11일차 - 통증 (2)  (0) 2023.08.28
구름톤 챌린지 8일차  (0) 2023.08.23
구름톤 챌린지 7일차  (0) 2023.08.22
구름톤 챌린지 2일차  (0) 2023.08.15

댓글