File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed
Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int n;
7+ static char [][] map;
8+ static boolean [][] vis;
9+ static int [] dx = {- 1 , 1 , 0 , 0 };
10+ static int [] dy = {0 , 0 , - 1 , 1 };
11+ static int area, perimeter;
12+
13+ public static void main (String [] args ) throws IOException {
14+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
15+ n = Integer . parseInt(br. readLine());
16+
17+ map = new char [n][n];
18+ vis = new boolean [n][n];
19+
20+ for (int i = 0 ; i < n; i++ ) {
21+ map[i] = br. readLine(). toCharArray();
22+ }
23+
24+ int maxArea = 0 ;
25+ int minPeri = Integer . MAX_VALUE ;
26+
27+ for (int i = 0 ; i < n; i++ ) {
28+ for (int j = 0 ; j < n; j++ ) {
29+ if (map[i][j] != ' #' || vis[i][j]) continue ;
30+
31+ area = 0 ;
32+ perimeter = 0 ;
33+ dfs(i, j);
34+
35+ if (area > maxArea || (area == maxArea && perimeter < minPeri)) {
36+ maxArea = area;
37+ minPeri = perimeter;
38+ }
39+ }
40+ }
41+
42+ System . out. println(maxArea + " " + minPeri);
43+ }
44+
45+ static void dfs (int x , int y ) {
46+ vis[x][y] = true ;
47+ area++ ;
48+
49+ for (int i = 0 ; i < 4 ; i++ ) {
50+ int nx = x + dx[i];
51+ int ny = y + dy[i];
52+
53+ if (nx < 0 || nx >= n || ny < 0 || ny >= n || map[nx][ny] == ' .' ) {
54+ perimeter++ ;
55+ continue ;
56+ }
57+
58+ if (map[nx][ny] == ' #' && ! vis[nx][ny]) {
59+ dfs(nx, ny);
60+ }
61+ }
62+ }
63+ }
64+ ```
You can’t perform that action at this time.
0 commit comments