/ CYOJ /

记录详情

Accepted


  
[Hydro](https://hydro.ac)提供评测服务
# 状态 耗时 内存占用
#1 Accepted 3ms 256.0 KiB
#2 Accepted 3ms 384.0 KiB
#3 Accepted 3ms 384.0 KiB
#4 Accepted 4ms 384.0 KiB
#5 Accepted 2ms 256.0 KiB
#6 Accepted 4ms 640.0 KiB
#7 Accepted 6ms 512.0 KiB
#8 Accepted 3ms 384.0 KiB
#9 Accepted 3ms 512.0 KiB
#10 Accepted 25ms 1.625 MiB

代码

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
int n,m,visited[1005][1005],count;
char a[1005][1005];
queue<int>q1,q2;
int t[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
void bfs(int sx,int sy) {
	//cout<<"1111111";
	q1.push(sx),q2.push(sy);
	visited[sx][sy] = 1;
	while(!(q1.empty() && q2.empty())) {
		int nx = q1.front(),ny = q2.front();
		q1.pop(),q2.pop();
		for(int i = 0; i < 4; i++) {
			int tx = t[i][0] + nx,ty = t[i][1] + ny;
			if(tx > 0 && tx <= n && ty > 0 && ty <= m && visited[tx][ty] == 0 && a[tx][ty] == '#') {
				q1.push(tx),q2.push(ty);
				visited[tx][ty] = 1;
			}
		}
	}
}

bool pd(int i,int j) {
	int cnt = 0;
	if(a[i][j] == '#') cnt++;
	if(a[i][j+1] == '#') cnt++;
	if(a[i+1][j] == '#') cnt++;
	if(a[i+1][j+1] == '#') cnt++;
	if(cnt == 3) return true;
	else return false;
}

int main() {
	cin>>n>>m;
	for(int i = 1; i <= n; i++) {
		scanf("%s",a[i] + 1);
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(pd(i,j)) {
				cout<<"Bad placement.";
				return 0;
			}
		}
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(visited[i][j] == 0 && a[i][j] == '#') {
				//cout<<i<<" "<<j<<endl;
				bfs(i,j);
				count++;
			}
		}
	}
	cout<<"There are "<<count<<" ships.";
}

信息

递交者
类型
递交
题目
P1012 海战
语言
C++
递交时间
2022-08-26 17:06:59
评测时间
2022-08-29 14:48:18
评测机
分数
100
总耗时
61ms
峰值内存
1.625 MiB