91分求条
  • 板块P1331 海战
  • 楼主lyc_qwq
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/11/24 21:41
  • 上次更新2024/11/25 10:18:45
查看原帖
91分求条
1128671
lyc_qwq楼主2024/11/24 21:41

测试点

代码:

#include<bits/stdc++.h>

using namespace std;

inline int read()
{
	int x = 0 , t = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-')
		{
			t = -1;
		}
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * t;
}
inline void write(int x)
{
	if(x < 0)
	{
		putchar('-');
	}
	if(x > 9)
	{
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
const int N = 200;
int n , m;
int sum;
char a[N][N];
int dx[5] = {0 , 1 , 0 , -1};
int dy[5] = {-1 , 0 , 1 , 0};
struct node
{
	int x;
	int y;
};
void bfs(int x , int y)
{
	queue <node> q;
	q.push({x , y });
	a[x][y] = '.';
	while(q.size())
	{
		node tmp = q.front();
		int tx = tmp.x;
		int ty = tmp.y;
		q.pop();
		for(int i = 0;i < 4; ++ i)
		{
			int ttx = tx + dx[i];
			int tty = ty + dy[i];
			if(ttx >= 1 && ttx <= n && tty >= 1 && tty <= m && a[ttx][tty] == '#')
			{
				q.push({ttx , tty });
				a[ttx][tty] = '.';
			}	
		}	
	}
} 
int pd(int x , int y)
{
	int tmp = 0;
	if(a[x][y] == '#')
	{
		tmp ++;
	}
	if(a[x + 1][y] == '#')
	{
		tmp ++;
	}
	if(a[x][y + 1] == '#')
	{
		tmp ++;
	}
	if(a[x + 1][y + 1] == '#')
	{
		tmp ++;
	}
	if(tmp == 3)
	{
		return 0;
	}
	return 1;
}

int main()
{
    n = read() , m = read();
    for(int i = 1;i <= n; ++ i)
    {
    	for(int j = 1;j <= m; ++ j)
    	{
    		cin >> a[i][j];
		}
	}
	for(int i = 1;i <= n; ++ i)
	{
		for(int j = 0;j <= m; ++ j)
		{
			if(pd(i , j) == 0)
			{
				cout << "Bad placement.";
				return 0;
			}
		}
	}
	for(int i = 1;i <= n; ++ i)
	{
		for(int j = 1;j <= m; ++ j)
		{
			if(a[i][j] == '#')
			{
				bfs(i , j);
				sum++;
			}
		}
	}
	cout << "There are ";
	write(sum);
	cout << " ships.";
	return 0;
}
2024/11/24 21:41
加载中...