BZOJ 2973 入门OJ4798 石头游戏

矩阵递推

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define ll long long
#include <algorithm>
using namespace std;
int n, m, t, act;
int opt[2000], len[100];
char s[20][70];
struct Matrix {
	static const int N = 75;
	ll num[N][N];
	void clear() {
		memset(num, 0, sizeof(num));
	}
	void unit(){
		clear();
		for(int i = 0; i < N; i++) num[i][i] = 1ll;
	}
	/*void print() {
		for(int i = 0; i < 10; i++) {
			for(int j = 0; j < 10; j++) {
				printf("%d", num[i][j]);
			}
			cout<<endl;
		}
		cout<<endl;
	} */
};
Matrix operator * (const Matrix & a, const Matrix & b) {
	Matrix res;
	res.clear();
	for(int i = 0; i <= n * m; i++) {
		for(int j = 0; j <= n * m; j++) {
			for(int k = 0; k <= n * m; k++) {
				res.num[i][j] += a.num[i][k] * b.num[k][j];
			}
		}
	}
	return res;
}
Matrix operator ^ (Matrix a, ll k) {
	Matrix ans;
	ans.unit();
	while(k) {
		if(k & 1ll) ans = ans * a;
		a = a * a;
		k >>= 1;
	}
	return ans;
}
Matrix build(int k) {
	Matrix ans;
	ans.clear();
	ans.num[0][0] = 1ll;
	for(int i = 1; i <= n * m; i++) {
		int ind = opt[i], cur = k % len[ind];
		switch (s[ind][cur]) {
			case 'W' : if(i > 1) {ans.num[i][i - 1] = 1ll;}break;
			case 'E' : if(i < m * n) {ans.num[i][i + 1] = 1ll;}break;
			case 'N' : if(i > m) {ans.num[i][i - m] = 1ll;}break;
			case 'S' : if(i < m * n - m) {ans.num[i][i + m] = 1ll;}break;
			case 'D' : break;
			default : ans.num[0][i] = s[ind][cur] - '0';ans.num[i][i] = 1ll;break;
		}
	}
	return ans;
}
Matrix r1, r2, tmp;
int main() {
	cin >> n >> m >> t >> act;
	for(int i = 1; i <= n * m; i++) scanf("%1d", &opt[i]),opt[i]++;
	for(int i = 1; i <= act; i++) cin>>s[i],len[i] = strlen(s[i]);
	tmp.unit();
	for(int i = 0; i < 60; i++) {
		if(i == (t % 60)) r2 = tmp;
		tmp = tmp * build(i);
	}
	r1 = tmp;
	r1 = r1 ^ (t/60); 
	r1 = r1 * r2;
	ll ma = 0ll;
	for(int i = 1; i <= n * m; i++) ma = max(ma, r1.num[0][i]);
	cout<<ma<<endl;
	return 0;
}
posted @ 2018-03-13 18:51  Mr_Wolfram  阅读(237)  评论(0编辑  收藏  举报