求助大佬! bfs90分,#5能算出正确答案,但是不开O2就超时
查看原帖
求助大佬! bfs90分,#5能算出正确答案,但是不开O2就超时
478755
Karis楼主2021/12/8 21:38
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

int n, m, h[501][501], is[501];
vector<int> a[501];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};

struct node{
	int x, y;
};

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	while(ch > '9' || ch < '0')
	{
		if(ch == '-')
			f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * f;
}

inline void init()
{
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
			h[i][j] = read();	
}

void bfs(int x, int y)
{
	queue<node> q;
	q.push({x,y});
	int book[501][501] = {0};
	while(!q.empty())
	{
		node b = q.front();
		q.pop();
		if(book[b.x][b.y])
			continue;
		book[b.x][b.y] = 1;
		if(b.x == n)
			a[y].push_back(b.y);
		for(int i = 0; i < 4; ++i)
		{
			int xx = b.x+dx[i], yy = b.y+dy[i];
			if(xx > 0 && xx <= n && yy > 0 && yy <= m && h[b.x][b.y] > h[xx][yy])
				q.push({xx, yy});
		}
	}
}

inline void work()
{
	for(int i = 1; i <= m; ++i)
		for(int j = 0; j < a[i].size(); ++j)
			is[a[i][j]]++;
	int tot = 0;
	for(int i = 1; i <= m; ++i)
		if(!is[i])
			tot++;
	if(tot)
		cout << 0 << endl << tot << endl;
	else
	{
		tot = m;
		for(int i = 1; i <= m; ++i)
		{
			int flag = 1;
			for(int j = 0; j < a[i].size(); ++j)
				if(is[a[i][j]] <= 1)
				{
					flag = 0;
					break;
				}
			if(flag)
			{
				for(int j = 0; j < a[i].size(); ++j)
					is[a[i][j]]--;
				tot--;
			}
		}
		cout << 1 << endl << tot << endl;
	}
}

int main()
{
	init();
	for(int i = 1; i <= m; ++i)
		bfs(1,i);
	work();
	return 0;
}

 
2021/12/8 21:38
加载中...