C语言中的熄灯问题extended lights out(枚举)POJ1222//乱用函数返回值会遭报应QAQ

Description
http://poj.org/problem?id=1222
In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual puzzle has 5 rows of 5 buttons each). Each button has a light. When a button is pressed, that button and each of its (up to four) neighbors above, below, right and left, has the state of its light reversed. (If on, the light is turned off; if off, the light is turned on.) Buttons in the corners change the state of 3 buttons; buttons on an edge change the state of 4 buttons and other buttons change the state of 5. For example, if the buttons marked X on the left below were to be pressed,the display would change to the image on the right.
The aim of the game is, starting from any initial set of lights on in the display, to press buttons to get the display to a state where all lights are off. When adjacent buttons are pressed, the action of one button can undo the effect of another. For instance, in the display below, pressing buttons marked X in the left display results in the right display.Note that the buttons in row 2 column 3 and row 2 column 5 both change the state of the button in row 2 column 4,so that, in the end, its state is unchanged.
Note:
1. It does not matter what order the buttons are pressed.
2. If a button is pressed a second time, it exactly cancels the effect of the first press, so no button ever need be pressed more than once.
3. As illustrated in the second diagram, all the lights in the first row may be turned off, by pressing the corresponding buttons in the second row. By repeating this process in each row, all the lights in the first
four rows may be turned out. Similarly, by pressing buttons in columns 2, 3 ?, all lights in the first 5 columns may be turned off.
Write a program to solve the puzzle.
Input
The first line of the input is a positive integer n which is the number of puzzles that follow. Each puzzle will be five lines, each of which has six 0 or 1 separated by one or more spaces. A 0 indicates that the light is off, while a 1 indicates that the light is on initially.
Output
For each puzzle, the output consists of a line with the string: “PUZZLE #m”, where m is the index of the puzzle in the input file. Following that line, is a puzzle-like display (in the same format as the input) . In this case, 1’s indicate buttons that must be pressed to solve the puzzle, while 0 indicate buttons, which are not pressed. There should be exactly one space between each 0 or 1 in the output puzzle-like display.
Sample Input
2
0 1 1 0 1 0
1 0 0 1 1 1
0 0 1 0 0 1
1 0 0 1 0 1
0 1 1 1 0 0
0 0 1 0 1 0
1 0 1 0 1 1
0 0 1 0 1 1
1 0 1 1 0 0
0 1 0 1 0 0
Sample Output
PUZZLE #1
1 0 1 0 0 1
1 1 0 1 0 1
0 0 1 0 1 1
1 0 0 1 0 0
0 1 0 0 0 0
PUZZLE #2
1 0 0 1 1 1
1 1 0 0 0 0
0 0 0 1 0 0
1 1 0 1 0 1

每盏灯只有两种可能,按或不按,因为按两次的结果和不按是一样的。一共有30盏灯,就是2的30次方种可能。显然一一枚举是不可能的。
那怎么能让枚举更快呢?
只枚举一个局部。如果存在某个局部,一旦这个局部的状态被确定,那么剩余其他部分的状态只能是确定的一种,或者不多的n种
那么就只需枚举这个局部的状态即可 。
在这道题中,第一行的灯就是这样一个局部。一旦第一行的灯的状态确定了,能控制第一行灯的只能是第二行的灯,所以按第二行灯的目的就是让所有第一行的灯关上,由此第二行灯的按与不按就确定了。由此类推,如果最后一行的灯也全能关上,那证明第一行的这种可能就是正确的。
第一行一共有6盏灯,按与不按两种状态,一共2的六次方64种可能,依次讨论就能得到答案。可以让1到64间的十进制数转成2进制,这样用一个for循环就可以达到枚举的效果。

#include

void input();
void turn();
void print(int n);
int test();
void switchs(int i,int j);

int ori[10][10],map[10][10],output[10][10];

int main()
{
    int m;
    scanf("%d",&m);
    int i=1;
    while(i<=m)
    {
        input();
        turn();
        print(i);
        i++;
    }
} 

void input()//输入
{
    int i,j;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=6;j++)
        {
            scanf("%d",&ori[i][j]);
        }
    }
}

void turn()
{
    for(int i=1;i<=64;i++)//枚举第一行的64种可能
    {
        int temp=i;
        for(int j=1;j<=6;j++)
        //将十进制数转换成二进制表示“按与不按”
        {
            output[1][j]=temp%2;
            temp=temp/2;
        }
        for(int i=1;i<=5;i++)
        {
            for(int j=1;j<=6;j++)
            {
                map[i][j]=ori[i][j];
            //每次尝试新可能时都要恢复上一种可能造成的影响和改变
            }
        }
        int result=test();
//注意!!!
//这里用if(test()==0)然后if(test()==1)是不对的
//这样test()实际上就被调用了两次
//每种可能只能调用一次
        if(result==0) continue;
        if(result==1) return;
    }
}

int test()
{
    for(int i=1;i<=5;i++)
    {
        for(int j=1;j<=6;j++)
        {
            if(output[i][j]==1)//按第i行的开关
            {
                switchs(i,j);
            }
        }
        for(int j=1;j<=6;j++)
        {
            if(map[i][j]==1)
            //根据第i行灯的开关情况决定第i+1行按与不按
            {
                output[i+1][j]=1;
            }
            else output[i+1][j]=0;
        }
    }
    int sum=0;
    for(int j=1;j<=6;j++)
    {
        sum+=map[5][j];
        //检查第5行的灯是不是都关着
    }
    if(sum==0) return 1;
    if(sum>0) return 0;
}

void switchs(int i,int j)
//按第i行第j列的开关后周围五盏灯的变化
//因为还有第0行第0列第6行第7列所以不用担心角落只影响3盏4盏灯的情况
{
    map[i][j]=(map[i][j]+1)%2;
    map[i+1][j]=(map[i+1][j]+1)%2;
    map[i-1][j]=(map[i-1][j]+1)%2;
    map[i][j+1]=(map[i][j+1]+1)%2;
    map[i][j-1]=(map[i][j-1]+1)%2;
}

void print(int n)//输出
{
    int i,j;
    printf("PUZZLE #%d\n",n);
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=6;j++)
        {
            printf("%d ",output[i][j]);
        }
        printf("\n");
    }
}

这道题一开始一直没过是因为在if(test()==0)和if(test()==1)中调用了两次test()。
这样的惨痛经历告诫我们不要把函数的返回值当成变量一直用,每一个函数返回值背后都有一次调用,最好还是把函数返回值赋值到某个变量上再用。

你可能感兴趣的:(题目,枚举,错误总结)