forked from IElgohary/Uva-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11953 - Battleships.cpp
170 lines (154 loc) · 2.23 KB
/
11953 - Battleships.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*input
11
5
x....
x....
.....
.....
.....
6
....@.
.@...x
@.@@@.
@.....
@..@.@
.x.x..
8
@@@..@..
.....x..
..x..x.@
x..@x...
x.@..xx.
.......x
@@.....x
..x.@...
6
x@x...
......
......
......
......
......
8
.@x@@..@
@......x
@.x@..x.
x.....@.
.....@.x
x.x@@..x
.....x.@
x..x....
4
x.x.
x...
x...
..x@
8
...x.x..
.....@..
@x@@.@..
.....@..
.@..x.xx
.x......
..x.@x@@
@..x....
1
x
10
..@.@x..@.
..x.......
.@....@..x
..x.@.@..x
..x..x.@..
......x..@
...@.....@
@..@.....x
.@.x.....@
@.@.....x.
3
x..
...
@.@
5
...@.
..@..
.@...
.x.@.
.x...
*/
#include <stdio.h>
#include <cmath>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string.h>
#include <stack>
#include <map>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <functional>
using namespace std;
#define r(input) freopen("input.txt","r",stdin)
#define w(output) freopen("output.txt","w",stdout)
#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
#define FOR(a , b) for ( int i = a ; i < b ; i++ )
#define FOR2(a , b) for ( int j = a ; j < b ; j++ )
#define sz() size()
#define printArr(array,n) for(int i = 0 ; i < n ; i++) cout << array[i]<<" ";
#define FOREACH(l) for (auto it = l.begin(); it != l.end(); it++)
#define count(i) __builtin_popcount(i)
#define Sort(v) sort(v.begin(),v.end())
#define cover(a,d) memset(a,d,sizeof(a))
#define VISITED 1
#define UNVISITED 0
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
char grid[100][100];
int n;
int dx[] = {0,1,0, -1};
int dy[] = {1,0,-1, 0};
bool valid(int x , int y)
{
if(x >= n || x < 0 || y>=n || y<0 || grid[x][y] == '.' )
return false;
return true;
}
int floodfill(int x , int y)
{
if(!valid(x,y))
return 0;
int ans = 0;
if ( grid[x][y] == 'x')
ans = 1;
grid[x][y] = '.';
FOR(0,4)
{
int nxt = floodfill(x + dx[i], y + dy[i]);
ans = ans || nxt;
}
return ans;
}
int main()
{
int tc;
cin >> tc;
for ( int test = 1 ; test <= tc ; test++)
{
cin >> n;
FOR(0, n)
FOR2(0,n)
cin >> grid[i][j];
int ships = 0;
FOR(0,n)
FOR2(0,n)
if(grid[i][j] != '.')
ships += floodfill(i,j);
cout << "Case "<< test<<": "<< ships<< endl;
}
}