Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 25859 | Accepted: 9979 | 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 rowi and all handles in columnj.
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
4*4的矩阵,16个开关,+代表开关处于关闭状态,-是打开状态,可以选取一个坐标,使以此点为中心的横排和纵排所有开关状态反转,问最少多少步使全部开关处于打开状态
由于是两种状态,所以每个坐标最多只能选取一次,选两次相当于反转两次,又回到原状态,相当于没动,所以16个点一共2^16种状态,因而最多需要16步,暴力可行
最多16步,加上不需要反转,也就是0,一共17种步数,可以依次递归i步能否满足,找出最小步数,并储存输出
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn = 1e3+10;
int s[5][5], sum;
bool flag;
struct node{
int x, y;
} cnt[maxn];
void mov(int x, int y){
s[x][y] = !s[x][y];
for(int i = 1; i <= 4; i++){
s[x][i] = !s[x][i];
s[i][y] = !s[i][y];
}
}
bool judge(){
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
if(!s[i][j]) return false;
}
}
return true;
}
void DFS(int x, int y, int step, int ans){
if(step == ans){
flag = judge();
sum = ans;
return ;
}
if(flag || x > 4) return ;
mov(x, y);
if(y < 4){
DFS(x, y + 1, step + 1, ans);
cnt[step].x = x;
cnt[step].y = y;
}
else{
DFS(x + 1, 1, step + 1, ans);
cnt[step].x = x;
cnt[step].y = y;
}
mov(x, y);
if(y < 4) DFS(x, y + 1, step, ans);
else DFS(x + 1, 1, step, ans);
}
int main(){
char u;
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
cin >> u;
if(u == '-') s[i][j] = 1;
else s[i][j] = 0;
}
}
flag = 0;
for(int i = 0; i <= 16; i++){
DFS(1, 1, 0, i);
if(flag){
cout << sum << endl;
break;
}
}
if(flag){
for(int i = 0; i < sum; i++) cout << cnt[i].x << ' ' << cnt[i].y << endl;
}
return 0;
}
参考巨巨思路,将一个点包含它本身和与它同排同列的点反转,本身状态改变7次,与它同排同列的改变4次,其余点改变2次,偶数次相当于没有变化,所以一个点包含它本身和与它同排同列的点都反转一次相当于只反转本身,所以查找所有的+,修改整体的改变次数,最后扫描矩阵,奇数的个数就是最少步数,输出奇数点即可
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn = 1e5+10;
int s[5][5];
void enu(int x, int y){
for(int i = 1; i <= 4; i++){
s[x][i]++;
if(i != x) s[i][y]++;
}
}
int ans(){
int c = 0;
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
if(s[i][j] & 1) c++;
}
}
return c;
}
int main(){
char u;
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
cin >> u;
if(u == '+') enu(i, j);
}
}
printf("%d\n", ans());
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
if(s[i][j] & 1) cout << i << ' ' << j << endl;
}
}
return 0;
}