91,样例11,求调
  • 板块P1331 海战
  • 楼主ken1006
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/18 14:36
  • 上次更新2025/1/18 17:08:43
查看原帖
91,样例11,求调
1387780
ken1006楼主2025/1/18 14:36
#include<iostream>
using namespace std;
char a[1010][1010] = {};
int dx[8] = {0,1,0,-1,1,1,-1,-1};
int dy[8] = {1,0,-1,0,1,-1,1,-1};
int n,m;
void dfs(int x,int y)
{
	a[x][y] = '.';
	for(int i = 0;i<=3;i++)
	{
		int nx = x+dx[i],ny = y+dy[i];
		if(nx<0 || ny<0 || nx>n || ny>m || a[nx][ny]!='#')continue;
		dfs(nx,ny);
	}
}
int main()
{
	cin>>n>>m;
	for(int i = 1;i<=n;i++)
		for(int j =1;j<=m;j++)
			cin>>a[i][j];
	int cnt = 0;
	for(int i = 1;i<=n;i++)
		for(int j = 1;j<=m;j++)
		{
			if(a[i][j] == '.')continue;
			int tmp = 0;
			for(int k = 0;k<8;k++)tmp+=(a[i+dx[k]][j+dy[k]] == '.');
			if(tmp == 1)
			{
				cout<<"Bad placement.";
				return 0;
			}
		}
	for(int i = 1;i<=n;i++)
		for(int j = 1;j<=m;j++)
		{
			if(a[i][j] == '.')continue;
			cnt++;
			dfs(i,j);
		}
	cout<<"There are ";
	cout<<cnt;
	cout<<" ships." ;
}
2025/1/18 14:36
加载中...