题意:
给出一个方格阵,初始每个格子中都没有石头,然后每个格子都有一个操作序列,并且每时刻执行一个,循环执行,序列长度小于等于6,问T时刻后石头个数最多的格子中有几个石头。
因为操作序列长度小于等于6,所以每60次操作必有一次循环,所以可以对60次操作建一个矩阵,由60次操作相乘,矩阵的长宽均为方格矩阵的元素总数,操作均可以用矩阵中的数字来表示,其中0号点用来表示所有石头的源头,然后矩阵快速幂即可。
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(s) freopen(s".in", "r", stdin), freopen(s."out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 70 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }
int a[20][20], c[20][20] ;
ll f[N], d[N][N], e[N][N][N] ;
char b[20][20], s[100] ;
int n, m, t, q, p ;
ll ans ;
int id(int a, int b) {
return (a - 1) * m + b ;
}
void mulself(ll a[N][N], ll b[N][N]) {
ll c[N][N] ; clr(c) ;
rep(i, 1, p)
rep(k, 1, p)
if (a[i][k])
rep(j, 1, p)
c[i][j] += a[i][k] * b[k][j] ;
memcpy(a, c, sizeof(c)) ;
}
void mul(ll a[N], ll b[N][N]) {
ll c[N] ; clr(c) ;
rep(i, 1, p)
rep(j, 1, p)
c[i] += a[j] * b[j][i] ;
memcpy(f, c, sizeof(c)) ;
}
signed main(){
scanf("%d%d%d%d", &n, &m, &t, &q) ;
rep(i, 1, n) {
scanf("%s", s + 1) ;
rep(j, 1, m) a[i][j] = s[j] - '0' + 1 ;
}
rep(i, 1, q) scanf("%s", b[i]) ;
p = n * m + 1 ;
rep(k, 1, 60) {
rep(i, 1, n)
rep(j, 1, m) {
int x = a[i][j], y = c[i][j] ;
if (isdigit(b[x][y])) {
e[k][p][id(i, j)] = b[x][y] - '0' ;
e[k][id(i, j)][id(i, j)] = 1 ;
}
else if (b[x][y] == 'N' && i > 1) e[k][id(i, j)][id(i - 1, j)] = 1 ;
else if (b[x][y] == 'W' && j > 1) e[k][id(i, j)][id(i, j - 1)] = 1 ;
else if (b[x][y] == 'S' && i < n) e[k][id(i, j)][id(i + 1, j)] = 1 ;
else if (b[x][y] == 'E' && j < m) e[k][id(i, j)][id(i, j + 1)] = 1 ;
c[i][j] = (y + 1) % strlen(b[x]) ;
}
e[k][p][p] = 1 ;
}
memcpy(d, e[1], sizeof(e[1])) ;
rep(k, 2, 60) mulself(d, e[k]) ;
f[p] = 1 ;
for (int i = t / 60; i; i >>= 1) {
if (i & 1) mul(f, d) ;
mulself(d, d) ;
}
rep(i, 1, t % 60) mul(f, e[i]) ;
rep(i, 1, p - 1) ans = max(ans, f[i]) ;
printf("%lld\n", ans) ;
return 0 ;
}
/*
写代码时请注意:
1.ll?数组大小,边界?数据范围?
2.精度?
3.特判?
4.至少做一些
思考提醒:
1.最大值最小->二分?
2.可以贪心么?不行dp可以么
3.可以优化么
4.维护区间用什么数据结构?
5.统计方案是用dp?模了么?
6.逆向思维?
*/