POJ 1222 EXTENDED LIGHTS OUT (熄灯问题)

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8417   Accepted: 5441

Description

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. 
POJ 1222 EXTENDED LIGHTS OUT  (熄灯问题)
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. 
POJ 1222 EXTENDED LIGHTS OUT  (熄灯问题)
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
1 0 1 1 0 1

POJ 1222 EXTENDED LIGHTS OUT  (熄灯问题)

POJ 1222 EXTENDED LIGHTS OUT  (熄灯问题)

POJ 1222 EXTENDED LIGHTS OUT  (熄灯问题)

  所以,枚举第一行按钮是否按下的所有状态就行。

解法一: 使用for循环枚举(只适用于循环层数已知的情况)

 #include<iostream>
#include<cstring>
#include<stdio.h>  using namespace std; int n;
int p = ;
int orig_maze[][]; //原始的迷宫
int maze[][]; //每次循环后都要将maze重新还原为orig_maze
int result[][] = { }; void opposite(int x, int y)
{
if (x >= && x < && y >= && y < )
{
if (maze[x][y] == )
maze[x][y] = ;
else
maze[x][y] = ;
}
} void press(int x, int y)
{
opposite(x, y);
opposite(x + , y);
opposite(x - , y);
opposite(x, y + );
opposite(x, y - );
} bool judge()
{
for (int i = ; i < ; ++i)
for (int j = ; j < ; ++j)
{
if (maze[i][j] == )
return false;
}
return true; } void solve()
{
for (int i = ; i < ; ++i)
for (int j = ; j < ; ++j)
{
cin >> orig_maze[i][j];
maze[i][j] = orig_maze[i][j];
} //枚举第一行按下的按钮
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
for (result[][] = ; result[][] <= ; result[][]++)
{
for (int i = ; i <= ; ++i)
{ //按第一行
if (result[][i] == )
press(, i);
} //由第一行算出后面四行
for (int i = ; i <= ; ++i)
{
int k = i - ; //k表示i的上一行
for (int j = ; j <= ; ++j)
{
if (maze[k][j] == )
{
result[i][j] = ; //将其置为1表示按下
press(i, j);
}
else
result[i][j] = ; //没按下一定要置零,因为以前循环时置的1还保留在此
}
} if (judge() == true)
{
printf("PUZZLE #%d\n", p++);
for (int i = ; i < ; ++i)
{
int j;
for (j = ; j < ; ++j)
cout << result[i][j] << ' ';
cout << result[i][j] << endl; }
return ;
} memcpy(maze, orig_maze, sizeof(orig_maze)); //将迷宫还原
}
} int main()
{
int n;
cin >> n; while (n--)
{
solve();
} return ;
}

解法二: 使用dfs枚举(适用于循环层数未知的情况)

 #include<iostream>
#include<cstring>
#include<stdio.h>  using namespace std; int m, n;
int orig_maze[][]; //原始的迷宫
int maze[][]; //每次循环后都要将maze重新还原为orig_maze
int result[][] = { };
int book[][]; // 使用book保存第一行的所有枚举情况
int index = ;
int p = ; void opposite(int x, int y)
{
if (x >= && x < m && y >= && y < n)
{
if (maze[x][y] == )
maze[x][y] = ;
else
maze[x][y] = ;
}
} void press(int x, int y)
{
opposite(x, y);
opposite(x + , y);
opposite(x - , y);
opposite(x, y + );
opposite(x, y - );
} bool judge()
{
for (int i = ; i < m; ++i)
for (int j = ; j < n; ++j)
{
if (maze[i][j] == )
return false;
}
return true; } void dfs(int step)
{
if (step == n)
{
for (int i = ; i < n; ++i)
book[index][i] = result[][i]; // 使用book保存第一行的所有枚举情况
index++;
return;
} for (int i = ; i <= ; ++i)
{
result[][step] = i;
dfs(step + );
}
} void g()
{ for (int i = ; i < m; ++i)
for (int j = ; j < n; ++j)
{
cin >> orig_maze[i][j];
maze[i][j] = orig_maze[i][j];
} dfs(); for (int t = ; t < index; ++t)
{
for (int t1 = ; t1 < n; ++t1)
{
result[][t1] = book[t][t1];
} for (int i = ; i < n; ++i)
{ //按第一行
if (result[][i] == )
press(, i);
} //由第一行算出后面m-1行
for (int i = ; i < m; ++i)
{
int k = i - ; //k表示i的上一行
for (int j = ; j < n; ++j)
{
if (maze[k][j] == )
{
result[i][j] = ; //将其置为1表示按下
press(i, j);
}
else
result[i][j] = ; //没按下一定要置零,因为以前循环时置的1还保留在此
}
} if (judge() == true)
{
printf("PUZZLE #%d\n", p++);
for (int i = ; i < m; ++i)
{
int j;
for (j = ; j < n - ; ++j)
cout << result[i][j] << ' ';
cout << result[i][j] << endl; }
return ;
}
memcpy(maze, orig_maze, sizeof(orig_maze)); //将迷宫还原
} } int main()
{
m = ;
n = ;
int k;
cin >> k;
while(k--)
{
g();
} return ;
}
上一篇:php-wamp滴定仪网站的根目录


下一篇:第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)