请大佬帮我看一下这题哪里有问题
  • 板块P1331 海战
  • 楼主Hyacinths
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/7/13 18:56
  • 上次更新2023/10/27 20:34:25
查看原帖
请大佬帮我看一下这题哪里有问题
561985
Hyacinths楼主2022/7/13 18:56

代码如下

#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x,y;
}q[1000001];
int n,m,ans,dir[4][2]={1,0,-1,0,0,1,0,-1}; 
char a[1010][1010],b[1010][1010];
bool check(int x,int y)
{
	int cnt=0;
	if(b[x][y]=='#') cnt++;
	if(b[x+1][y]=='#') cnt++;
	if(b[x][y+1]=='#') cnt++;
	if(b[x+1][y+1]=='#') cnt++;
	return cnt==4;
}
void bfs(int x,int y)
{
	int h=0,t=1;
	q[h].x=x;
	q[h].y=y;
	a[x][y]='.';
	while(h<t)
	{
		node now=q[h++];
		node nxt=now;
		for(int i=0;i<4;i++)
		{
			nxt.x=now.x+dir[i][0];
			nxt.y=now.y+dir[i][1];
			if(nxt.x<1||nxt.x>n||nxt.y<1||nxt.y>m||a[nxt.x][nxt.y]=='.')
				continue;
			else
			{
				a[nxt.x][nxt.y]='.';
				q[t++]=nxt;
			}
		}
	}
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
			b[i][j]=a[i][j];
		}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(check(i,j)==false)
			{
				cout<<"Bad placement.";
				return 0;
			}
			if(a[i][j]=='#')
			{
				bfs(i,j);
				ans++;
			}
		}	
	cout<<"There are "<<ans<<" ships.";
    return 0;
}

2022/7/13 18:56
加载中...