求助!WA#10
查看原帖
求助!WA#10
116608
花满楼_落枫楼主2020/10/5 18:25

题目是P5198

以下为我的代码:

#include <bits/stdc++.h>
#define N 1010
#define f(U,S,E)	for(int U = S; U <= E; U++)

using namespace std;

struct rec	{	int x, y;	};
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int edge[N][N], ans1[N], ans2[N];
//int vis[N][N];
int la1 = -1, la2 = 0x3f3f3f3f;
int n, tot;

rec q[N*N];	int front, back;
//queue< pair< int, int > >	q;

void bfs(int x, int y)	{
	tot++;
	int a = 1, b = 0;
//	vis[x][y] = 1;
	edge[x][y] = 2;
//	q.push(make_pair(x, y));
	q[back].x = x;	q[back].y = y;	back++;
	while(back != front)
	{
//		int nx = q.front().first, ny = q.front().second;	q.pop();
//		int nx = q[front].x, ny = q[front].y;	front++;
		rec nxy = q[front];	front++;
		f(i, 0, 3)
		{
			int kx = nxy.x + dx[i], ky = nxy.y + dy[i];
			if(edge[kx][ky] == 0)	b++;
			if(kx >= 1 && ky >= 1 && kx <= n+2 && ky <= n+2 && edge[kx][ky] != 2)
			{
				if(edge[kx][ky] == 1)
				{
					a++;
//					q.push(make_pair(kx, ky));
					q[back].x = kx, q[back].y = ky;	back++;
//					vis[kx][ky] = 1;
					edge[kx][ky] = 2;
				}
			}
		}
		ans2[tot] += b;
		b = 0;
	}
	/*
	f(i, 1, n+2)
	{
		f(j, 1, n+2)
		{
			if(vis[i][j] == 1)	edge[i][j] = 0;
		}
	}
	*/
	ans1[tot] = a;
//	front = 0;	back = 0;
	if(la1 < ans1[tot])	la1 = ans1[tot], la2 = ans2[tot];
	if(la1 == ans1[tot] && la2 > ans2[tot])	la2 = ans2[tot];
}

int main()
{
	std::ios::sync_with_stdio(false);
	cin >> n;
	f(i, 1, n)
	{
		f(j, 1, n)
		{
			char s;
			cin >> s;
			if(s == '#')	edge[i+1][j+1] = 1;
			if(s == '.')	edge[i+1][j+1] = 0;
		}
	}
	f(i, 1, n+2)
	{
		f(j, 1, n+2)
		{
			if(edge[i][j] == 1)	bfs(i, j);
		}
	}
	cout << la1 << " " << la2 << endl;
	return 0;
}

开始我是用的STL的队列结果TLE#10(真·慢的一批),于是这个菜鸡就手动了队列,emmm....

TLE成WA了 (菜鸡连bfs裸题都不会) 。。。。

所以有大佬能可怜可怜这个蒟蒻帮帮忙吗。。。

2020/10/5 18:25
加载中...