46pts,re,求调!
查看原帖
46pts,re,求调!
907430
corner_xiejunqi楼主2024/9/26 22:24

#include<bits/stdc++.h>
using namespace std;
int r,c;
const int N=1e3+10;
char g[N][N];
int dis[4][2]={0,-1,-1,0,1,0,0,1};
int dfs(int x,int y){
	g[x][y]='*';
	for(int i=0;i<4;i++){
		int xx=x+dis[i][0];
		int yy=y+dis[i][1];
		if(xx>0 && xx<=r && yy>0 && yy<=c && g[xx][yy]=='#'){
			dfs(xx,yy);
		}
	}
}
bool d(int i,int j){
	int a=0;
	if(g[i][j]=='#')a++;
	if(g[i+1][j]=='#') a++;
	if(g[i][j+1]=='#') a++;
	if(g[i+1][j+1]=='#') a++;
	if(a==3) return 0;
	return 1;
}
int i,j;
int main(){
	//1、声明变量,输入
	cin>>r>>c;
	for(i=1;i<=r;i++){
		for(j=1;j<=c;j++){
			cin>>g[i][j];
		}
	}
	//2、计算过程
	int ans=0;
	for(i=1;i<=r;i++){
		for(j=1;j<=c;j++){
			if(!d(i,j)){
				cout<<"Bad placement.";
				return 0;
			}
		}
	}
	for(i=1;i<=r;i++){
		for(j=1;j<=c;j++){
			if(g[i][j]=='#'){
				ans++;
				dfs(i,j);
			}
		}
	}
	//3、输出
	cout<<"There are "<<ans<<" ships.";
	return 0;
}
2024/9/26 22:24
加载中...