82分求助!
  • 板块P1331 海战
  • 楼主Abel1755
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/10/30 16:45
  • 上次更新2023/10/27 04:50:53
查看原帖
82分求助!
689265
Abel1755楼主2022/10/30 16:45

dalao帮忙看看这怎么改

#include <bits/stdc++.h>

using namespace std;

int r, c, cnt = 0, dir[4][2] = {{0, 0}, {0, +1}, {+1, 0}, {+1, +1}}, dirr[4][2] = {{0, +1}, {+1, 0}, {-1, 0}, {0, -1}};
char Map[1005][1005];
bool b[1005][1005];

struct node
{
	int x, y;
	node() {}
	node(int x_, int y_): x(x_), y(y_) {}
};

int Bad_placement(int x, int y)
{
	int s = 0;
	for (int i = 0; i < 4; i++)
		if (Map[x + dir[i][0]][y + dir[i][1]] == '#')
			s ++;
	return (s == 3);
}

int bfs(int i, int j)
{
	int _ = 0;
	
	queue <node> q;
	
	q.push(node(i, j));
	b[i][j] = 1;
	
	while (!q.empty())
	{
		node n = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i++)
		{
			int tx, ty;
			tx = n.x + dirr[i][0];
			ty = n.y + dirr[i][1];
			
			if (Map[tx][ty] == '#' && b[tx][ty] == 0)
			{
				b[tx][ty] = 1;
				_ ++;
				q.push(node(tx, ty));
			}
		}
	}
	return (_ > 0);
}

int main()
{
	cin >> r >> c;
	
	for (int i = 1; i <= r; i++)
	{
		cin >> Map[i];
	}
	
	for (int i = 1; i <= r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			if (Bad_placement(i, j))
			{
				cout << "Bad placement.";
				return 0;
			}
		}
	}
	
	for (int i = 1; i <= r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			if (bfs(i, j))
				cnt ++;
		}
	}
	
	cout << "There are " << cnt << " ships.";

 	return 0;
}
2022/10/30 16:45
加载中...