POJ2965 - The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator
| Time Limit: 1000MS | Memory Limit: 65536K | |
|---|---|---|
| Total Submissions: 35347 | Accepted: 13667 | Special Judge |
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+--
----
----
-+--
Sample Output
6
1 1
1 3
1 4
4 1
4 3
4 4
Source
Northeastern Europe 2004, Western Subregion
Hints:
Quite similar to the the other turn on/off question(POJ 1753)
Write a BFS, update the state and change it back to its previous state after searching
Implemented a stack myself, and copy the current stack to a result array res[21][2] to save the result.
Even though I copied most codes from the solution of POJ 1753, it still took me 15 mins to finish this question.(orz)
Solution:
// POJ2965
// zqsml on 3 Mar 2021
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
int ans = INF;
char board[5][5];
int grid[4][4];
int st[21][2];
int res[21][2];
int h;
void push(int a, int b){
h ++ ;
st[h][0] = a;
st[h][1] = b;
}
void pop(){
h -- ;
}
bool check(){
for(int i = 0; i < 4; i ++ )
for(int j = 0; j < 4; j ++ )
if(grid[i][j] != 1)
return false;
return true;
}
void flip(int row, int col){
for(int i = 0; i < 4; i ++ ){
if(i == row) continue;
grid[i][col] = !grid[i][col];
}
for(int i = 0; i < 4; i ++ ){
grid[row][i] = !grid[row][i];
}
}
void dfs(int row, int col, int count, int head){
if(check()){
ans = min(ans, count);
memcpy(res, st, sizeof res);
return ;
}
if(count > ans) return;
int x = row * 4 + col;
if(x == 16) return;
x ++ ;
int nx = x / 4;
int ny = x % 4;
flip(row, col);
push(row, col);
dfs(nx, ny, count + 1, head + 1);
pop();
flip(row, col);
dfs(nx, ny, count, head);
}
int main(){
for(int i = 0; i < 4; i ++ ) scanf("%s", board[i]);
memset(grid, 0, sizeof grid);
h = -1;
for(int i = 0; i < 4; i ++ )
for(int j = 0; j < 4; j ++ )
if(board[i][j] == '-') grid[i][j] = 1;
dfs(0, 0, 0, -1);
printf("%d\n", ans);
for(int i = 0; i < ans; i ++ )
printf("%d %d\n", res[i][0] + 1, res[i][1] + 1);
}
It is weird to write multiple functions for me, since I've never done that in competitions = =.
Maybe I've spent too much time on Java and OOP recently.
C++ is the best still.(it's just a joke, no cyerbullying plz)

浙公网安备 33010602011771号