/*
* @Description: To iterate is human, to recurse divine.
* @Autor: Recursion
* @Date: 2022-03-25 10:17:49
* @LastEditTime: 2022-03-26 10:38:45
*/
#include<bits/stdc++.h>
using namespace std;
const int N = 1001;
char a[N][N];
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int n,m,ans = 0;
void read()
{
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= m;j ++){
cin >> a[i][j];
}
}
bool cheak(int i,int j)
{
int cnt = 0;
if(a[i][j] == '#') cnt++;
if(a[i + 1][j] == '#') cnt++;
if(a[i][j + 1] == '#') cnt++;
if(a[i + 1][j + 1] == '#') cnt++;
if(cnt == 3) return false;
return true;
}
void dfs(int x,int y)
{
a[x][y] = '*';
for(int i = 0;i <= 3;i++){
if(x + dx[i] <= n&&x + dx[i] >=1&&y + dy[i] <= m&&y + dy[i] >= 1&&a[x + dx[i]][y + dy[i]] == '#')
dfs(x + dx[i],y + dy[i]);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
read();
// for(int i = 1;i <= n;i ++)
// for(int j = 1;j <= m;j ++){
// cout << a[i][j] << " ";
// }
for(int i = 1;i <= n;i ++)
for(int j = 1;j <=m;j ++)
if(!cheak(i,j)){
cout << "Bad placement." << endl;
return 0;
}
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= m;j ++){
if(a[i][j] == '#'){
ans++;
dfs(i,j);
}
}
// for(int i = 1;i <= n;i ++)
// for(int j = 1;j <= m;j ++){
// cout << a[i][j] << " ";
// }
cout << "There are "<< ans <<" ships."<< endl;
}
P1331 海战
最新推荐文章于 2025-07-30 14:22:39 发布