跳到正文

个人记录

iuhbvfdeisu

hhsyh_0929约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年2月27日
收录
2026年2月27日
更新
2026年11月16日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1
C++
#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int r,c,ans;
char a[N][N];
bool vis[N][N];
int dx[5]={0,0,1,-1};
int dy[5]={1,-1,0,0};
struct node{
	int x,y,step;
};
bool bfs(int hx,int hy){
	int x1=1e9,z1=1e9,x2=0,z2=0,s=0;
	queue<node>q;
	node tmp={hx,hy,0};
	q.push(tmp);
	vis[hx][hy]=true;
	while(!q.empty()){
		node cur=q.front();
		q.pop();
		x1=min(x1,cur.x);
		z1=min(z1,cur.y);
		x2=max(x2,cur.x);
		z2=max(z2,cur.y);
		s++;
		for(int i=0;i<=3;i++){
			int nx=cur.x+dx[i];
			int ny=cur.y+dy[i];
			if(nx>=1&&nx<=r&&ny>=1&&ny<=c&&vis[nx][ny]==false&&a[nx][ny]=='#'){
				node nxt={nx,ny,cur.step+1};
				q.push(nxt);
				vis[nx][ny]=true;
			}
		}
	}
	int sum=(x2-x1+1)*(z2-z1+1);
	if(sum==s)return true;
	return false;
}
int main(){
	cin>>r>>c;
	for(int i=1;i<=r;i++){
		for(int j=1;j<=c;j++){
			cin>>a[i][j];
		}
	}
	for(int i=1;i<=r;i++){
		for(int j=1;j<=c;j++){
			if(vis[i][j]==false && a[i][j]=='#'){
				if(bfs(i,j)==false){
					cout<<"Bad placement.";
					return 0;
				}
				ans++;
			}
		}
	}
	cout<<"There are "<<ans<<" ships.";
	return 0;
}
原文