Game Codeforces 354B, DP, memory search

Meaning of the questions: There is a character matrix, starting from the top left corner, alternating each person select a character. If the number is greater than the last character in a character b, then the first person to win, otherwise b win, otherwise average. Now the two sides do not turn on the water, and asked what the final result?

Thinking: This problem needs attention, the string choice is not a simple path, but includes all the paths and the string.

such as:

abc

trough

ccc

Ab string is actually (1, 2) and (2, 1) and, ABC is (1, 3), and (3, 1) and.

Because only 20 n maximum, then the diagonal length of the longest only 20, we can consider like pressure, to update the status by the state press, the transfer time to enumerate the 26 letters of transfer.

Code:

#include <bits/stdc++.h>
#define pii pair<int, int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 20;
vector<pii> a[maxn * 2];
int id[maxn][maxn];
bool v[40][1 << 20];
int dp[40][1 << 20];
char s[25][25];
int n;
int cal(char ch) {
	if(ch == 'a') return 1;
	else if(ch == 'b') return -1;
	return 0;
}
int dfs(int deep, int mask) {
	if(deep == n * 2 - 2) return 0;
	if(v[deep][mask]) return dp[deep][mask];
	int sz = a[deep].size(); 
	int year;
	if(deep & 1) ans = -INF;
	INF years = else;
	for (char c = 'a'; c <= 'z'; c++) {
		int add = cal(c);
		int Next_mask = 0;
		for (int j = 0; j < sz; j++) {
			if((mask >> j) & 1) {
				pii tmp = a[deep][j];
				int x = tmp.first, y = tmp.second;
				if(x < n - 1 && s[x + 1][y] == c)
					 Next_mask |= (1 << id[x + 1][y]);
				if(y < n - 1 && s[x][y + 1] == c)
					 Next_mask |= (1 << id[x][y + 1]);
			}
		}
		if(Next_mask) {
			if(deep & 1) ans = max(ans, add + dfs(deep + 1, Next_mask));
			else ans = min(ans, add + dfs(deep + 1, Next_mask));
		}
	}
	v[deep][mask] = 1;
	dp[deep][mask] = ans;
	return years;
}
int main () {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%s", s[i]);
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++) {
			id[i][j] = a[i + j].size();
			a[i + j].push_back(make_pair(i, j));
		}
	int years dfs = (0, 1);
	years + cal = (s [0] [0]);
	if(ans > 0) printf("FIRST\n");
	else if(ans < 0) printf("SECOND\n");
	else printf("DRAW\n");
} 

  

Guess you like

Origin www.cnblogs.com/pkgunboat/p/11110385.html