AcWing 116. Vector存取状态
原题链接
简单
作者:
Value
,
2020-04-09 11:22:18
,
所有人可见
,
阅读 705
Vector存取状态
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
typedef pair<int, int> pii;
vector<pii> res;
int step;
int a[4][4], backup[4][4];
/*
“+” 闭合状态(0) “-” 打开状态(1)
目标:全打开状态(1)
*/
void read(){
char tmp;
for(int i = 0; i < 4; i ++ ){
for(int j = 0; j < 4; j ++ ){
cin >> tmp;
if(tmp == '+') a[i][j] = 0;
else a[i][j] = 1;
}
}
}
void work(int x, int y){
for(int i = 0; i < 4; i ++ ) backup[x][i] ^= 1;
for(int i = 0; i < 4; i ++ ) backup[i][y] ^= 1;
backup[x][y] ^= 1;
}
int main(){
read();
step = 17;
for(int k = 0; k < (1 << 16); k ++ ){
memcpy(backup, a, sizeof a);
vector<pii> tmp;
int cnt = 0;
//1:需要按下 0:不需要按下
for(int i = 0; i < 16; i ++ ){
if(k >> i & 1){
cnt++;
work(i/4, i%4);
tmp.push_back({i/4+1, i%4+1});
}
}
//检验
bool flag = true;
for(int i = 0; i < 4; i ++ ){
for(int j = 0; j < 4; j ++ ){
if(!backup[i][j]) {flag = false; break;}
}
}
if(flag && cnt < step){
res = tmp;
step = cnt;
}
}
cout << step << endl;
for(int i = 0; i < res.size(); i ++ ){
cout << res[i].first << " " << res[i].second << endl;
}
return 0;
}