-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11953_battleships.cpp
More file actions
55 lines (52 loc) · 1 KB
/
Copy path11953_battleships.cpp
File metadata and controls
55 lines (52 loc) · 1 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
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
using namespace std;
#define size 101
#define valid(nx,ny) nx>=0 && nx<N && ny>=0 && ny<N
int N,T,Ans;
char Ground[size][size];
void readinput(){
cin >> N;
for (int i = 0; i < N; i++)
cin >> Ground[i];
}
int fx[] = { -1, 1, 0, 0 };
int fy[] = { 0, 0, -1, 1 };
void solve(int x, int y){
int nx, ny;
for (int i = 0; i < 4; i++){ // one way of solution
nx = x + fx[i];
ny = y + fy[i];
if (valid(nx,ny) && Ground[nx][ny]!='.'){
Ground[nx][ny] = '.';
solve(nx, ny);
}
}
}
void solveinput(){
int i, j;
Ans = 0;
for (i = 0; i < N; i++){
for (j = 0; j < N; j++){
if (Ground[i][j] == 'x'){
solve(i, j);
Ans++;
}
}
}
}
void printoutput(){
printf("Case %d: %d\n", T, Ans);
}
int main(){
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int t;
cin >> t;
for (T = 1; T <= t; T++){
readinput();
solveinput();
printoutput();
}
}