萌新刚学 OI 一秒, 插头dp只有40分, 求助
查看原帖
萌新刚学 OI 一秒, 插头dp只有40分, 求助
261773
youwike楼主2021/8/4 10:49

不知道哪里有问题 link

#include <iostream>
#include <vector>
#include <map>
#include <string>
#define ll long long
#define int long long

using namespace std;

const int N = 14;


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	map<int, ll> f[2];
	int n, m;
	int ex, ey;
	string s;
	cin >> n >> m;
	vector<vector<int> > a(n + 2, vector<int>(m + 2, 0));
	vector<int> inc(m + 2);
	for (int i = 1; i <= n; ++i) {
		cin >> s;
		for (int j = 1; j <= m; ++j) {
			if (s[j - 1] == '*') a[i][j] = 0;
			else a[i][j] = 1, ex = i, ey = j;
		}
	}
	cout << ex << ' ' << ey << endl;
	for (int i = 1; i <= m + 1; ++i) {
		inc[i] = 1 << (2 * (i - 1));
	}
	ll ans = 0;
	f[0][0] = 1;
	int cur = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cur ^= 1;
			f[cur].clear();
			for (auto tmp : f[cur ^ 1]) {
				int st = tmp.first;
				int l = (st >> (2 * (j - 1))) & 3;
				int u = (st >> (2 * j)) & 3;
				if (!a[i][j]) {
					if (!l && !u) f[cur][st] += f[cur ^ 1][st];
				}
				else {
					if (!l && !u) {
						if (a[i + 1][j] && a[i][j + 1]) {
							f[cur][st + inc[j] + inc[j + 1] * 2] += f[cur ^ 1][st];
						}
					}
					else if (!l && u) {
						if (a[i + 1][j]) 
							f[cur][st + inc[j] * u - inc[j + 1] * u] += f[cur ^ 1][st];
						if (a[i][j + 1])
							f[cur][st] += f[cur ^ 1][st];
					}
					else if (l && !u) {
						if (a[i][j + 1])
							f[cur][st - inc[j] * l + inc[j + 1] * l] += f[cur ^ 1][st];
						if (a[i + 1][j])
							f[cur][st] += f[cur ^ 1][st];
					}
					else if (l == 1 && u == 1) {
						for (int k = j + 2; k <= m + 1; ++k) {
							int cnt = 0;
							if (((st >> (2 * (k - 1))) & 3) == 2)
								--cnt;
							if (((st >> (2 * (k - 1))) & 3) == 1)
								++cnt;
							if (cnt == -1) {
								f[cur][st - inc[k] * 2 + inc[k] - inc[j] - inc[j + 1]] += f[cur ^ 1][st];
								break;
							}
						}
					}
					else if (l == 2 && u == 2) {
						for (int k = j - 1; k; --k) {
							int cnt = 0;
							if (((st >> (2 * (k - 1))) & 3) == 1) --cnt;
							if (((st >> (2 * (k - 1))) & 3) == 2) ++cnt;
							if (cnt == -1) {
								f[cur][st - inc[k] + inc[k] * 2 - inc[j] * 2 - inc[j + 1] * 2] += f[cur ^ 1][st];
								break;
							}
						}
					}
					else if (l == 2 && u == 1) {
						f[cur][st - inc[j] * 2 - inc[j + 1]] += f[cur ^ 1][st];
					}
					else if (l == 1 && u == 2) {
						if (i == ex && j == ey) ans += f[cur ^ 1][st];
					}
				}
			}
		}
		map<int, ll> t;
		for (auto tmp : f[cur]) {
			int st = tmp.first;
			t[st << 2] = tmp.second;
		}
		swap(t, f[cur]);
	}
	cout << ans << endl;
	return 0;
}
2021/8/4 10:49
加载中...