54求调,1 3 5 8 11 wrong answer
查看原帖
54求调,1 3 5 8 11 wrong answer
896447
Determined_Love_Act楼主2024/3/21 16:35
#include <iostream>
using namespace std;

int n, m;
bool a[1005][1005];
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int vis[1005][1005];

struct node {
	int x;
	int y;
};
node q[10005];

void bfs(int x, int y) {
	int head = 1;
	int tail = 1;
	q[tail].x = x;
	q[tail].y = y;
	tail ++;
	while (head < tail) {
		int x0 = q[head].x;
		int y0 = q[head].y;
		vis[x0][y0] = 1;
		for (int i = 0; i < 4; i++) {
			int nx = x0 + d[i][0];
			int ny = y0 + d[i][1];
			if (nx >= 0 && nx < n && ny >= 0 && ny < m && vis[nx][ny] != 1 && a[nx][ny] != 0) {
				q[tail].x = nx;
				q[tail].y = ny;
				tail++;
				vis[nx][ny] = 1;
			}
		}
		head++;
	}
}
char sep;
int main() {
	int ans = 0;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
	    for(int j = 0;j<m;j++){
		cin>>sep;
		if(sep == '#')a[i][j] = 1;
		if(sep == '.')a[i][j] = 0;
	        
	    }
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (a[i][j] != 0 && vis[i][j] == 0) {
				ans ++ ;
				bfs(i, j); 
			}
		}
	}
	if(ans == 0){
		cout<<"Bad placement.";
		return 0;
	}
	cout << "There are "<<ans<<" ships.";
	return 0;
}
2024/3/21 16:35
加载中...