forked from Azndarkslayer/Weekly-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsatpix.java
More file actions
89 lines (87 loc) · 2.3 KB
/
Copy pathsatpix.java
File metadata and controls
89 lines (87 loc) · 2.3 KB
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
import java.io.*;
import java.util.ArrayList;
/*
ID: albert.9
LANG: JAVA
TASK: satpix
*/
public class satpix{
//RECURSIVE
private static int evaluate(String[][] grid, int row, int col, String from){
int ans;
if(row >= grid.length ||
row < 0 ||
col >= grid[0].length ||
col < 0)
ans = 0;
else if(grid[row][col].equals(".")) ans = 0;
else{//grid[row][col].equals("*")
grid[row][col] = ".";
ans = 1;
if(from.equals("")){
ans += evaluate(grid, row + 1, col, "top") +
evaluate(grid, row - 1, col, "bottom") +
evaluate(grid, row, col + 1, "left") +
evaluate(grid, row, col - 1, "right");
}
else if(from.equals("top")){
ans += evaluate(grid, row + 1, col, "top") +
evaluate(grid, row, col + 1, "left") +
evaluate(grid, row, col - 1, "right");
}
else if(from.equals("bottom")){
ans += evaluate(grid, row - 1, col, "bottom") +
evaluate(grid, row, col + 1, "left") +
evaluate(grid, row, col - 1, "right");
}
else if(from.equals("left")){
ans += evaluate(grid, row + 1, col, "top") +
evaluate(grid, row - 1, col, "bottom") +
evaluate(grid, row, col + 1, "left");
}
else{
ans += evaluate(grid, row + 1, col, "top") +
evaluate(grid, row - 1, col, "bottom") +
evaluate(grid, row, col - 1, "right");
}
}
return ans;
}
private static int findMax(ArrayList<Integer> list){
int ans = 0;
if(! list.isEmpty()){
for(int x: list){
if(x > ans)
ans = x;
}
}
return ans;
}
public static void main(String[] args) throws IOException{
BufferedReader f = new BufferedReader(new FileReader("satpix.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("satpix.out")));
String[] nums = f.readLine().split(" ");
int w = Integer.parseInt(nums[0]);
int h = Integer.parseInt(nums[1]);
String[][] grid = new String[h][w];
String temp = "";
for(int i = 0; i < h; i++){
temp = f.readLine();
for(int j = 0; j < w; j++){
grid[i][j] = temp.substring(j,j+1);
}
}
ArrayList<Integer> sizes = new ArrayList<Integer>();
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(grid[i][j].equals("*")){
sizes.add(evaluate(grid, i, j, ""));
}
}
}
int ans = findMax(sizes);
out.println(ans);
out.close();
System.exit(0);
}
}