題意:

給一堆亮或暗的燈泡,按一個燈泡可以讓自己以及四周交換狀態,問說要按那些燈泡才行將所有燈泡變成0

 

解法:

燈泡開關問題,窮舉第一排狀態,其他的燈泡照填

 

程式碼:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 1<<29
int map[10][10];
int light[10][10];
int choice[10][10];
int move[5][2] = { {0,0},{1,0},{0,1},{-1,0},{0,-1} };
void copy(){
    int i,j;
    for(i=1;i<=5;++i){
        for(j=1;j<=6;++j){
            light[i][j] = map[i][j];
        }
    }
}
void push(int x,int y){
    int i;
    choice[x][y] = 1;
    for(i=0;i<5;++i){
        light[x+move[i][0]][y+move[i][1]] = !light[x+move[i][0]][y+move[i][1]];
    }
}
int solve(){
    int i,j;
    for(i=2;i<=5;++i){
        for(j=1;j<=6;++j){
            if(light[i-1][j] ==1){
                push(i,j);
            }
        }
    }
    for(j=1;j<=6 && light[5][j]==0;++j);
    if(j>6)return 1;
    return 0;
}
int main(){
    int i,j;
    int t;
    int Case = 0;
    scanf("%d",&t);
    while(t--){
        for(i=1;i<=5;++i){
            for(j=1;j<=6;++j){
                scanf("%d",&map[i][j]);
            }
        }
        for(i=0;i<64;++i){
            copy();
            memset(choice,0,sizeof(choice));
            for(j=0;j<6;++j){
                if( (i&(1<<j))>0 ){
                    push(1,j+1);
                }
            }
            if(solve())break;
        }
        if(i<64){
            printf("PUZZLE #%d\n",++Case);
            for(i=1;i<=5;++i){
                for(j=1;j<=6;++j){
                    printf("%d%c",choice[i][j],(j==6)?'\n':' ');
                }
            }
        }else puts("GG");
    }
    return 0;
}

arrow
arrow
    全站熱搜

    alan790712 發表在 痞客邦 留言(0) 人氣()