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
題目大意:與POJ1753類似(沒做過poj1753的可以參看本博客關於poj1753的博文)。只是把操作上下左右改爲操作同行同列。解題思想一樣的。不過要注意的是,這道題目要求輸出操作過的點。所以要維護一下。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
int hand[4][4];
vector<pair<int, int> > ans;
int vis[16];
int tot = 1e4;
void run(int pos)
{
int x = pos / 4;
int y = pos % 4;
for(int j = 0; j < 4; ++j) hand[x][j] = !hand[x][j];
for(int i = 0; i < 4; ++i) hand[i][y] = !hand[i][y];
hand[x][y] = !hand[x][y];
}
void judge()
{
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
if(!hand[i][j]) return;
int sum = 0;
for(int i = 0; i < 16; ++i) if(vis[i]) sum++;
if(sum < tot){
ans.clear();
for(int i = 0; i < 16; ++i)
if(vis[i]){
int x = i / 4;
int y = i % 4;
ans.push_back({x, y});
}
tot = sum;
}
}
void solve(int cur)
{
if(cur >= 16) return;
judge();
solve(cur+1);
run(cur);
vis[cur] = 1;
judge();
solve(cur+1);
run(cur);
vis[cur] = 0;
}
int main()
{
string sin;
for(int i = 0; i < 4; ++i){
cin >> sin;
for(int j = 0; j < 4; ++j){
if(sin[j] == '-') hand[i][j] = 1;
else hand[i][j] = 0;
}
}
memset(vis, 0, sizeof(vis));
int cur = 0;
solve(cur);
printf("%d\n", ans.size());
for(int i = 0; i < ans.size(); ++i){
pair<int, int> tag = ans[i];
printf("%d %d\n", tag.first+1, tag.second+1);
}
return 0;
}