0716 POJ1222 EXTENDED LIGHTS OUT

来源:互联网 发布:太原java培训机构 编辑:程序博客网 时间:2024/06/05 07:39
摘要:枚举所有按的情况 ,检验符合要求的情况。

原题目摘要POJ - 1222 EXTENDED LIGHTS OUT

...
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.) ....
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. 
...

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
20 1 1 0 1 01 0 0 1 1 10 0 1 0 0 11 0 0 1 0 10 1 1 1 0 00 0 1 0 1 01 0 1 0 1 10 0 1 0 1 11 0 1 1 0 00 1 0 1 0 0
Sample Output
PUZZLE #11 0 1 0 0 11 1 0 1 0 10 0 1 0 1 11 0 0 1 0 00 1 0 0 0 0PUZZLE #21 0 0 1 1 11 1 0 0 0 00 0 0 1 0 01 1 0 1 0 11 0 1 1 0 1

题目理解
 note部分 已经很清楚的说明了解法,只要照着实现就行。也就是确定部分状态就可以确定其他的状态。

注意
行和列 以少的为出发点可以高效。

日期
20170716

代码
1
#include <iostream>
2
#include <cstdio>
3
#include <algorithm>
4
#include <iostream>
5
#include <cstring>
6
using namespace std;
7
8
#define R 5
9
#define C 6
10
#define change(x,y) {if(x>=0&&x<R&&y>=0&&y<C) map1[x][y]^=true;} 
11
12
bool state = false;//is_get
13
bool map1[R][C];//state
14
bool map2[R][C];//result
15
16
void press(int x,int y){
17
    map2[x][y]^=true;
18
    change(x,y);change(x-1,y);change(x+1,y);
19
    change(x,y-1);change(x,y+1);
20
}
21
22
void fresh(){//reback to point before tryall()  
23
    for(int i=1;i<R;i++){
24
        for(int j=0;j<C;j++)
25
            if(map2[i][j]) {
26
                press(i,j); 
27
        }
28
    }
29
}
30
31
void show(){
32
static int nn=0;
33
nn++;
34
    cout<<nn<<"------"<<endl;
35
    for(int i=0;i<R;i++) {
36
37
        for(int j=0;j<C;j++) cout<<map1[i][j];
38
        cout<<endl;
39
    }
40
}
41
42
bool tryall(){
43
    
44
    for(int i=1;i<R;i++){
45
        for(int j=0;j<C;j++){
46
            if(map1[i-1][j]) press(i,j); 
47
        }
48
    }
49
    
50
    //check
51
    //show();
52
    for(int j=0;j<C;j++){ 
53
        if(map1[R-1][j]) return false;
54
    } 
55
56
    return true;
57
}
58
59
void bin_find(int y,bool s){
60
    if(state) return;
61
    if(y<C){
62
        if(s) press(0,y);
63
        bin_find(y+1,true);
64
        bin_find(y+1,false);
65
        press(0,y);
66
    }else{
67
        if(tryall()){
68
            //found
69
            state = true;
70
            for(int i=0;i<R;i++){
71
                for(int j=0;j<C;j++) printf("%d ",map2[i][j]?1:0);
72
                printf("\n");
73
            }
74
        }
75
        fresh();
76
    }
77
}
78
void solve(){
79
    state = false;
80
    for(int i=0;i<R;i++){ 
81
        for(int j=0;j<C;j++){
82
            scanf("%d",&map1[i][j]);
83
            map2[i][j]=false;   
84
        }
85
    } 
86
    bin_find(0,true);
87
    bin_find(0,false);
88
}
89
90
int main(){
91
    int t;scanf("%d",&t);
92
    int t0=0;
93
    while(t0<t){
94
        t0++;
95
        printf("PUZZLE #%d\n",t0);
96
        solve();
97
    }
98
    
99
    return 0;
100
}