大佬救命,蒟蒻民间数据后两点 1.2s 超时。
查看原帖
大佬救命,蒟蒻民间数据后两点 1.2s 超时。
768692
InoriILU楼主2023/2/1 16:02

虽然题解与讨论版里都有提到前缀和做法,但是还是不太清楚实现方法,求解qwq

#include <iostream>

const int mod{ 998244353 };

class Plant
{
private:
	const int high;
	const int wide;
	bool ** ground;

public:
	Plant(int n, int m) : high(n), wide(m)
	{
		auto temp = new bool * [n];
		for (int i = 0; i < n; i++)
		{
			temp[i] = new bool[m];
		}
		ground = temp;
		//cout << high << " " << wide << endl;
	};
	~Plant()
	{
		for (int i = 0; i < high; i++)
		{
			delete [] ground[i];
		}
		delete [] ground;
	};
	void input();
	int right(int x, int y);
	int up(int x, int y);
	int down(int x, int y);
};

void Plant::input()
{
	char temp[wide];
	for (int i = 0; i < high; ++i)
	{
		std::cin >> temp;
		for (int j = 0; j < wide; ++j)
		{
			ground[i][j] = temp[j] - '0';
		}
	}
};

int Plant::right(int x, int y)
{
	if (ground[x][y])
		return 0;
	else if (y == wide - 1)
		return 1;
	else
		return this->right(x, y + 1) + 1;
};

int Plant::up(int x, int y)
{
	if (x == 0 || x == 1)
		return 0;
	else if (ground[x - 2][y] || ground[x - 1][y] || ground[x][y])
		return 0;
	else
		return this->up(x - 1, y) + this->right(x - 2, y) - 1;
};

int Plant::down(int x, int y)
{
	if (ground[x][y])
		return 0;
	else if (x == high - 1)
		return 1;
	else
		return this->down(x + 1, y) + 1;
};

int main()
{
	using namespace std;
	//freopen("plant.in", "r", stdin);
	//freopen("plant.out", "w", stdout);
	int T, ID, high, wide, c, f;
	cin >> T >> ID;
	long long cans{}, fans{};
	for (int i = 0; i < T; ++i)
	{
		cin >> high >> wide >> c >> f;
		Plant temp(high, wide);
		temp.input();
		for (int i = 2; i < high; ++i)
		{
			for (int j = 0; j < wide - 1; ++j)
			{
				int t_up = temp.up(i, j);
				int t_right = temp.right(i, j) - 1;
				int t_down = temp.down(i, j) - 1;
				cans += t_up * t_right;
				fans += t_up * t_right * t_down;
			}
		}
		cout << cans * c % mod << " " << fans * f % mod << endl;
		cans = fans = 0;
	}
	//system("pause");
};
2023/2/1 16:02
加载中...