枚举 PROBLEM4 熄灯问题

 

0 0 0 0 0 0 0 0
0     p       0
0   p P&p p     0
0     p?       0
0             0
0             0

!the value of press[][] is up to Puzzle[][] previous row   and press[][] previous row

!use 8*8 matrix to simplify the formula
so that we do not have to change formula on the border  

#include <cstdlib>
#include <iostream>

using namespace std;
/**lights our problem*/

//use 8*8 matrix to simplify the formula 
//so that we do not have to change formula on the border   
int puzzle[6][8];//global variable  
int press[6][8];
/**juedge if it douse out all lights
*根据press第一行和puzzle情况是否能够熄灭所有灯 
*/
bool guess()
{
     for(int i=1;i<5;i++)
     {
             for(int j=1;j<=6;j++)
             {
                   //do not forget press[i][j]  
                   press[i+1][j]=
                   (puzzle[i][j]+press[i][j-1]+press[i][j+1]+press[i-1][j]+press[i][j])%2;  
             }
     }
     for(int j=1;j<=6;j++)
     {
             if((press[5][j-1]+press[5][j+1]+press[4][j]+press[5][j])%2!=puzzle[5][j])
             {
                   return false;
             }
     }
     return true;
}

/**enumerate the condition of first line 
*枚举press第一行的情况 
*0----------------
*1----------------
*01---------------
*11---------------
*001--------------
*101--------------
*011--------------
*111--------------
*like binary addition
*/
void enumerate()
{
     for(int j=1;j<=6;j++)
     {
             press[1][j]=0;
     }
     //this is a substitution of for(0 1){for(0 1){for(0 1){.....}}}
     while(guess()==false)
     {
            press[1][1]++;
            int k=1;
            while(press[1][k]>1)
            {
                 press[1][k]=0;
                 k++;
                 press[1][k]++;
            }
     }
     return;
}
int main(int argc, char *argv[])
{
    int c;
    scanf("%d",&c);
    for(int x=0;x<=7;x++)
    {
            press[0][x]=0;
    }
    for(int x=0;x<=6;x++)
    {
            press[x][0]=press[x][7]=0;
    }
    for(int num=0;num<c;num++)
    {
            for(int x=1;x<=5;x++)
            {
                    for(int y=1;y<=6;y++)
                    {
                            scanf("%d",&puzzle[x][y]);
                    }
            }
            enumerate();   
            printf("PUZZLE# %d  \n",num+1);
            for(int x=1;x<=5;x++)
            {
                    for(int y=1;y<=6;y++)
                    {
                            printf("%d",press[x][y]);
                    }
                    printf("\n");
            }
    }   
    system("PAUSE");
    return EXIT_SUCCESS;
}
posted @ 2010-04-20 11:49  love && peace  阅读(301)  评论(0编辑  收藏  举报