-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa - 11953 - Battleships.cpp
More file actions
76 lines (65 loc) · 1.17 KB
/
UVa - 11953 - Battleships.cpp
File metadata and controls
76 lines (65 loc) · 1.17 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
#include<bits/stdc++.h>
using namespace std;
const int sz = 110;
char grid[sz][sz];
bool visited[sz][sz];
int t, n;
int cnt, ans;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int dfs(int i, int j)
{
int dum;
if(i<0 || j<0)
{
return 0;
}
if(i>=n || j>=n)
{
return 0;
}
if(visited[i][j])
{
return 0;
}
if(grid[i][j] == '.')
{
return 0;
}
visited[i][j] = true;
for(int k=0; k<4; k++)
{
dum = dfs(i+dx[k], j+dy[k]);
}
return 1;
}
int main()
{
freopen("_in.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> t;
while(t--)
{
cin >> n;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cin >> grid[i][j];
visited[i][j] = false;
}
}
ans = 0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(grid[i][j]=='x')
ans += dfs(i, j);
}
}
cout << "Case " << ++cnt << ": " << ans << "\n";
}
return 0;
}