标签:des style blog http color os io java strong
3 1 1 RRRR 1 2 RRRF FCCC 8 8 FCFF RRFC FRCR FRFR RCCR FFCC RRFF CRFR FRRC FRFR CCCR FCFC CRRC CRRR FRCR FRFR RRCR FRRR CCCR FFFC RRFF RFCR CCFF FCCC CFCF RRFF CRFR FFRR FRRF CCRR FFFC CRRF CFRR FFFF FFFF RRFF RRRR RCRR FFCC RFRF RRCF FRFR FRRR FRFR RCCR RCCC CFFC RFRF CFCF FRFF RRFF FFFF CFFF CFFF FRFF RFRR CCRR FCFC FCCC FCCC FFCC FCCF FFCC RFRF
Case 1: 4 Case 2: 1 Case 3: 1048576
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MOD = 1000000007; const int maxm = 550000+10; typedef long long LL; struct Mat{ int top,rgt,bot,lft; bool same; Mat(int top,int rgt,int bot,int lft):top(top),rgt(rgt),bot(bot),lft(lft){ same = (top==rgt&&top==bot&&top==lft&&rgt==bot&&rgt==lft&&bot==lft); } Mat(){} }; Mat mat[13][13][5]; LL dp[2][maxm]; LL mi_3[14]; int fmask; int n,m; void init(){ scanf("%d%d",&n,&m); memset(dp,0,sizeof dp); fmask = 1; for(int i = 1; i <= m; i++){ fmask *= 3; } } int getDir(char op){ if(op=='C') return 0; if(op=='F') return 1; if(op=='R') return 2; } void Rotate(){ for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ for(int k = 1; k <= 3; k++){ mat[i][j][k].top = mat[i][j][k-1].lft; mat[i][j][k].rgt = mat[i][j][k-1].top; mat[i][j][k].bot = mat[i][j][k-1].rgt; mat[i][j][k].lft = mat[i][j][k-1].bot; } } } } int getDigit(int mask,int x){//得到三进制右边的第x位 while(x--){ mask /= 3; } return mask%3; } void input(){ for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ string str; int num[4]; cin >> str; for(int k = 0; k < 4; k++){ num[k] = getDir(str[k]); } mat[i][j][0] = Mat(num[0],num[1],num[2],num[3]); } } Rotate(); } void dfs(int row,int col ,int lastmask,int nowmask,int botmask,LL val,int cnt){ if(col==m){ for(int i = 0; i < cnt; i++) { val = (val*4)%MOD; } dp[0][botmask] = (dp[0][botmask]+val)%MOD; return; } int up = getDigit(lastmask,col); if(mat[row][col][0].same) { if((col==0||mat[row][col][0].lft==nowmask)&&(row==0||mat[row][col][0].top==up)) { botmask += mat[row][col][0].bot*mi_3[col]; dfs(row,col+1,lastmask,mat[row][col][0].rgt,botmask,val,cnt+1); botmask -= mat[row][col][0].bot*mi_3[col]; } }else { for(int i = 0; i < 4; i++){ if((col==0||mat[row][col][i].lft==nowmask)&&(row==0||mat[row][col][i].top==up)){ botmask += mat[row][col][i].bot*mi_3[col]; dfs(row,col+1,lastmask,mat[row][col][i].rgt,botmask,val,cnt); botmask -= mat[row][col][i].bot*mi_3[col]; } } } } void solve(){ dfs(0,0,0,0,0,1,0); for(int i = 1; i < n; i++){ for(int j = 0; j < fmask; j++){ dp[1][j] = dp[0][j]; dp[0][j] = 0; } for(int j = 0; j < fmask; j++){ if(dp[1][j]){ dfs(i,0,j,0,0,dp[1][j],0); } } } LL ans = 0; for(int i = 0; i < fmask; i++){ ans = (ans+dp[0][i])%MOD; } printf("%I64d\n",ans); } void doit(){ mi_3[0] = 1; for(int i = 1; i < 14; i++){ mi_3[i] = mi_3[i-1]*3; } } int main(){ int ncase,T=1; doit(); cin >> ncase; while(ncase--){ init(); input(); printf("Case %d: ",T++); solve(); } return 0; }
标签:des style blog http color os io java strong
原文地址:http://blog.csdn.net/mowayao/article/details/39031027