1861 정사각형 방
2020. 2. 10. 17:55ㆍLearn/Algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package com.java.first;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int dx[] = {0,0,-1,1};
static int dy[] = {1,-1,0,0};
static int N = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int testcase = sc.nextInt();
for(int t= 1; t<= testcase; t++) {
int max_index = Integer.MAX_VALUE;
int maxim = 0;
int temp = 0;
N = sc.nextInt();
int arr[][] = new int[N][N];
for(int i = 0; i <N; i++)
for(int j = 0; j < N; j++)
arr[i][j] = sc.nextInt();
for(int i = 0; i <N; i++)
for(int j = 0; j < N; j++) {
temp = findout(arr,i,j,arr[i][j],1);
if(temp > maxim) {
maxim = temp;
max_index = arr[i][j];
}
if(temp == maxim) {
max_index = Math.min(max_index, arr[i][j]);
}
}
System.out.println("#" + t + " " + max_index + " " + maxim);
}
}
public static int findout(int ar[][],int row,int col, int num,int cnt) {
for(int i = 0; i < 4; i++) {
if(row+dx[i] >= 0 && row+dx[i] < N && col+dy[i] >=0 && col+dy[i] < N)
if(ar[row + dx[i]][col + dy[i]] == num+1) {
return findout(ar,row+dx[i],col+dy[i],num+1,cnt+1);
}
}
return cnt;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
재귀를 이용해서 탐색하여 결과값을 리턴하도록 하였다.
재귀에 뭔가 익숙해지는 거 같아 기분이 좋다.
'Learn > Algorithm' 카테고리의 다른 글
11054 가장 긴 바이토닉 부분 수열 (0) | 2020.02.11 |
---|---|
2493 탑 (0) | 2020.02.11 |
1828 냉장고 (0) | 2020.02.10 |
1873. 상호의 배틀필드 (0) | 2020.02.10 |
4408 자기방으로 돌아가기 (0) | 2020.02.10 |