代码:
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int x = 0 , t = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
{
t = -1;
}
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * t;
}
inline void write(int x)
{
if(x < 0)
{
putchar('-');
}
if(x > 9)
{
write(x / 10);
}
putchar(x % 10 + '0');
}
const int N = 200;
int n , m;
int sum;
char a[N][N];
int dx[5] = {0 , 1 , 0 , -1};
int dy[5] = {-1 , 0 , 1 , 0};
struct node
{
int x;
int y;
};
void bfs(int x , int y)
{
queue <node> q;
q.push({x , y });
a[x][y] = '.';
while(q.size())
{
node tmp = q.front();
int tx = tmp.x;
int ty = tmp.y;
q.pop();
for(int i = 0;i < 4; ++ i)
{
int ttx = tx + dx[i];
int tty = ty + dy[i];
if(ttx >= 1 && ttx <= n && tty >= 1 && tty <= m && a[ttx][tty] == '#')
{
q.push({ttx , tty });
a[ttx][tty] = '.';
}
}
}
}
int pd(int x , int y)
{
int tmp = 0;
if(a[x][y] == '#')
{
tmp ++;
}
if(a[x + 1][y] == '#')
{
tmp ++;
}
if(a[x][y + 1] == '#')
{
tmp ++;
}
if(a[x + 1][y + 1] == '#')
{
tmp ++;
}
if(tmp == 3)
{
return 0;
}
return 1;
}
int main()
{
n = read() , m = read();
for(int i = 1;i <= n; ++ i)
{
for(int j = 1;j <= m; ++ j)
{
cin >> a[i][j];
}
}
for(int i = 1;i <= n; ++ i)
{
for(int j = 0;j <= m; ++ j)
{
if(pd(i , j) == 0)
{
cout << "Bad placement.";
return 0;
}
}
}
for(int i = 1;i <= n; ++ i)
{
for(int j = 1;j <= m; ++ j)
{
if(a[i][j] == '#')
{
bfs(i , j);
sum++;
}
}
}
cout << "There are ";
write(sum);
cout << " ships.";
return 0;
}