Blog·Tanky WooABOUTRSS

百练oj2811 熄灯问题

15 May 2010
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

//ID:百练2811 熄灯问题 //网站:C++奋斗乐园|C++论坛|算法论坛|ACM/ICPC论坛 //地址:http://www.cppleyuan.com/ //个人主页:www.wutianqi.com

以下是方法一:

 #include 
#include

#include 
#include 
#include 
#include 
using namespace std;

//就怕超时啊!
int puzzle[7][8], press[7][8];
int temp[7][8];

int main()
{
    //freopen("input_2811.txt", "r", stdin);
    //freopen("output_2811.txt", "w", stdout);
    int nCases;
    scanf("%d", &nCases);
    int i, j;
    int count = 1;
    while(nCases)
    {

        for(i = 1; i <= 5; i++)
            for(j = 1; j <= 6; j++)
                scanf("%d", &puzzle[i][j]);
        //strcpy(temp, puzzle);
        int m[7];//={0,1,0,1,0,0,1};

        for(m[1]= 0; m[1] <= 1; m[1]++)
            for(m[2]= 0; m[2] <= 1; m[2]++)
                for(m[3]= 0; m[3] <= 1; m[3]++)
                    for(m[4]= 0; m[4] <= 1; m[4]++)
                        for(m[5]= 0; m[5] <= 1; m[5]++)
                            for(m[6]= 0; m[6] <= 1; m[6]++)
                            {
                                memset(temp, 0, sizeof(temp));
                                memset(press, 0, sizeof(press)); //害我想了N久
                                for(int x = 1; x <= 5; x++)
                                    for(int z = 1; z <= 6; z++)
                                        temp[x][z] = puzzle[x][z];
                                //printf("%d,%d,%d,%d,%d,%d\n", m[1],m[2],m[3],m[4], m[5],m[6]);
                                for(int k = 1; k <= 6; k++)
                                {
                                    press[1][k] = m[k];
                                    //printf("%d\n", press[1][k]);
                                    if(press[1][k])
                                    {
                                        temp[1][k] = !temp[1][k];
                                        temp[1+1][k] = !temp[1+1][k];
                                        temp[1][k-1] = !temp[1][k-1];
                                        temp[1][k+1] = !temp[1][k+1];
                                    }
                                }
                                /*
                                for(int k = 1; k <= 6; k++)
                                {
                                    printf("%d ", press[1][k]);
                                }
                                printf("\n");
                                */
                                for(i = 1; i <= 4; i++)
                                    for(j = 1; j <= 6; j++)
                                        if(temp[i][j] == 1)
                                        {
                                            //printf("i=%d, j=%d\n", i, j);
                                            press[i+1][j] = 1;
                                            temp[i+1][j] = !temp[i+1][j];
                                            temp[i][j] = !temp[i][j];
                                            temp[i+2][j] = !temp[i+2][j];
                                            temp[i+1][j-1] = !temp[i+1][j-1];
                                            temp[i+1][j+1] = !temp[i+1][j+1];
                                        }
                                int flag = 1;
                                for(int q = 1; q <= 6; q++)
                                    if(temp[5][q] == 1)
                                    {
                                        flag = 0;
                                        break;
                                    }
                                    if(flag)
                                    {
                                        printf("PUZZLE #%d\n", count);
                                        for(i = 1; i <= 5; i++)
                                        {
                                            for(j = 1; j <= 6; j++)
                                                printf("%d ", press[i][j]);
                                            putchar('\n');
                                        }
                                    }
                            }
    count++;
    nCases--;
    }
    return 0;
}