[Luogu P3190] [BZOJ 1187] [HNOI2007]神奇游乐园

版权声明:欢迎转载蒟蒻博客,但请注明出处: https://blog.csdn.net/LPA20020220/article/details/85682061

洛谷传送门

BZOJ传送门

题目描述

经历了一段艰辛的旅程后,主人公小P乘坐飞艇返回。在返回的途中,小P发现在漫无边际的沙漠中,有一块狭长的绿地特别显眼。往下仔细一看,才发现这是一个游乐场,专为旅途中疲惫的人设计。

娱乐场可以看成是一块大小为 n m n*m 的区域,且这个 n m n*m 的区域被分成 n m n*m 个小格子,每个小格子中就有一个娱乐项目。然而,小P并不喜欢其中的所有娱乐项目,于是,他给每个项目一个满意度。满意度为正时表示小P喜欢这个项目,值越大表示越喜欢。为负时表示他不喜欢,这个负数的绝对值越大表示他越不喜欢。为0时表示他对这个项目没有喜恶。

小P决定将飞艇停在某个小格中,然后每步他可以移动到相邻的上下左右四个格子的某个格子中。小P希望找一条路径,从飞艇所在格出发,最后又回到这个格子。

小P有一个习惯,从不喜欢浪费时间。因此,他希望经过每个格子都是有意义的:他到一个地方后,就一定要感受以下那里的惊险和刺激,不管自己是不是喜欢那里的娱乐项目。而且,除了飞艇所在格,其他的格子他不愿意经过两次。小P希望自己至少要经过四个格子。

在满足这些条件的情况下,小P希望自己玩过的娱乐项目的满意度之和最高。你能帮他找到这个最高的满意度之和吗?

输入输出格式

输入格式:

输入文件中的第一行为两个正整数 n n m m ,表示游乐场的大小为 n × m n×m 。因为这个娱乐场很狭窄,所以 n n m m 满足: 2 n 100 2 m 6 2\le n\le 100,2\le m\le 6 。接下来的 n n 行,每行有 m m 个整数,第 i i 行第 j j 列表示游乐场的第 i i 行第 j j 列的小格子中的娱乐项目的满意度,这个满意度的范围是 [ 1000 1000 ] [-1000,1000] 。同一行的两个整数之间用空格隔开。

输出格式:

输出文件中仅一行为一个整数,表示最高的满意度之和。

输入输出样例

输入样例#1:

4 4
100 300 -400 400
-100 1000 1000 1000
-100 -100 -100 -100
-100 -100 -100 1000

输出样例#1:

4000

解题分析

仍然比较板, 把累加改成取 m a x max 即可。

注意每次闭合路径的时候都可以更新答案。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <climits>
#include <algorithm>
#include <unordered_map>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define ll long long
std::unordered_map <int, int> ind;
int n, m, now, pre;
ll val[2][500500], ans = -LONG_LONG_MAX;
int mp[105][7], stat[2][500500], tot[2], dgt[10];
template <class T>
IN void in(T &x)
{
	static char c; static bool neg;
	x = 0; c = gc;
	for (; !isdigit(c); c = gc)
	if (c == '-') neg = true;
	for (;  isdigit(c); c = gc)
	x = (x << 1) + (x << 3) + c - 48;
	if (neg) neg = false, x = -x;
}
template <class T> IN T max(T a, T b) {return a > b ? a : b;}
template <class T> IN T min(T a, T b) {return a < b ? a : b;}
IN void insert(R int hs, R ll v)
{
	R int pos = ind[hs];
	if (!pos) pos = ind[hs] = ++tot[now], stat[now][pos] = hs, val[now][pos] = v;
	else val[now][pos] = max(val[now][pos], v);
}
int main(void)
{
	//freopen("1.out", "w", stdout);
	ll v;
	int i, j, k, l, st, lef, up, base, foo, bar;
	in(n), in(m); now = 0, pre = 1;
	for (i = 1; i <= n; ++i)
	for (j = 1; j <= m; ++j)
	in(mp[i][j]);
	for (i = 1; i <= m; ++i) dgt[i] = i << 1;
	tot[now] = 1;
	for (i = 1; i <= n; ++i)
	{
		for (j = 1; j <= m; ++j)
		{
			std::swap(now, pre);
			ind.clear(); tot[now] = 0;
			for (k = 1; k <= tot[pre]; ++k)
			{
				st = stat[pre][k], v = val[pre][k];
				lef = (st >> dgt[j - 1]) % 4, up = (st >> dgt[j]) % 4;
				base = st ^ (lef << dgt[j - 1]) ^ (up << dgt[j]);
				if (lef == 0)
				{
					if (up == 0)
					{
						insert(st, v);//00 -> 00
						if (j != m && i != n) insert(base | (1 << dgt[j - 1]) | (2 << dgt[j]), v + mp[i][j]);//00 -> 12
					}
					else if (up == 1)
					{
						if (i != n) insert(base | (1 << dgt[j - 1]), v + mp[i][j]);// 01 -> 10
						if (j != m) insert(st, v + mp[i][j]);// 01 -> 01
					}
					else
					{
						if (i != n) insert(base | (2 << dgt[j - 1]), v + mp[i][j]);// 02 -> 20
						if (j != m) insert(st, v + mp[i][j]);//02 -> 02
					}
				}
				else if (lef == 1)
				{
					if (up == 0)
					{
						if (i != n) insert(st, v + mp[i][j]);//10 -> 10
						if (j != m) insert(base | (1 << dgt[j]), v + mp[i][j]);//10 -> 01
					}
					else if (up == 1)//11 -> 00, first 2 -> 1
					{
						foo = 1;
						for (l = j + 1; l <= m; ++l)
						{
							bar = (st >> dgt[l]) % 4;
							if (bar == 1) ++foo;
							if (bar == 2) --foo;
							if (!foo) {insert(base - (1 << dgt[l]), v + mp[i][j]); break;}
						}
					}
					else {if ((!base) && ans < v + mp[i][j]) ans = v + mp[i][j];}
				}
				else
				{
					if (up == 0)
					{
						if (i != n) insert(st, v + mp[i][j]);//20 -> 20
						if (j != m) insert(base | (2 << dgt[j]), v + mp[i][j]);//20 -> 02
					}
					else if (up == 1) insert(base, v + mp[i][j]);//21 -> 00
					else//22 -> 00, first 1 -> 2
					{
						foo = 1;
						for (l = j - 2; ~l; --l)
						{
							bar = (st >> dgt[l]) % 4;
							if (bar == 1) --foo;
							if (bar == 2) ++foo;
							if (!foo) {insert(base + (1 << dgt[l]), v + mp[i][j]); break;}
						}
					}
				}
			}
		}
		for (l = 1; l <= tot[now]; ++l) stat[now][l] <<= 2;
	}
	printf("%lld", ans);
}

猜你喜欢

转载自blog.csdn.net/LPA20020220/article/details/85682061