POJ 2811 熄灯问题 解题报告

POJ 2811 熄灯问题 解题报告

编号:2811

 

考查点:枚举,遍历搜索空间

 

思路:需要找出来从何下手搜索,综合得出,枚举第一行按钮出现的情况,然后根据第一行数据进行搜索,判断最后一行的灯是否全部熄灭。

 

提交情况: 这道题是在在纸上画的,然后写代码,一次编译通过,然后直接提交,直接AC,一点阻拦都没有(表达式错误不怪我),太爽了!

 

Source Code

 


//POJ Grids 2811
#include <iostream>
using namespace std;

bool light[6][7];
bool button[6][7];
bool temp[6][7];

void change(int i,int j)
{
    temp[i][j] 
= !temp[i][j];
    temp[i
-1][j] = !temp[i-1][j];
    temp[i
+1][j] = !temp[i+1][j];
    temp[i][j
-1= !temp[i][j-1];
    temp[i][j
+1= !temp[i][j+1];
}

bool press()
{

    memcpy(temp,light,
sizeof temp);
    
for (int i=1;i<=5;i++)
    {
        
for (int j=1;j<=6;j++)
        {
            
if (button[i][j])
            {
                change(i,j);

            }
        }
        
for (int j=1;j<=6;j++)
        {
            
if (temp[i][j])
            {
                button[i
+1][j] = 1;
            }
        }
    }
    
for (int i=1;i<=6;i++)
    {
        
if (temp[5][i])
        {
            
return false;
        }
    }
    
return true;
}

int main()
{
    
int n;
    cin
>>n;
    
for (int i=1;i<=n;i++)
    {
        
for (int m=1;m<=5;m++)
        {
            
for (int n=1;n<=6;n++)
            {
                cin
>>light[m][n];
            }
        }

        
for (int a=0;a<=1;a++)
        {
            
for (int b=0;b<=1;b++)
            {
                
for (int c=0;c<=1;c++)
                {
                    
for (int d=0;d<=1;d++)
                    {
                        
for (int e=0;e<=1;e++)
                        {
                            
for (int f=0;f<=1;f++)
                            {
                                memset(button,
0,sizeof button);
                                button[
1][1= a;
                                button[
1][2= b;
                                button[
1][3= c;
                                button[
1][4= d;
                                button[
1][5= e;
                                button[
1][6= f;

                                
if(press())
                                    
goto L;
                                
                            }
                        }
                    }
                }
            }
        }
L:
        cout
<<"PUZZLE #"<<i<<endl;
        
for (int i=1;i<=5;i++)
        {
            
for (int j=1;j<=6;j++)
            {
                cout
<<button[i][j]<<" ";
            }
            cout
<<endl;
        }
    }
    
return 0;
}

 

总结:深深的体会到了模块化的优势,虽然ACM的题都基本不会超过300行,但是把每个功能抽象成函数可以让自己思维清晰,代码简洁,感谢模块化的思考.让我完美的解决了这道题.

 

 

 

                                                       By   Ns517

                                                      Time 09.02.04

posted @ 2009-02-04 14:56  端木  阅读(1534)  评论(0编辑  收藏  举报