#include <bits/stdc++.h>
#define S 103
using namespace std;
int dirX[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dirY[] = {0, 1, 0, -1, 1, -1, -1, 1};
string battle[S];
int cnt, n;
void dfs(int i, int j){
if(i < 0 || j < 0 || i >= n || j >= n || battle[i][j] == '.')return;
battle[i][j] = '.';
for(int z = 0; z < 8; z++){
int px = i+dirX[z];
int py = j+dirY[z];
dfs(px, py);
}
}
int main(){
int t, cs = 0;
cin >> t;
while(t--){
cin >> n;
for(int i = 0; i < n; i++)cin >> battle[i];
cnt = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(battle[i][j] == 'x'){
cnt++;
dfs(i, j);
}
}
}
cout << "Case " << ++cs << ": " << cnt << endl;
}
return 0;
}
#define S 103
using namespace std;
int dirX[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dirY[] = {0, 1, 0, -1, 1, -1, -1, 1};
string battle[S];
int cnt, n;
void dfs(int i, int j){
if(i < 0 || j < 0 || i >= n || j >= n || battle[i][j] == '.')return;
battle[i][j] = '.';
for(int z = 0; z < 8; z++){
int px = i+dirX[z];
int py = j+dirY[z];
dfs(px, py);
}
}
int main(){
int t, cs = 0;
cin >> t;
while(t--){
cin >> n;
for(int i = 0; i < n; i++)cin >> battle[i];
cnt = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(battle[i][j] == 'x'){
cnt++;
dfs(i, j);
}
}
}
cout << "Case " << ++cs << ": " << cnt << endl;
}
return 0;
}
No comments:
Post a Comment