-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva11953.java
More file actions
58 lines (54 loc) · 1.56 KB
/
Copy pathUva11953.java
File metadata and controls
58 lines (54 loc) · 1.56 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
import java.util.* ;
import java.io.*;
/**
* Uva 11953 - Battleships
* @author mostafa
*/
public class Uva11953
{
static char [][] Grid ;
static int N ;
static int [] dx = { 1 , -1 , 0 , 0 };
static int [] dy = { 0 , 0 , 1 , -1 };
static boolean dfs(int i , int j)
{
boolean status = Grid[i][j] == 'x' ;
Grid[i][j] = '.' ;
for (int k = 0; k < 4; k++) {
int x = i+dx[k];
int y = j+dy[k];
if(isvalid(x, y))
status = dfs(x, y) || status ;
}
return status ;
}
static boolean isvalid(int i , int j)
{
if(i<0 || j < 0 || i >= N || j >= N || Grid[i][j] == '.' )
return false ;
return true ;
}
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
int tc = sc.nextInt();
for(int k = 1 ; k <= tc ; k++)
{
N = sc.nextInt();
Grid = new char[N][N];
int sum = 0 ;
for(int j = 0 ; j < N ; j++)
Grid[j] = sc.next().toCharArray();
for(int i = 0; i < N ; i++)
for (int j = 0; j < N ; j++)
if(Grid[i][j] != '.' && dfs(i, j))
sum++ ;
sb.append("Case ").append(k).append(": ").append(sum).append("\n");
}
pr.print(sb.toString());
pr.close();
sc.close();
}
}