NC50950. The Pilots Brothers' refrigerator
描述
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] . 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.
输入描述
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.
输出描述
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.
示例1
输入:
-+-- ---- ---- -+--
输出:
6 1 1 1 3 1 4 4 1 4 3 4 4
C++(g++ 7.5.0) 解法, 执行用时: 3ms, 内存消耗: 436K, 提交时间: 2023-07-16 16:09:42
#include<bits/stdc++.h> using namespace std; long long ans,t; vector<pair<long long,long long> >vt; int main(){ string s[4];cin>>s[0]>>s[1]>>s[2]>>s[3]; for(register int i=0;i<4;++i) for(register int j=0;j<4;++j){ t=0; for(register int k=0;k<4;++k)if(s[i][k]=='+')++t; for(register int k=0;k<4;++k)if(s[k][j]=='+'&&k!=i)++t; if(t&1)++ans,vt.push_back(make_pair(i+1,j+1)); } printf("%lld\n",ans); for(register int i=0;i<vt.size();++i)printf("%lld %lld\n",vt[i].first,vt[i].second); }
C++11(clang++ 3.9) 解法, 执行用时: 6ms, 内存消耗: 508K, 提交时间: 2020-01-29 10:33:11
#include<bits/stdc++.h> using namespace std; int ans,x[20],y[20]; char a[10][10];bool f[10][10]; int main(){ for(int i=1;i<5;++i){ cin>>a[i]; for(int j=0;j<4;++j){ if (a[i][j]=='-')f[i][j+1]=0; else f[i][j+1]=1; } } for(int i=1;i<5;++i) for(int j=1;j<5;++j){ int t=f[i][j]; for(int k=1;k<5;++k){ t^=f[i][k];t^=f[k][j]; } if (t){x[++ans]=i;y[ans]=j;} } cout<<ans<<endl; for(int i=1;i<=ans;++i)cout<<x[i]<<" "<<y[i]<<endl; return 0; }