-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathForestFire.java
More file actions
63 lines (51 loc) · 1.73 KB
/
ForestFire.java
File metadata and controls
63 lines (51 loc) · 1.73 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
package com.company;
import java.util.Scanner;
public class ForestFire{
static int m,n,r,c;
static int temp[][];
static char mat[][];
public static void countTime(char mat[][], int temp[][], int n ,int row, int col, int count)
{
if((row>=n) || (row<0) || (col<0) || (col >= n) || (mat[row][col] == 'W')){
return;
}
if (temp[row][col] != 0 && count >= temp[row][col])
return;
temp [row][col] = count;
countTime(mat,temp,n,row+1,col,count+1 );
countTime(mat,temp,n,row-1,col,count+1 );
countTime(mat,temp,n,row,col+1,count+1 );
countTime(mat,temp,n,row,col-1,count+1 );
countTime(mat,temp,n,row+1,col+1,count+1 );
countTime(mat,temp,n,row+1,col-1,count+1 );
countTime(mat,temp,n,row-1,col+1,count+1 );
countTime(mat,temp,n,row-1,col-1,count+1 );
}
static int getTime(){
countTime(mat,temp,n,r-1,c-1,1);
int max = Integer.MIN_VALUE;
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j < temp[0].length; j++)
{
if(max <temp[i][j])
max = temp[i][j];
}
}
return max;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();
r = sc.nextInt();
c = sc.nextInt();
mat = new char[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
mat[i][j] = sc.next().charAt(0);
}
temp = new int[m][n];
System.out.println(getTime());
}
}
}