bfs,20分求助
查看原帖
bfs,20分求助
748239
OIbishop楼主2022/10/18 10:48
#include <bits/stdc++.h>
using namespace std;
int a[25][25] , ans = INT_MIN , dx[5] = {0 , 1 , 0 , -1 , 0} , dy[5] = {0 , 0 , 1 , 0 , -1} , t[25][25] , s[25][25];
bool f[25][25];
int n , m , k;
void bfs (int x , int y)
{
	queue < pair <int , int> > q;
	memset (f , false , sizeof (f));
	memset (t , false , sizeof (t));
	memset (s , false , sizeof (s));
	q.push (make_pair (x , y));
	f[x][y] = true;
	s[x][y] = a[x][y];
	t[x][y] = 1;
	if (a[x][y]) t[x][y]++;
	while (q.size ())
	{
		int xx = q.front ().first;
		int yy = q.front ().second;
		q.pop ();
		for (int i = 1; i <= 4; i++)
		{
			int xxx = xx + dx[i];
			int yyy = yy + dy[i];
			if (xxx >= 1 && xxx <= n && yyy >= 1 && yyy <= m && !f[xxx][yyy] && t[xx][yy] + abs (xxx - 1) <= k)
			{
				q.push (make_pair (xxx , yyy));
				f[xxx][yyy] = true;
				s[xxx][yyy] = s[xx][yy] + a[xxx][yyy];
				t[xxx][yyy] = t[xx][yy] + 1;
				if (a[xxx][yyy]) ++t[xxx][yyy];
				if (t[xx][yy] + abs (xxx - 1) <= k) ans = max (ans , s[xxx][yyy]);
			}
		}
	}
}
signed main ()
{
	cin >> n >> m >> k;
	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++)
	{
		bfs (1 , i);
	}
	cout << ans;
	return 0;
}

RT

2022/10/18 10:48
加载中...