본문 바로가기
2024-?학기 ???/Solving

SWEA 16268_풍선팡2

by 껐다 켜보셨어요? 2024. 2. 29.
// TC IN
3
3 5
2 1 1 2 2 
2 2 1 2 2 
2 2 1 1 2 
5 5
3 4 1 2 3 
3 4 1 3 2 
2 3 2 4 1 
1 4 4 1 3 
2 2 3 4 4 
5 8
1 3 4 4 4 4 3 3 
4 1 2 4 3 1 4 4 
4 1 4 4 1 4 2 1 
3 2 4 2 1 1 2 1 
4 4 1 4 4 2 2 2

// TC OUT
#1 8
#2 16
#3 17
import java.util.Scanner;

public class Solution {
	public static Scanner sc = new Scanner(System.in);
	public static int loop;
	public static int maxPop;
	
	public static void main(String[] args) {
		loop = sc.nextInt();
		for(int i = 0; i<loop; i++) {
			maxPop = 0;
			int[][] board = setting(new int[sc.nextInt()][sc.nextInt()]);
			pop(board);
			System.out.printf("#%d %d\n", i+1, maxPop);
		}
	}
	
	public static int[][] setting(int[][] board) { // 배열 채우는 함수 
		for(int j = 0; j<board.length; j++) {
			for(int k = 0; k<board[0].length; k++) {
				board[j][k] = sc.nextInt();
			}
		} // 배열 채우기 
		return board;
	}
	
	public static void pop(int[][] board) { // main logic
		
		int[] dx = new int[]{0, 1, 0, -1}; // col에 더함
		int[] dy = new int[]{-1, 0, 1, 0}; // row에 더함
		for(int r = 0; r<board.length; r++) {
			for (int c = 0; c < board[0].length; c++) {
				int sum = board[r][c];
				for (int i = 0; i < 4; i++) {
					if (r + dy[i] >= 0 && r + dy[i] < board.length && c + dx[i] >= 0 && c + dx[i] < board[0].length)
						sum += board[r+dy[i]][c+dx[i]];
					else continue;
				}
				if(sum>maxPop) maxPop = sum;
			}
		}
	}
}

 

처음에 제출했을 때 어이없게 틀리고 말았는데

(TC 12개 중 10개 맞음)

왜냐면 TC별 max를(최종 출력되는 그거)  초기화하지 않아서 그랬다. 

원래 루프마다 새로 만들어서 쓰던 걸 static으로 쓰려니까 초기화하는 걸 자꾸 까먹음

ㅋㅋㅋㅋㅋ

 

 

 

 

 

 

 

 

 

 

 

 

'2024-?학기 ??? > Solving' 카테고리의 다른 글

BOJ 1780_종이의 개수  (0) 2024.03.03
BOJ 17478_재귀함수가 뭔가요?  (0) 2024.03.03
SWEA 5215_햄버거 다이어트  (0) 2024.02.29
SWEA 2817_부분 수열의 합  (1) 2024.02.27
SWEA 2817_부분 수열의 합(재귀)  (0) 2024.02.27

댓글