1258 SW Expert 행렬찾기
2020. 2. 11. 12:13ㆍ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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Solution {
static int dx[] = {0,0,1,-1};
static int dy[] = {1,-1,0,0};
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcase = sc.nextInt();
for(int t = 1; t <= testcase; t++) {
n= sc.nextInt();
int arr[][] = new int [n][n];
boolean boo[][] = new boolean [n][n];
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++)
arr[i][j] = sc.nextInt();
}//endofarr
ArrayList<matrix> arra = new ArrayList<>();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(arr[i][j] != 0 && boo[i][j] == false) {
int nu[] = {1,1};
findout(arr,boo,i,j,nu,i);
arra.add(new matrix(nu[0],nu[1]));
}
}
}//endofarr
Collections.sort(arra);
System.out.print("#" + t + " " + arra.size());
for(matrix i : arra) {
System.out.print(" " + i.start + " " +i.end);
}
System.out.println();
}//end of testcase
}//end of main
public static int[] findout(int ar[][], boolean[][] bo, int row, int col, int num[],int row2) {
bo[row][col] = true;
for(int i = 0; i < 4; i++) {
int ro = row + dx[i];
int co = col + dy[i];
if(ro >= 0 && co >= 0 && ro < n && co < n) {
if(bo[ro][co] == true)
continue;
if(ar[ro][co] == 0)
continue;
if(ro == row+1) {
num[0]++;
return findout(ar,bo,ro,co,num,row2);
}
if(co == col+1 && ro == row2) {
num[1]++;
return findout(ar, bo, ro, co, num,row2);
}
return findout(ar, bo, ro, co, num,row2);
}
}
return num;
}
public static class matrix implements Comparable<matrix>{
int start,end;
public matrix(int start,int end) {
this.start = start;
this.end = end;
}
public int compareTo(matrix d) {
if(this.start*this.end > d.start*d.end) {
return 1;
}
else if(this.start*this.end < d.start*d.end) {
return -1;
}
else {
if(this.start < d.start) {
return -1;
}
return 0;
}
}
}
}//end of class
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' 카테고리의 다른 글
3289. 서로소 집합 - SWexpert (0) | 2020.02.13 |
---|---|
백준 4485 녹색 옷 입은 애가 젤다지? (0) | 2020.02.13 |
11054 가장 긴 바이토닉 부분 수열 (0) | 2020.02.11 |
2493 탑 (0) | 2020.02.11 |
1861 정사각형 방 (0) | 2020.02.10 |