뚱땅뚱땅

[문제] SWEA 1861번 정사각형 방 본문

알고리즘/swea

[문제] SWEA 1861번 정사각형 방

양순이 2021. 2. 7. 11:00
728x90

* 출처: SWEA

swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LtJYKDzsDFAXc&categoryId=AV5LtJYKDzsDFAXc&categoryType=CODE&problemTitle=1861&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

내 생각

 

재귀

public class Solution {
	static int dx[] = { -1, 1, 0, 0 }; // 상하좌우
	static int dy[] = { 0, 0, -1, 1 };
	static int N;
	static int[][] room;
	static int max, maxRoom;
	static int cnt;

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		int T = Integer.parseInt(in.readLine());
		for(int tc = 1;tc<= T;tc++) {
			N = Integer.parseInt(in.readLine());
			room = new int[N][N];
			max = 0;
			for (int i = 0; i < N; i++) {
				String[] s = in.readLine().split(" ");
				for (int j = 0; j < N; j++) {
					room[i][j] = Integer.parseInt(s[j]);
				}
			}
			for(int i=0;i<N;i++) {
				for(int j=0;j<N;j++) {
					cnt= 1;
					findMax(i,j);
					if(max<cnt) {
						max = cnt;
						maxRoom = room[i][j];
					}else if(max == cnt) {
						maxRoom = Math.min(maxRoom, room[i][j]);
					}
				}
			}
			sb.append("#").append(tc).append(" ").append(maxRoom).append(" ").append(max).append("\n");
		}
		System.out.println(sb);
	}

	static boolean isInside(int x, int y) {
		if (x >= 0 && x < N && y >= 0 && y < N)
			return true;
		return false;
	}

	static void findMax(int x, int y) {
		for(int i=0;i<4;i++) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if(isInside(nx,ny)) {
				if (room[nx][ny] - room[x][y] == 1) {
					cnt++;
					findMax(nx, ny);
				}
			}
		}
	}
}
728x90
Comments