poj 1222 Extended lights out (emulator)
2015-05-04 11:13
302 查看
This is an emulator question. Just try the answer so many times and you can solve it. However there are some tips as follow.
(1) If you want to turn off all lights on the first row, you should press the button on the second row to affect it and so on.
(2) the status of lights on the 5th row depend on the initial status of the first row, so just try to change the status of all lights on the first row as much as possible. The maximum try is 2^6 = 64;
#include<stdio.h>
#include<iostream>
#include<string.h>
int press[7][8], puzzle[7][8];
void emulator();
bool guess();
int main(){
int n, sum = 0;
scanf("%d", &n);
while(n--){
memset(puzzle, 0, sizeof(puzzle));
memset(press, 0, sizeof(press));
for(int i = 1; i <= 5; i++){
for(int j = 1; j <= 6; j++){
scanf("%d",&puzzle[i][j]);
}
}
emulator();
printf("PUZZLE #%d\n", ++sum);
for(int i = 1; i <= 5; i++){
for(int j = 1; j <=6; j++){
if(j == 6){
printf("%d", press[i][j]);
}else{
printf("%d ", press[i][j]);
}
}
printf("\n");
}
}
}
void emulator(){ // emulate all the possible status of all lights on the fist row
for(int i = 1; i <= 64; i++){
int pos = 1;
press[1][pos]++;
while(press[1][pos] > 1){
press[1][pos++] = 0;
press[1][pos]++;
}
press[1][7] = 0;
if(guess() == true){
break;
}
}
}
bool guess(){ // judge whether it can make all the lights on the map turn off. Pay much more attention on the lights of last row
for(int i = 2;i <= 5; i++){
for(int j = 1;j <= 6; j++){
press[i][j] = (press[i-1][j] + press[i-2][j] + press[i-1][j-1] + press[i-1][j+1] + puzzle[i-1][j]) % 2;
}
}
int pos = 5;
for(int j = 1; j <= 6; j++){
if(press[pos][j] != (press[pos-1][j] + press[pos][j-1] + press[pos][j+1] + puzzle[pos][j]) % 2){
return false;
}
}
return true;
}
(1) If you want to turn off all lights on the first row, you should press the button on the second row to affect it and so on.
(2) the status of lights on the 5th row depend on the initial status of the first row, so just try to change the status of all lights on the first row as much as possible. The maximum try is 2^6 = 64;
#include<stdio.h>
#include<iostream>
#include<string.h>
int press[7][8], puzzle[7][8];
void emulator();
bool guess();
int main(){
int n, sum = 0;
scanf("%d", &n);
while(n--){
memset(puzzle, 0, sizeof(puzzle));
memset(press, 0, sizeof(press));
for(int i = 1; i <= 5; i++){
for(int j = 1; j <= 6; j++){
scanf("%d",&puzzle[i][j]);
}
}
emulator();
printf("PUZZLE #%d\n", ++sum);
for(int i = 1; i <= 5; i++){
for(int j = 1; j <=6; j++){
if(j == 6){
printf("%d", press[i][j]);
}else{
printf("%d ", press[i][j]);
}
}
printf("\n");
}
}
}
void emulator(){ // emulate all the possible status of all lights on the fist row
for(int i = 1; i <= 64; i++){
int pos = 1;
press[1][pos]++;
while(press[1][pos] > 1){
press[1][pos++] = 0;
press[1][pos]++;
}
press[1][7] = 0;
if(guess() == true){
break;
}
}
}
bool guess(){ // judge whether it can make all the lights on the map turn off. Pay much more attention on the lights of last row
for(int i = 2;i <= 5; i++){
for(int j = 1;j <= 6; j++){
press[i][j] = (press[i-1][j] + press[i-2][j] + press[i-1][j-1] + press[i-1][j+1] + puzzle[i-1][j]) % 2;
}
}
int pos = 5;
for(int j = 1; j <= 6; j++){
if(press[pos][j] != (press[pos-1][j] + press[pos][j-1] + press[pos][j+1] + puzzle[pos][j]) % 2){
return false;
}
}
return true;
}
相关文章推荐
- poj 1222 EXTENDED LIGHTS OUT
- poj1222 EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT 高斯消元?暴力枚举!
- [高斯消元] poj 1222 EXTENDED LIGHTS OUT
- poj-1222-EXTENDED LIGHTS OUT && poj-1681-Painter's Problem
- poj 1222 EXTENDED LIGHTS OUT
- poj 1222:EXTENDED LIGHTS OUT
- poj 1222 EXTENDED LIGHTS OUT //高斯消元
- 【POJ 1222】EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT 高斯消元
- poj1222 EXTENDED LIGHTS OUT 高斯消元
- poj1222 EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- OpenJudge 2811 熄灯问题 / Poj 1222 EXTENDED LIGHTS OUT
- Poj1222 EXTENDED LIGHTS OUT
- poj 1222 EXTENDED LIGHTS OUT
- [POJ 1222] EXTENDED LIGHTS OUT
- POJ1222_EXTENDED LIGHTS OUT
- poj 1222 EXTENDED LIGHTS OUT