1038 Bugs Integrated, Inc.

#include <iostream.h> 
const int Power[] ={1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049}; 
unsigned char best[4][10][59049]; 
bool bug[150][10]; 
int profile[10]; 
int d,m,n,k; 
int x,y,p; 
int tx, ty, tp; 
int py, py1, py2, py3; 
int pp,dd,kk; 
int main() 
{ 
    cin>>d; 
    for ( dd=0; dd<d; dd++) { 
        cin>>n>>m>>k; 
        for ( x=0; x<n; x++) 
            for ( y=0; y<m; y++) 
                bug[x][y] = false; 
        for ( kk=0; kk<k; kk++) { 
            cin>>x>>y; 
            bug[x-1][y-1] = true; 
        } 
        int maxP = Power[m]; 
        for ( y=0; y<m; y++) for (p=0; p<maxP; p++) best[0][y][p] = 0; 
        for ( x=1; x<n; x++) for ( y=0; y<m; y++) { 
            for ( pp=0,p=0,py=0,py1=0,py2=0; p<maxP; p++,pp++) { 
            if ( p==1 && y==m-1 && x==n-1 ) break; 
            if ( y==0 ) { if ( pp==3 ) pp=0; py = pp;} 
            else if ( y==1 )  
            { if ( pp==3 ) {pp=0; py++;} py1 = pp; if ( py ==3 ) py=0;} 
            else if ( y==2 ) 
            { if ( pp==3 ) {pp=0; py1++;} py2 = pp; if ( py1==3 ) { py1=0; py++;} if ( py==3 ) py=0;} 
            else if ( y>2) 
            {if ( pp==Power[y-2] ) { pp=0; py2++;} if ( py2==3 ) { py2=0; py1++;} if ( py1==3 ) { py1=0; py++; } if (py==3 ) py=0;} 
            if ( py ) { 
                if ( y==0 ) { tx = x-1; ty = m-1;} 
                else { tx = x; ty= y-1; } 
                tp = p - Power[y]; 
                best [x&3][y][p] = best[tx&3][ty][tp]; 
            } else { 
                int temp1=0, temp2=0, temp3=0; 
                // can place 3*2 chip 
                if ( y>0 && x>1 && py1==0 && !bug[x][y] && !bug[x-1][y] && !bug[x-2][y] && !bug[x][y-1] && !bug[x-1][y-1] && !bug[x-2][y-1]) { 
                    if ( y==1 ) { tx = x-1; ty = m-1; } 
                    else { tx = x; ty = y-2; } 
                    tp = p + Power[y+1] - Power[y-1]; 
                    temp1 = best[tx&3][ty][tp]+1; 
                } 
                // can place 2*3 chip 
                if ( y>1 && x>0 && py1==0 && py2==0 && !bug[x][y] && !bug[x-1][y] && !bug[x][y-1] && !bug[x-1][y-1] && !bug[x][y-2] && !bug[x-1][y-2]) { 
                    if ( y==2 ) { tx = x-1; ty = m-1; } 
                    else { tx = x; ty = y-3; } 
                    tp = p + Power[y] + Power[y-1] + Power[y-2]; 
                    temp2 = best[tx&3][ty][tp]+1; 
                } 
                // place nothing 
                if ( y==0 ) { tx = x-1; ty = m-1; } 
                else { tx = x; ty = y-1; } 
                temp3 = best[tx&3][ty][p]; 
                temp1 = (temp2>temp1) ? temp2 : temp1; 
                temp1 = (temp3>temp1) ? temp3 : temp1; 
                best[x&3][y][p] = temp1; 
            } 
            } 
        } 
        cout<<int(best[(n-1)&3][m-1][0])<<endl; 
    } 
    return 0; 
} 

你可能感兴趣的:(bugs integrated,inc.)