大概就是一个形状要嵌到矩阵里头这样子,看看代码就明白了qwq
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int n, m, k, lim=1, limcnt, rev[8388655];
char ss[5000005];
const double PI=acos(-1.0);
struct Complex{
double x, y;
Complex(double u=0.0, double v=0.0){
x = u; y = v;
}
Complex operator+(const Complex &u)const{
return Complex(x+u.x, y+u.y);
}
Complex operator-(const Complex &u)const{
return Complex(x-u.x, y-u.y);
}
Complex operator*(const Complex &u)const{
return Complex(x*u.x-y*u.y, x*u.y+y*u.x);
}
}a[8388655], b[8388655];
void fft(Complex a[], int opt){
for(int i=0; i<lim; i++)
if(i<rev[i])
swap(a[i], a[rev[i]]);
for(int i=2; i<=lim; i<<=1){
int tmp=i>>1;
Complex wn=Complex(cos(2*PI/i), opt*sin(2*PI/i));
for(int j=0; j<lim; j+=i){
Complex w=Complex(1.0, 0.0);
for(int k=0; k<tmp; k++){
Complex tmp1=a[j+k], tmp2=w*a[j+k+tmp];
a[j+k] = tmp1 + tmp2;
a[j+k+tmp] = tmp1 - tmp2;
w = w * wn;
}
}
}
if(opt==-1)
for(int i=0; i<lim; i++)
a[i].x /= lim;
}
int main(){
cin>>n>>m>>k;
for(int i=0; i<n; i++){
scanf("%s", ss);
for(int j=0; j<m; j++)
a[i*m+j].x = ss[j]=='1';
}
int nx=0, ny=0, mxy=0, mxx=0, mnx=0, mny=0;
scanf("%s", ss+1);
for(int i=1; i<=k; i++){
if(ss[i]=='w') nx--;
if(ss[i]=='s') nx++;
if(ss[i]=='a') ny--;
if(ss[i]=='d') ny++;
mxy = max(mxy, ny); mxx = max(mxx, nx);
mny = min(mny, ny); mnx = min(mnx, nx);
}
nx = ny = 0;
mxx -= mnx; mxy -= mny;
b[(nx-mnx)*m+(ny-mny)].x = 1;
for(int i=1; i<=k; i++){
if(ss[i]=='w') nx--;
if(ss[i]=='s') nx++;
if(ss[i]=='a') ny--;
if(ss[i]=='d') ny++;
b[(nx-mnx)*m+(ny-mny)].x = 1;
}
int u=n*m-1;
while(lim<=2*u) lim <<= 1, limcnt++;
for(int i=0; i<lim; i++)
rev[i] = (rev[i>>1]>>1) | ((i&1)<<(limcnt-1));
reverse(b, b+1+u);
fft(a, 1);
fft(b, 1);
for(int i=0; i<lim; i++)
a[i] = a[i] * b[i];
fft(a, -1);
int ans=0;
for(int i=0; i<n-mxx; i++)
for(int j=0; j<m-mxy; j++)
if(a[u+i*m+j].x<0.5)
ans++;
cout<<ans<<endl;
return 0;
}