poj3620

这是一道简单题,但是我错了n次。

开数组的时候一定要注意范围,不要直接maxn;

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int maxn = 101;

struct point
{
	int x, y;
}cell[maxn * maxn];

int n, m, k;
int dir[4][2] = {{1, 0},{-1, 0},{0, 1},{0, -1}};
int map[maxn][maxn];
bool vis[maxn * maxn];

void init()
{
	memset(map, -1, sizeof(map));
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < k; i++)
	{
		scanf("%d%d", &cell[i].x, &cell[i].y);
		cell[i].x--;
		cell[i].y--;
		map[cell[i].x][cell[i].y] = i;
	}
}

bool ok(point &a)
{
	if (a.x < 0 || a.y < 0 || a.y >= m || a.x >=n)
		return false;
	if (map[a.x][a.y] == -1)
		return false;
	if (vis[map[a.x][a.y]])
		return false;
	return true;
}

int bfs(point &s)
{
	int front = 0, rear = 0;
	point q[maxn * maxn];
	int r = 1;

	vis[map[s.x][s.y]] = true;
	q[front++] = s;
	while (front != rear)
	{
		point b = q[rear++];
		if (rear == maxn * maxn)
			rear = 0;
		for (int i = 0; i < 4; i++)
		{
			point a;
			a.x = b.x + dir[i][0];
			a.y = b.y + dir[i][1];
			if (ok(a))
			{
				q[front++] = a;
				if (front == maxn * maxn)
					front = 0;
				r++;
				vis[map[a.x][a.y]] = true;
			}
		}
	}
	return r;
}

int main()
{
	int ans = 0;
	//freopen("D:\\t.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &k);
	init();
	for (int i = 0; i < k; i++)
		if (!vis[i])
		{
			int temp = bfs(cell[i]);
			if (ans < temp)
				ans = temp;
		}
	printf("%d\n", ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值