-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForest.java
102 lines (84 loc) · 2.16 KB
/
Forest.java
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
92
93
94
95
96
97
98
99
100
101
102
import java.util.Scanner;
public class Forest {
static int count;
void isTrue(int x, int y, String [][] str,int m ,int n){
if((y+1)<n ){
if(str[x][y+1].equalsIgnoreCase("t")){
str[x][y+1]=Integer.toString(count);
}}
if((x-1)>=0 && (y-1)>=0){
if(str[x-1][y-1].equalsIgnoreCase("t")){
str[x-1][y-1]=Integer.toString(count);
}}
if((x-1)>=0){
if(str[x-1][y].equalsIgnoreCase("t")){
str[x-1][y]= Integer.toString(count);
}}
if(x-1>=0 && y+1<n){
if(str[x-1][y+1].equalsIgnoreCase("t")){
str[x-1][y+1]= Integer.toString(count);
}}
if(y-1>=0 ){
if(str[x][y-1].equalsIgnoreCase("t")){
str[x][y-1]= Integer.toString(count);
}}
if(x+1<m && y-1>=0){
if(str[x+1][y-1].equalsIgnoreCase("t")){
str[x+1][y-1]=Integer.toString(count);
}}
if((x+1)<m && (y+1)<n){
if(str[x+1][y+1].equalsIgnoreCase("t")){
str[x+1][y+1]= Integer.toString(count);
}}
if(x+1<m){
if(str[x+1][y].equalsIgnoreCase("t")){
str[x+1][y]= Integer.toString(count);
}
}
}
public static void main(String[] args) {
Forest f=new Forest();
Scanner scan=new Scanner(System.in);
int m=scan.nextInt();
int n=scan.nextInt();
int x=scan.nextInt();
int y=scan.nextInt();
String str[][]=new String[m][n];
if(3<=m && m<=20 && 3<=n && n<=20 )
{
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
str[i][j]=scan.next();
}
}
str[x-1][y-1]=Integer.toString(++count);
count++;
--x;
--y;
f.isTrue(x, y, str, m, n);
count++;
int flag;
do{
flag = 1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(str[i][j].equalsIgnoreCase(Integer.toString(count-1))){
f.isTrue(i, j, str, m, n);
}
}
}
count++;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(flag == 1){
if(str[i][j].equalsIgnoreCase("t")) {
flag=0;
}
}
}
}
} while(flag==0);
System.out.println(--count);
}
}
}