//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,mk[20][20];
int dx[]={0,0,1,1,1,-1,-1,-1};
int dy[]={1,-1,0,1,-1,0,1,-1};
mi mm;
string p[10];
int main(){
int tt=1,flag,r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string s;
f(i,0,20){
if(i<=2)
mm[i]=0;
else if(i<=4)
mm[i]=1;
else if(i==5)
mm[i]=2;
else if(i==6)
mm[i]=3;
else if(i==7)
mm[i]=5;
else
mm[i]=11;
}
si(t);
while(t--){
ans=0;
f(i,0,4)
cin>>p[i];
si(m);
while(m--){
cin>>s;
flag=0;
if(s.size()<=16){
f(i,0,4){
f(j,0,4){
if(p[i][j]==s[0]){
clr(mk,0);
queue<pair<pii,pii > > q;
q.push(mp(mp(i,j),mp(1,(1 << (4*i+j)))));
while(!q.empty()){
pair<pii,pii > pp=q.front();
q.pop();
x=pp.first.first;y=pp.first.second;z=pp.second.first;r=pp.second.second;
if(z==s.size()){
flag=1;
break;
}
else if(z>s.size())
break;
f(l,0,8){
x1=x+dx[l];y1=y+dy[l];
if(x1>=0 && x1<4 && y1>=0 && y1<=4 && p[x1][y1]==s[z]){
if(((1<<(4*x1+y1)) & r)==0)
q.push(mp(mp(x1,y1),mp(z+1,r|(1<<(4*x1+y1)))));
}
}
}
}
}
}
}
if(flag)
ans+=mm[s.size()];
}
printf("Score for Boggle game #%d: %d\n",tt++,ans);
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,mk[20][20];
int dx[]={0,0,1,1,1,-1,-1,-1};
int dy[]={1,-1,0,1,-1,0,1,-1};
mi mm;
string p[10];
int main(){
int tt=1,flag,r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string s;
f(i,0,20){
if(i<=2)
mm[i]=0;
else if(i<=4)
mm[i]=1;
else if(i==5)
mm[i]=2;
else if(i==6)
mm[i]=3;
else if(i==7)
mm[i]=5;
else
mm[i]=11;
}
si(t);
while(t--){
ans=0;
f(i,0,4)
cin>>p[i];
si(m);
while(m--){
cin>>s;
flag=0;
if(s.size()<=16){
f(i,0,4){
f(j,0,4){
if(p[i][j]==s[0]){
clr(mk,0);
queue<pair<pii,pii > > q;
q.push(mp(mp(i,j),mp(1,(1 << (4*i+j)))));
while(!q.empty()){
pair<pii,pii > pp=q.front();
q.pop();
x=pp.first.first;y=pp.first.second;z=pp.second.first;r=pp.second.second;
if(z==s.size()){
flag=1;
break;
}
else if(z>s.size())
break;
f(l,0,8){
x1=x+dx[l];y1=y+dy[l];
if(x1>=0 && x1<4 && y1>=0 && y1<=4 && p[x1][y1]==s[z]){
if(((1<<(4*x1+y1)) & r)==0)
q.push(mp(mp(x1,y1),mp(z+1,r|(1<<(4*x1+y1)))));
}
}
}
}
}
}
}
if(flag)
ans+=mm[s.size()];
}
printf("Score for Boggle game #%d: %d\n",tt++,ans);
}
return 0;
}
No comments:
Post a Comment